annotate src/paint.c @ 107:069868161f63

Fix bug of get_now()
author Thinker K.F. Li <thinker@branda.to>
date Fri, 12 Sep 2008 12:55:13 +0800
parents 1ca417f741f1
children e96a584487af
rev   line source
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <stdlib.h>
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <cairo.h>
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include "paint.h"
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
6 #define ASSERT(x)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
7
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
8 /*! \brief Solid color paint.
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
9 */
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 typedef struct _paint_color {
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 paint_t paint;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
12 co_comp_t r, g, b, a;
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 redraw_man_t *rdman;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 } paint_color_t;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 static void paint_color_prepare(paint_t *paint, cairo_t *cr) {
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 paint_color_t *color = (paint_color_t *)paint;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
20 cairo_set_source_rgba(cr, color->r, color->g, color->b, color->a);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 static void paint_color_free(paint_t *paint) {
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 paint_color_t *color = (paint_color_t *)paint;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 shnode_list_free(color->rdman, paint->members);
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 free(paint);
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 paint_t *paint_color_new(redraw_man_t *rdman,
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
31 co_comp_t r, co_comp_t g,
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
32 co_comp_t b, co_comp_t a) {
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 paint_color_t *color;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 color = (paint_color_t *)malloc(sizeof(paint_color_t));
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 if(color == NULL)
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 return NULL;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 color->rdman = rdman;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 color->r = r;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 color->g = g;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 color->b = b;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
42 color->a = a;
19
cf6d65398619 More animation demo
Thinker K.F. Li <thinker@branda.to>
parents: 18
diff changeset
43 paint_init(&color->paint, paint_color_prepare, paint_color_free);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 return (paint_t *)color;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 void paint_color_set(paint_t *paint,
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
48 co_comp_t r, co_comp_t g,
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
49 co_comp_t b, co_comp_t a) {
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 paint_color_t *color = (paint_color_t *)paint;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 color->r = r;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 color->g = g;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 color->b = b;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
55 color->a = a;
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 }
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
57
52
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
58 void paint_color_get(paint_t *paint,
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
59 co_comp_t *r, co_comp_t *g,
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
60 co_comp_t *b, co_comp_t *a) {
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
61 paint_color_t *color = (paint_color_t *)paint;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
62
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
63 *r = color->r;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
64 *g = color->g;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
65 *b = color->b;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
66 *a = color->a;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
67 }
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
68
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
69 /*! \brief Linear gradient.
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
70 */
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
71 typedef struct _paint_linear {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
72 paint_t paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
73 co_aix x1, y1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
74 co_aix x2, y2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
75 int n_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
76 grad_stop_t *stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
77 int flags;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
78 cairo_pattern_t *ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
79 } paint_linear_t;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
80
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
81 #define LIF_DIRTY 0x1
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
82
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
83 static void paint_linear_prepare(paint_t *paint, cairo_t *cr) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
84 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
85 cairo_pattern_t *ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
86 grad_stop_t *stop;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
87 int i;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
88
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
89 ptn = linear->ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
90 if(linear->flags & LIF_DIRTY) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
91 if(ptn)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
92 cairo_pattern_destroy(ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
93 linear->flags &= ~LIF_DIRTY;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
94 ptn = cairo_pattern_create_linear(linear->x1, linear->y1,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
95 linear->x2, linear->y2);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
96 for(i = 0; i < linear->n_stops; i++) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
97 stop = &linear->stops[i];
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
98 cairo_pattern_add_color_stop_rgba(ptn, stop->offset,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
99 stop->r, stop->g, stop->b,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
100 stop->a);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
101 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
102 linear->ptn = ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
103 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
104
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
105 cairo_set_source(cr, ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
106 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
107
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
108 static void paint_linear_free(paint_t *paint) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
109 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
110
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
111 if(linear->ptn)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
112 cairo_pattern_destroy(linear->ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
113 free(paint);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
114 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
115
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
116 paint_t *paint_linear_new(redraw_man_t *rdman,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
117 co_aix x1, co_aix y1, co_aix x2, co_aix y2) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
118 paint_linear_t *linear;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
119
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
120 linear = (paint_linear_t *)malloc(sizeof(paint_linear_t));
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
121 if(linear == NULL)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
122 return NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
123
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
124 paint_init(&linear->paint, paint_linear_prepare, paint_linear_free);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
125
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
126 linear->x1 = x1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
127 linear->y1 = y1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
128 linear->x2 = x2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
129 linear->y2 = y2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
130 linear->n_stops = 0;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
131 linear->stops = NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
132 linear->flags = LIF_DIRTY;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
133 linear->ptn = NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
134
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
135 return (paint_t *)linear;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
136 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
137
58
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
138 /*! \brief Setup color stop for a linear radient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
139 *
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
140 * stops should be managed by users of the function. It should be
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
141 * available before the paint being freed or changed to another
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
142 * array of stops.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
143 */
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
144 grad_stop_t *paint_linear_stops(paint_t *paint,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
145 int n_stops,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
146 grad_stop_t *stops) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
147 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
148 grad_stop_t *old_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
149
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
150 old_stops = linear->stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
151 linear->n_stops = n_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
152 linear->stops = stops;
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
153 linear->flags |= LIF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
154
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
155 return old_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
156 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
157
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
158 /*! \brief Radial gradient.
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
159 *
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
160 * NOTE: The only supported gradient unit is userSpaceOnUse.
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
161 */
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
162 typedef struct _paint_radial {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
163 paint_t paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
164 co_aix cx, cy;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
165 co_aix r;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
166 int n_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
167 grad_stop_t *stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
168 int flags;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
169 cairo_pattern_t *ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
170 } paint_radial_t;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
171
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
172 #define RDF_DIRTY 0x1
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
173
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
174 static void paint_radial_prepare(paint_t *paint, cairo_t *cr) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
175 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
176 cairo_pattern_t *ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
177 grad_stop_t *stop;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
178 int i;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
179
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
180 if(radial->flags & RDF_DIRTY) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
181 ptn = cairo_pattern_create_radial(radial->cx, radial->cy, 0,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
182 radial->cx, radial->cy,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
183 radial->r);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
184 ASSERT(ptn != NULL);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
185 stop = radial->stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
186 for(i = 0; i < radial->n_stops; i++, stop++) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
187 cairo_pattern_add_color_stop_rgba(ptn, stop->offset,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
188 stop->r, stop->g,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
189 stop->b, stop->a);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
190 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
191 cairo_pattern_destroy(radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
192 radial->ptn = ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
193 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
194 cairo_set_source(cr, radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
195 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
196
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
197 static void paint_radial_free(paint_t *paint) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
198 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
199
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
200 if(radial->ptn)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
201 cairo_pattern_destroy(radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
202 free(paint);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
203 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
204
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
205 paint_t *paint_radial_new(redraw_man_t *rdman,
56
e444a8c01735 Change interface of paint_radial_new()
Thinker K.F. Li <thinker@branda.to>
parents: 55
diff changeset
206 co_aix cx, co_aix cy, co_aix r) {
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
207 paint_radial_t *radial;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
208
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
209 radial = O_ALLOC(paint_radial_t);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
210 if(radial == NULL)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
211 return NULL;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
212
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
213 paint_init(&radial->paint, paint_radial_prepare, paint_radial_free);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
214 radial->cx = cx;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
215 radial->cy = cy;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
216 radial->r = r;
56
e444a8c01735 Change interface of paint_radial_new()
Thinker K.F. Li <thinker@branda.to>
parents: 55
diff changeset
217 radial->n_stops = 0;
e444a8c01735 Change interface of paint_radial_new()
Thinker K.F. Li <thinker@branda.to>
parents: 55
diff changeset
218 radial->stops = NULL;
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
219 radial->flags = RDF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
220 radial->ptn = NULL;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
221
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
222 return (paint_t *)radial;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
223 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
224
58
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
225 /*! \brief Setup color stop for a radial radient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
226 *
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
227 * stops should be managed by users of the function. It should be
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
228 * available before the paint being freed or changed to another
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
229 * array of stops.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
230 */
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
231 grad_stop_t *paint_radial_stops(paint_t *paint,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
232 int n_stops,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
233 grad_stop_t *stops) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
234 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
235 grad_stop_t *old_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
236
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
237 old_stops = radial->stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
238 radial->n_stops = n_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
239 radial->stops = stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
240 radial->flags |= RDF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
241
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
242 return old_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
243 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
244