]> Dogcows Code - chaz/openbox/blob - render/gradient.c
fix the PangoLanguage thing to get from locale
[chaz/openbox] / render / gradient.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 gradient.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "render.h"
21 #include "gradient.h"
22 #include "color.h"
23 #include <glib.h>
24
25 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised);
26 static void gradient_solid(RrAppearance *l, gint w, gint h);
27 static void gradient_vertical(RrSurface *sf, gint w, gint h);
28 static void gradient_horizontal(RrSurface *sf, gint w, gint h);
29 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
30 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
31 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
32
33 void RrRender(RrAppearance *a, gint w, gint h)
34 {
35 RrPixel32 *data = a->surface.pixel_data;
36 RrPixel32 current;
37 guint r,g,b;
38 gint off, x;
39
40 switch (a->surface.grad) {
41 case RR_SURFACE_SOLID:
42 gradient_solid(a, w, h);
43 break;
44 case RR_SURFACE_VERTICAL:
45 gradient_vertical(&a->surface, w, h);
46 break;
47 case RR_SURFACE_HORIZONTAL:
48 gradient_horizontal(&a->surface, w, h);
49 break;
50 case RR_SURFACE_DIAGONAL:
51 gradient_diagonal(&a->surface, w, h);
52 break;
53 case RR_SURFACE_CROSS_DIAGONAL:
54 gradient_crossdiagonal(&a->surface, w, h);
55 break;
56 case RR_SURFACE_PYRAMID:
57 gradient_pyramid(&a->surface, w, h);
58 break;
59 default:
60 g_assert_not_reached(); /* unhandled gradient */
61 return;
62 }
63
64 if (a->surface.interlaced) {
65 gint i;
66 RrPixel32 *p;
67
68 r = a->surface.interlace_color->r;
69 g = a->surface.interlace_color->g;
70 b = a->surface.interlace_color->b;
71 current = (r << RrDefaultRedOffset)
72 + (g << RrDefaultGreenOffset)
73 + (b << RrDefaultBlueOffset);
74 p = data;
75 for (i = 0; i < h; i += 2, p += w)
76 for (x = 0; x < w; ++x, ++p)
77 *p = current;
78 }
79
80 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
81 r = a->surface.border_color->r;
82 g = a->surface.border_color->g;
83 b = a->surface.border_color->b;
84 current = (r << RrDefaultRedOffset)
85 + (g << RrDefaultGreenOffset)
86 + (b << RrDefaultBlueOffset);
87 for (off = 0, x = 0; x < w; ++x, off++) {
88 *(data + off) = current;
89 *(data + off + ((h-1) * w)) = current;
90 }
91 for (off = 0, x = 0; x < h; ++x, off++) {
92 *(data + (off * w)) = current;
93 *(data + (off * w) + w - 1) = current;
94 }
95 }
96
97 if (a->surface.relief != RR_RELIEF_FLAT) {
98 if (a->surface.bevel == RR_BEVEL_1) {
99 for (off = 1, x = 1; x < w - 1; ++x, off++)
100 highlight(data + off,
101 data + off + (h-1) * w,
102 a->surface.relief==RR_RELIEF_RAISED);
103 for (off = 0, x = 0; x < h; ++x, off++)
104 highlight(data + off * w,
105 data + off * w + w - 1,
106 a->surface.relief==RR_RELIEF_RAISED);
107 }
108
109 if (a->surface.bevel == RR_BEVEL_2) {
110 for (off = 2, x = 2; x < w - 2; ++x, off++)
111 highlight(data + off + w,
112 data + off + (h-2) * w,
113 a->surface.relief==RR_RELIEF_RAISED);
114 for (off = 1, x = 1; x < h-1; ++x, off++)
115 highlight(data + off * w + 1,
116 data + off * w + w - 2,
117 a->surface.relief==RR_RELIEF_RAISED);
118 }
119 }
120 }
121
122 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised)
123 {
124 gint r, g, b;
125
126 RrPixel32 *up, *down;
127 if (raised) {
128 up = x;
129 down = y;
130 } else {
131 up = y;
132 down = x;
133 }
134 r = (*up >> RrDefaultRedOffset) & 0xFF;
135 r += r >> 1;
136 g = (*up >> RrDefaultGreenOffset) & 0xFF;
137 g += g >> 1;
138 b = (*up >> RrDefaultBlueOffset) & 0xFF;
139 b += b >> 1;
140 if (r > 0xFF) r = 0xFF;
141 if (g > 0xFF) g = 0xFF;
142 if (b > 0xFF) b = 0xFF;
143 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
144 + (b << RrDefaultBlueOffset);
145
146 r = (*down >> RrDefaultRedOffset) & 0xFF;
147 r = (r >> 1) + (r >> 2);
148 g = (*down >> RrDefaultGreenOffset) & 0xFF;
149 g = (g >> 1) + (g >> 2);
150 b = (*down >> RrDefaultBlueOffset) & 0xFF;
151 b = (b >> 1) + (b >> 2);
152 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
153 + (b << RrDefaultBlueOffset);
154 }
155
156 static void create_bevel_colors(RrAppearance *l)
157 {
158 gint r, g, b;
159
160 /* light color */
161 r = l->surface.primary->r;
162 r += r >> 1;
163 g = l->surface.primary->g;
164 g += g >> 1;
165 b = l->surface.primary->b;
166 b += b >> 1;
167 if (r > 0xFF) r = 0xFF;
168 if (g > 0xFF) g = 0xFF;
169 if (b > 0xFF) b = 0xFF;
170 g_assert(!l->surface.bevel_light);
171 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
172
173 /* dark color */
174 r = l->surface.primary->r;
175 r = (r >> 1) + (r >> 2);
176 g = l->surface.primary->g;
177 g = (g >> 1) + (g >> 2);
178 b = l->surface.primary->b;
179 b = (b >> 1) + (b >> 2);
180 g_assert(!l->surface.bevel_dark);
181 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
182 }
183
184 static void gradient_solid(RrAppearance *l, gint w, gint h)
185 {
186 RrPixel32 pix;
187 gint i, a, b;
188 RrSurface *sp = &l->surface;
189 gint left = 0, top = 0, right = w - 1, bottom = h - 1;
190
191 pix = (sp->primary->r << RrDefaultRedOffset)
192 + (sp->primary->g << RrDefaultGreenOffset)
193 + (sp->primary->b << RrDefaultBlueOffset);
194
195 for (a = 0; a < w; a++)
196 for (b = 0; b < h; b++)
197 sp->pixel_data[a + b * w] = pix;
198
199 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
200 0, 0, w, h);
201
202 if (sp->interlaced) {
203 for (i = 0; i < h; i += 2)
204 XDrawLine(RrDisplay(l->inst), l->pixmap,
205 RrColorGC(sp->interlace_color),
206 0, i, w, i);
207 }
208
209 switch (sp->relief) {
210 case RR_RELIEF_RAISED:
211 if (!sp->bevel_dark)
212 create_bevel_colors(l);
213
214 switch (sp->bevel) {
215 case RR_BEVEL_1:
216 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
217 left, bottom, right, bottom);
218 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
219 right, bottom, right, top);
220
221 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
222 left, top, right, top);
223 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
224 left, bottom, left, top);
225 break;
226 case RR_BEVEL_2:
227 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
228 left + 1, bottom - 2, right - 2, bottom - 2);
229 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
230 right - 2, bottom - 2, right - 2, top + 1);
231
232 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
233 left + 1, top + 1, right - 2, top + 1);
234 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
235 left + 1, bottom - 2, left + 1, top + 1);
236 break;
237 default:
238 g_assert_not_reached(); /* unhandled BevelType */
239 }
240 break;
241 case RR_RELIEF_SUNKEN:
242 if (!sp->bevel_dark)
243 create_bevel_colors(l);
244
245 switch (sp->bevel) {
246 case RR_BEVEL_1:
247 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
248 left, bottom, right, bottom);
249 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
250 right, bottom, right, top);
251
252 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
253 left, top, right, top);
254 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
255 left, bottom, left, top);
256 break;
257 case RR_BEVEL_2:
258 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
259 left + 1, bottom - 2, right - 2, bottom - 2);
260 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
261 right - 2, bottom - 2, right - 2, top + 1);
262
263 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
264 left + 1, top + 1, right - 2, top + 1);
265 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
266 left + 1, bottom - 2, left + 1, top + 1);
267
268 break;
269 default:
270 g_assert_not_reached(); /* unhandled BevelType */
271 }
272 break;
273 case RR_RELIEF_FLAT:
274 if (sp->border) {
275 XDrawRectangle(RrDisplay(l->inst), l->pixmap,
276 RrColorGC(sp->border_color),
277 left, top, right, bottom);
278 }
279 break;
280 default:
281 g_assert_not_reached(); /* unhandled ReliefType */
282 }
283 }
284
285 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
286
287 #define VARS(x) \
288 guint color##x[3]; \
289 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
290 gboolean bigslope##x[3] /* color slope > 1 */
291
292 #define SETUP(x, from, to, w) \
293 len##x = w; \
294 \
295 color##x[0] = from->r; \
296 color##x[1] = from->g; \
297 color##x[2] = from->b; \
298 \
299 cdelta##x[0] = to->r - from->r; \
300 cdelta##x[1] = to->g - from->g; \
301 cdelta##x[2] = to->b - from->b; \
302 \
303 if (cdelta##x[0] < 0) { \
304 cdelta##x[0] = -cdelta##x[0]; \
305 inc##x[0] = -1; \
306 } else \
307 inc##x[0] = 1; \
308 if (cdelta##x[1] < 0) { \
309 cdelta##x[1] = -cdelta##x[1]; \
310 inc##x[1] = -1; \
311 } else \
312 inc##x[1] = 1; \
313 if (cdelta##x[2] < 0) { \
314 cdelta##x[2] = -cdelta##x[2]; \
315 inc##x[2] = -1; \
316 } else \
317 inc##x[2] = 1; \
318 bigslope##x[0] = cdelta##x[0] > w;\
319 bigslope##x[1] = cdelta##x[1] > w;\
320 bigslope##x[2] = cdelta##x[2] > w
321
322 #define COLOR_RR(x, c) \
323 c->r = color##x[0]; \
324 c->g = color##x[1]; \
325 c->b = color##x[2]
326
327 #define COLOR(x) \
328 ((color##x[0] << RrDefaultRedOffset) + \
329 (color##x[1] << RrDefaultGreenOffset) + \
330 (color##x[2] << RrDefaultBlueOffset))
331
332 #define INCREMENT(x, i) \
333 (inc##x[i])
334
335 #define NEXT(x) \
336 { \
337 gint i; \
338 for (i = 2; i >= 0; --i) { \
339 if (!cdelta##x[i]) continue; \
340 \
341 if (!bigslope##x[i]) { \
342 /* Y (color) is dependant on X */ \
343 error##x[i] += cdelta##x[i]; \
344 if ((error##x[i] << 1) >= len##x) { \
345 color##x[i] += INCREMENT(x, i); \
346 error##x[i] -= len##x; \
347 } \
348 } else { \
349 /* X is dependant on Y (color) */ \
350 while (1) { \
351 color##x[i] += INCREMENT(x, i); \
352 error##x[i] += len##x; \
353 if ((error##x[i] << 1) >= cdelta##x[i]) { \
354 error##x[i] -= cdelta##x[i]; \
355 break; \
356 } \
357 } \
358 } \
359 } \
360 }
361
362 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
363 {
364 gint x, y;
365 RrPixel32 *data = sf->pixel_data, *datav;
366 RrPixel32 current;
367
368 VARS(x);
369 SETUP(x, sf->primary, sf->secondary, w);
370
371 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
372 current = COLOR(x);
373 datav = data;
374 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
375 *datav = current;
376 datav += w;
377 }
378 ++data;
379
380 NEXT(x);
381 }
382 current = COLOR(x);
383 for (y = h - 1; y >= 0; --y) /* 0 -> h */
384 *(data + y * w) = current;
385 }
386
387 static void gradient_vertical(RrSurface *sf, gint w, gint h)
388 {
389 gint x, y;
390 RrPixel32 *data = sf->pixel_data;
391 RrPixel32 current;
392
393 VARS(y);
394 SETUP(y, sf->primary, sf->secondary, h);
395
396 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
397 current = COLOR(y);
398 for (x = w - 1; x >= 0; --x) /* 0 -> w */
399 *(data++) = current;
400
401 NEXT(y);
402 }
403 current = COLOR(y);
404 for (x = w - 1; x >= 0; --x) /* 0 -> w */
405 *(data++) = current;
406 }
407
408
409 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
410 {
411 gint x, y;
412 RrPixel32 *data = sf->pixel_data;
413 RrColor left, right;
414 RrColor extracorner;
415
416 VARS(lefty);
417 VARS(righty);
418 VARS(x);
419
420 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
421 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
422 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
423
424 SETUP(lefty, sf->primary, (&extracorner), h);
425 SETUP(righty, (&extracorner), sf->secondary, h);
426
427 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
428 COLOR_RR(lefty, (&left));
429 COLOR_RR(righty, (&right));
430
431 SETUP(x, (&left), (&right), w);
432
433 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
434 *(data++) = COLOR(x);
435
436 NEXT(x);
437 }
438 *(data++) = COLOR(x);
439
440 NEXT(lefty);
441 NEXT(righty);
442 }
443 COLOR_RR(lefty, (&left));
444 COLOR_RR(righty, (&right));
445
446 SETUP(x, (&left), (&right), w);
447
448 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
449 *(data++) = COLOR(x);
450
451 NEXT(x);
452 }
453 *data = COLOR(x);
454 }
455
456 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
457 {
458 gint x, y;
459 RrPixel32 *data = sf->pixel_data;
460 RrColor left, right;
461 RrColor extracorner;
462
463 VARS(lefty);
464 VARS(righty);
465 VARS(x);
466
467 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
468 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
469 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
470
471 SETUP(lefty, (&extracorner), sf->secondary, h);
472 SETUP(righty, sf->primary, (&extracorner), h);
473
474 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
475 COLOR_RR(lefty, (&left));
476 COLOR_RR(righty, (&right));
477
478 SETUP(x, (&left), (&right), w);
479
480 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
481 *(data++) = COLOR(x);
482
483 NEXT(x);
484 }
485 *(data++) = COLOR(x);
486
487 NEXT(lefty);
488 NEXT(righty);
489 }
490 COLOR_RR(lefty, (&left));
491 COLOR_RR(righty, (&right));
492
493 SETUP(x, (&left), (&right), w);
494
495 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
496 *(data++) = COLOR(x);
497
498 NEXT(x);
499 }
500 *data = COLOR(x);
501 }
502
503 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
504 {
505 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
506 RrPixel32 *data = sf->pixel_data;
507 RrPixel32 *end = data + inw*inh - 1;
508 RrPixel32 current;
509 RrColor left, right;
510 RrColor extracorner;
511
512 VARS(lefty);
513 VARS(righty);
514 VARS(x);
515
516 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
517 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
518 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
519
520 SETUP(lefty, (&extracorner), sf->secondary, h);
521 SETUP(righty, sf->primary, (&extracorner), h);
522
523 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
524 COLOR_RR(lefty, (&left));
525 COLOR_RR(righty, (&right));
526
527 SETUP(x, (&left), (&right), w);
528
529 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
530 current = COLOR(x);
531 *(data+x) = current;
532 *(data+inw-x) = current;
533 *(end-x) = current;
534 *(end-(inw-x)) = current;
535
536 NEXT(x);
537 }
538 current = COLOR(x);
539 *(data+x) = current;
540 *(data+inw-x) = current;
541 *(end-x) = current;
542 *(end-(inw-x)) = current;
543
544 data+=inw;
545 end-=inw;
546
547 NEXT(lefty);
548 NEXT(righty);
549 }
550 COLOR_RR(lefty, (&left));
551 COLOR_RR(righty, (&right));
552
553 SETUP(x, (&left), (&right), w);
554
555 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
556 current = COLOR(x);
557 *(data+x) = current;
558 *(data+inw-x) = current;
559 *(end-x) = current;
560 *(end-(inw-x)) = current;
561
562 NEXT(x);
563 }
564 current = COLOR(x);
565 *(data+x) = current;
566 *(data+inw-x) = current;
567 *(end-x) = current;
568 *(end-(inw-x)) = current;
569 }
570
This page took 0.06258 seconds and 4 git commands to generate.