Mercurial > MadButterfly
annotate src/paint.c @ 86:7d0580f89468
Fix bug of dealing matrix().
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 22 Aug 2008 16:14:53 +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 | 6 #define ASSERT(x) |
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 | 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 | 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 | 31 co_comp_t r, co_comp_t g, |
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 | 42 color->a = a; |
19 | 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 | 48 co_comp_t r, co_comp_t g, |
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 | 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 | 138 /*! \brief Setup color stop for a linear radient paint. |
139 * | |
140 * stops should be managed by users of the function. It should be | |
141 * available before the paint being freed or changed to another | |
142 * array of stops. | |
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 | 153 linear->flags |= LIF_DIRTY; |
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 | 158 /*! \brief Radial gradient. |
159 * | |
160 * NOTE: The only supported gradient unit is userSpaceOnUse. | |
161 */ | |
162 typedef struct _paint_radial { | |
163 paint_t paint; | |
164 co_aix cx, cy; | |
165 co_aix r; | |
166 int n_stops; | |
167 grad_stop_t *stops; | |
168 int flags; | |
169 cairo_pattern_t *ptn; | |
170 } paint_radial_t; | |
171 | |
172 #define RDF_DIRTY 0x1 | |
173 | |
174 static void paint_radial_prepare(paint_t *paint, cairo_t *cr) { | |
175 paint_radial_t *radial = (paint_radial_t *)paint; | |
176 cairo_pattern_t *ptn; | |
177 grad_stop_t *stop; | |
178 int i; | |
179 | |
180 if(radial->flags & RDF_DIRTY) { | |
181 ptn = cairo_pattern_create_radial(radial->cx, radial->cy, 0, | |
182 radial->cx, radial->cy, | |
183 radial->r); | |
184 ASSERT(ptn != NULL); | |
185 stop = radial->stops; | |
186 for(i = 0; i < radial->n_stops; i++, stop++) { | |
187 cairo_pattern_add_color_stop_rgba(ptn, stop->offset, | |
188 stop->r, stop->g, | |
189 stop->b, stop->a); | |
190 } | |
191 cairo_pattern_destroy(radial->ptn); | |
192 radial->ptn = ptn; | |
193 } | |
194 cairo_set_source(cr, radial->ptn); | |
195 } | |
196 | |
197 static void paint_radial_free(paint_t *paint) { | |
198 paint_radial_t *radial = (paint_radial_t *)paint; | |
199 | |
200 if(radial->ptn) | |
201 cairo_pattern_destroy(radial->ptn); | |
202 free(paint); | |
203 } | |
204 | |
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 | 207 paint_radial_t *radial; |
208 | |
209 radial = O_ALLOC(paint_radial_t); | |
210 if(radial == NULL) | |
211 return NULL; | |
212 | |
213 paint_init(&radial->paint, paint_radial_prepare, paint_radial_free); | |
214 radial->cx = cx; | |
215 radial->cy = cy; | |
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 | 219 radial->flags = RDF_DIRTY; |
220 radial->ptn = NULL; | |
221 | |
222 return (paint_t *)radial; | |
223 } | |
224 | |
58 | 225 /*! \brief Setup color stop for a radial radient paint. |
226 * | |
227 * stops should be managed by users of the function. It should be | |
228 * available before the paint being freed or changed to another | |
229 * array of stops. | |
230 */ | |
55 | 231 grad_stop_t *paint_radial_stops(paint_t *paint, |
232 int n_stops, | |
233 grad_stop_t *stops) { | |
234 paint_radial_t *radial = (paint_radial_t *)paint; | |
235 grad_stop_t *old_stops; | |
236 | |
237 old_stops = radial->stops; | |
238 radial->n_stops = n_stops; | |
239 radial->stops = stops; | |
240 radial->flags |= RDF_DIRTY; | |
241 | |
242 return old_stops; | |
243 } | |
244 |