]> Dogcows Code - chaz/openbox/blob - render/gradient.c
remove asserts pending sanity check.
[chaz/openbox] / render / gradient.c
1 #include <glib.h>
2 #include "render.h"
3 #include "gradient.h"
4 #include "../kernel/openbox.h"
5 #include "color.h"
6
7 void gradient_render(Surface *sf, int w, int h)
8 {
9 pixel32 *data = sf->data.planar.pixel_data;
10 pixel32 current;
11 unsigned int r,g,b;
12 int off, x;
13
14 switch (sf->data.planar.grad) {
15 case Background_Solid: /* already handled */
16 return;
17 case Background_Vertical:
18 gradient_vertical(sf, w, h);
19 break;
20 case Background_Horizontal:
21 gradient_horizontal(sf, w, h);
22 break;
23 case Background_Diagonal:
24 gradient_diagonal(sf, w, h);
25 break;
26 case Background_CrossDiagonal:
27 gradient_crossdiagonal(sf, w, h);
28 break;
29 default:
30 g_message("unhandled gradient");
31 return;
32 }
33
34 if (sf->data.planar.relief == Flat && sf->data.planar.border) {
35 r = sf->data.planar.border_color->r;
36 g = sf->data.planar.border_color->g;
37 b = sf->data.planar.border_color->b;
38 current = (r << default_red_shift)
39 + (g << default_green_shift)
40 + (b << default_blue_shift);
41 for (off = 0, x = 0; x < w; ++x, off++) {
42 *(data + off) = current;
43 *(data + off + ((h-1) * w)) = current;
44 }
45 for (off = 0, x = 0; x < h; ++x, off++) {
46 *(data + (off * w)) = current;
47 *(data + (off * w) + w - 1) = current;
48 }
49 }
50
51 if (sf->data.planar.relief != Flat) {
52 if (sf->data.planar.bevel == Bevel1) {
53 for (off = 1, x = 1; x < w - 1; ++x, off++)
54 highlight(data + off,
55 data + off + (h-1) * w,
56 sf->data.planar.relief==Raised);
57 for (off = 0, x = 0; x < h; ++x, off++)
58 highlight(data + off * w,
59 data + off * w + w - 1,
60 sf->data.planar.relief==Raised);
61 }
62
63 if (sf->data.planar.bevel == Bevel2) {
64 for (off = 2, x = 2; x < w - 2; ++x, off++)
65 highlight(data + off + w,
66 data + off + (h-2) * w,
67 sf->data.planar.relief==Raised);
68 for (off = 1, x = 1; x < h-1; ++x, off++)
69 highlight(data + off * w + 1,
70 data + off * w + w - 2,
71 sf->data.planar.relief==Raised);
72 }
73 }
74 }
75
76
77
78 void gradient_vertical(Surface *sf, int w, int h)
79 {
80 pixel32 *data = sf->data.planar.pixel_data;
81 pixel32 current;
82 float dr, dg, db;
83 unsigned int r,g,b;
84 int x, y;
85
86 dr = (float)(sf->data.planar.secondary->r - sf->data.planar.primary->r);
87 dr/= (float)h;
88
89 dg = (float)(sf->data.planar.secondary->g - sf->data.planar.primary->g);
90 dg/= (float)h;
91
92 db = (float)(sf->data.planar.secondary->b - sf->data.planar.primary->b);
93 db/= (float)h;
94
95 for (y = 0; y < h; ++y) {
96 r = sf->data.planar.primary->r + (int)(dr * y);
97 g = sf->data.planar.primary->g + (int)(dg * y);
98 b = sf->data.planar.primary->b + (int)(db * y);
99 current = (r << default_red_shift)
100 + (g << default_green_shift)
101 + (b << default_blue_shift);
102 for (x = 0; x < w; ++x, ++data)
103 *data = current;
104 }
105 }
106
107 void gradient_horizontal(Surface *sf, int w, int h)
108 {
109 pixel32 *data = sf->data.planar.pixel_data;
110 pixel32 current;
111 float dr, dg, db;
112 unsigned int r,g,b;
113 int x, y;
114
115 dr = (float)(sf->data.planar.secondary->r - sf->data.planar.primary->r);
116 dr/= (float)w;
117
118 dg = (float)(sf->data.planar.secondary->g - sf->data.planar.primary->g);
119 dg/= (float)w;
120
121 db = (float)(sf->data.planar.secondary->b - sf->data.planar.primary->b);
122 db/= (float)w;
123
124 for (x = 0; x < w; ++x, ++data) {
125 r = sf->data.planar.primary->r + (int)(dr * x);
126 g = sf->data.planar.primary->g + (int)(dg * x);
127 b = sf->data.planar.primary->b + (int)(db * x);
128 current = (r << default_red_shift)
129 + (g << default_green_shift)
130 + (b << default_blue_shift);
131 for (y = 0; y < h; ++y)
132 *(data + y*w) = current;
133 }
134 }
135
136 void gradient_diagonal(Surface *sf, int w, int h)
137 {
138 pixel32 *data = sf->data.planar.pixel_data;
139 pixel32 current;
140 float drx, dgx, dbx, dry, dgy, dby;
141 unsigned int r,g,b;
142 int x, y;
143
144 for (y = 0; y < h; ++y) {
145 drx = (float)(sf->data.planar.secondary->r - sf->data.planar.primary->r);
146 dry = drx/(float)h;
147 drx/= (float)w;
148
149 dgx = (float)(sf->data.planar.secondary->g - sf->data.planar.primary->g);
150 dgy = dgx/(float)h;
151 dgx/= (float)w;
152
153 dbx = (float)(sf->data.planar.secondary->b - sf->data.planar.primary->b);
154 dby = dbx/(float)h;
155 dbx/= (float)w;
156 for (x = 0; x < w; ++x, ++data) {
157 r = sf->data.planar.primary->r + ((int)(drx * x) + (int)(dry * y))/2;
158 g = sf->data.planar.primary->g + ((int)(dgx * x) + (int)(dgy * y))/2;
159 b = sf->data.planar.primary->b + ((int)(dbx * x) + (int)(dby * y))/2;
160 current = (r << default_red_shift)
161 + (g << default_green_shift)
162 + (b << default_blue_shift);
163 *data = current;
164 }
165 }
166 }
167
168 void gradient_crossdiagonal(Surface *sf, int w, int h)
169 {
170 pixel32 *data = sf->data.planar.pixel_data;
171 pixel32 current;
172 float drx, dgx, dbx, dry, dgy, dby;
173 unsigned int r,g,b;
174 int x, y;
175
176 for (y = 0; y < h; ++y) {
177 drx = (float)(sf->data.planar.secondary->r - sf->data.planar.primary->r);
178 dry = drx/(float)h;
179 drx/= (float)w;
180
181 dgx = (float)(sf->data.planar.secondary->g - sf->data.planar.primary->g);
182 dgy = dgx/(float)h;
183 dgx/= (float)w;
184
185 dbx = (float)(sf->data.planar.secondary->b - sf->data.planar.primary->b);
186 dby = dbx/(float)h;
187 dbx/= (float)w;
188 for (x = w; x > 0; --x, ++data) {
189 r = sf->data.planar.primary->r + ((int)(drx * (x-1)) + (int)(dry * y))/2;
190 g = sf->data.planar.primary->g + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
191 b = sf->data.planar.primary->b + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
192 current = (r << default_red_shift)
193 + (g << default_green_shift)
194 + (b << default_blue_shift);
195 *data = current;
196 }
197 }
198 }
199
200 void highlight(pixel32 *x, pixel32 *y, gboolean raised)
201 {
202 int r, g, b;
203
204 pixel32 *up, *down;
205 if (raised) {
206 up = x;
207 down = y;
208 } else {
209 up = y;
210 down = x;
211 }
212 r = (*up >> default_red_shift) & 0xFF;
213 r += r >> 1;
214 g = (*up >> default_green_shift) & 0xFF;
215 g += g >> 1;
216 b = (*up >> default_blue_shift) & 0xFF;
217 b += b >> 1;
218 if (r > 255) r = 255;
219 if (g > 255) g = 255;
220 if (b > 255) b = 255;
221 *up = (r << default_red_shift) + (g << default_green_shift)
222 + (b << default_blue_shift);
223
224 r = (*down >> default_red_shift) & 0xFF;
225 r = (r >> 1) + (r >> 2);
226 g = (*down >> default_green_shift) & 0xFF;
227 g = (g >> 1) + (g >> 2);
228 b = (*down >> default_blue_shift) & 0xFF;
229 b = (b >> 1) + (b >> 2);
230 *down = (r << default_red_shift) + (g << default_green_shift)
231 + (b << default_blue_shift);
232 }
233
234 void gradient_solid(Appearance *l, int x, int y, int w, int h)
235 {
236 pixel32 pix;
237 int i, a, b;
238 PlanarSurface *sp = &l->surface.data.planar;
239 int left = x, top = y, right = w - 1, bottom = h - 1;
240
241 if (sp->primary->gc == None)
242 color_allocate_gc(sp->primary);
243 pix = (sp->primary->r << default_red_shift)
244 + (sp->primary->g << default_green_shift)
245 + (sp->primary->b << default_blue_shift);
246
247 for (a = 0; a < l->area.width; a++)
248 for (b = 0; b < l->area.height; b++)
249 sp->pixel_data[a + b*l->area.width] = pix;
250
251 XFillRectangle(ob_display, l->pixmap, sp->primary->gc
252 , x, y, w, h);
253
254 if (l->surface.data.planar.interlaced) {
255 if (sp->secondary->gc == None)
256 color_allocate_gc(sp->secondary);
257 for (i = y; i < h; i += 2)
258 XDrawLine(ob_display, l->pixmap, sp->secondary->gc,
259 x, i, w, i);
260 }
261 /*
262 switch (texture.relief()) {
263 case RenderTexture::Raised:
264 switch (texture.bevel()) {
265 case RenderTexture::Bevel1:
266 XDrawLine(ob_display, l->pixmap, texture.bevelDarkColor().gc(),
267 left, bottom, right, bottom);
268 XDrawLine(ob_display, l->pixmap, texture.bevelDarkColor().gc(),
269 right, bottom, right, top);
270
271 XDrawLine(ob_display, l->pixmap, texture.bevelLightColor().gc(),
272 left, top, right, top);
273 XDrawLine(ob_display, l->pixmap, texture.bevelLightColor().gc(),
274 left, bottom, left, top);
275 break;
276 case RenderTexture::Bevel2:
277 XDrawLine(ob_display, l->pixmap, texture.bevelDarkColor().gc(),
278 left + 1, bottom - 2, right - 2, bottom - 2);
279 XDrawLine(ob_display, l->pixmap, texture.bevelDarkColor().gc(),
280 right - 2, bottom - 2, right - 2, top + 1);
281
282 XDrawLine(ob_display, l->pixmap, texture.bevelLightColor().gc(),
283 left + 1, top + 1, right - 2, top + 1);
284 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
285 left + 1, bottom - 2, left + 1, top + 1);
286 break;
287 default:
288 assert(false); // unhandled RenderTexture::BevelType
289 }
290 break;
291 case RenderTexture::Sunken:
292 switch (texture.bevel()) {
293 case RenderTexture::Bevel1:
294 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
295 left, bottom, right, bottom);
296 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
297 right, bottom, right, top);
298
299 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
300 left, top, right, top);
301 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
302 left, bottom, left, top);
303 break;
304 case RenderTexture::Bevel2:
305 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
306 left + 1, bottom - 2, right - 2, bottom - 2);
307 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
308 right - 2, bottom - 2, right - 2, top + 1);
309
310 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
311 left + 1, top + 1, right - 2, top + 1);
312 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
313 left + 1, bottom - 2, left + 1, top + 1);
314
315 break;
316 default:
317 assert(false); // unhandled RenderTexture::BevelType
318 }
319 break;
320 case RenderTexture::Flat:
321 if (texture.border())
322 XDrawRectangle(**display, sf.pixmap(), texture.borderColor().gc(),
323 left, top, right, bottom);
324 break;
325 default:
326 assert(false); // unhandled RenderTexture::ReliefType
327 }
328 */
329 }
This page took 0.057072 seconds and 5 git commands to generate.