]> Dogcows Code - chaz/openbox/blob - render/gradient.c
9f245743b1bfceefbcece5c68e7be26bc7d92566
[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_split(RrAppearance *a, gint w, gint h);
28 static void gradient_vertical(RrSurface *sf, gint w, gint h);
29 static void gradient_horizontal(RrSurface *sf, gint w, gint h);
30 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
31 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
32 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
33
34 void RrRender(RrAppearance *a, gint w, gint h)
35 {
36 RrPixel32 *data = a->surface.pixel_data;
37 RrPixel32 current;
38 guint r,g,b;
39 gint off, x;
40
41 switch (a->surface.grad) {
42 case RR_SURFACE_SOLID:
43 gradient_solid(a, w, h);
44 break;
45 case RR_SURFACE_SPLIT:
46 gradient_split(a, w, h);
47 break;
48 case RR_SURFACE_VERTICAL:
49 gradient_vertical(&a->surface, w, h);
50 break;
51 case RR_SURFACE_HORIZONTAL:
52 gradient_horizontal(&a->surface, w, h);
53 break;
54 case RR_SURFACE_DIAGONAL:
55 gradient_diagonal(&a->surface, w, h);
56 break;
57 case RR_SURFACE_CROSS_DIAGONAL:
58 gradient_crossdiagonal(&a->surface, w, h);
59 break;
60 case RR_SURFACE_PYRAMID:
61 gradient_pyramid(&a->surface, w, h);
62 break;
63 default:
64 g_assert_not_reached(); /* unhandled gradient */
65 return;
66 }
67
68 if (a->surface.interlaced) {
69 gint i;
70 RrPixel32 *p;
71
72 r = a->surface.interlace_color->r;
73 g = a->surface.interlace_color->g;
74 b = a->surface.interlace_color->b;
75 current = (r << RrDefaultRedOffset)
76 + (g << RrDefaultGreenOffset)
77 + (b << RrDefaultBlueOffset);
78 p = data;
79 for (i = 0; i < h; i += 2, p += w)
80 for (x = 0; x < w; ++x, ++p)
81 *p = current;
82 }
83
84 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
85 r = a->surface.border_color->r;
86 g = a->surface.border_color->g;
87 b = a->surface.border_color->b;
88 current = (r << RrDefaultRedOffset)
89 + (g << RrDefaultGreenOffset)
90 + (b << RrDefaultBlueOffset);
91 for (off = 0, x = 0; x < w; ++x, off++) {
92 *(data + off) = current;
93 *(data + off + ((h-1) * w)) = current;
94 }
95 for (off = 0, x = 0; x < h; ++x, off++) {
96 *(data + (off * w)) = current;
97 *(data + (off * w) + w - 1) = current;
98 }
99 }
100
101 if (a->surface.relief != RR_RELIEF_FLAT) {
102 if (a->surface.bevel == RR_BEVEL_1) {
103 for (off = 1, x = 1; x < w - 1; ++x, off++)
104 highlight(data + off,
105 data + off + (h-1) * w,
106 a->surface.relief==RR_RELIEF_RAISED);
107 for (off = 0, x = 0; x < h; ++x, off++)
108 highlight(data + off * w,
109 data + off * w + w - 1,
110 a->surface.relief==RR_RELIEF_RAISED);
111 }
112
113 if (a->surface.bevel == RR_BEVEL_2) {
114 for (off = 2, x = 2; x < w - 2; ++x, off++)
115 highlight(data + off + w,
116 data + off + (h-2) * w,
117 a->surface.relief==RR_RELIEF_RAISED);
118 for (off = 1, x = 1; x < h-1; ++x, off++)
119 highlight(data + off * w + 1,
120 data + off * w + w - 2,
121 a->surface.relief==RR_RELIEF_RAISED);
122 }
123 }
124 }
125
126 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised)
127 {
128 gint r, g, b;
129
130 RrPixel32 *up, *down;
131 if (raised) {
132 up = x;
133 down = y;
134 } else {
135 up = y;
136 down = x;
137 }
138 r = (*up >> RrDefaultRedOffset) & 0xFF;
139 r += r >> 1;
140 g = (*up >> RrDefaultGreenOffset) & 0xFF;
141 g += g >> 1;
142 b = (*up >> RrDefaultBlueOffset) & 0xFF;
143 b += b >> 1;
144 if (r > 0xFF) r = 0xFF;
145 if (g > 0xFF) g = 0xFF;
146 if (b > 0xFF) b = 0xFF;
147 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
148 + (b << RrDefaultBlueOffset);
149
150 r = (*down >> RrDefaultRedOffset) & 0xFF;
151 r = (r >> 1) + (r >> 2);
152 g = (*down >> RrDefaultGreenOffset) & 0xFF;
153 g = (g >> 1) + (g >> 2);
154 b = (*down >> RrDefaultBlueOffset) & 0xFF;
155 b = (b >> 1) + (b >> 2);
156 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
157 + (b << RrDefaultBlueOffset);
158 }
159
160 static void create_bevel_colors(RrAppearance *l)
161 {
162 gint r, g, b;
163
164 /* light color */
165 r = l->surface.primary->r;
166 r += r >> 1;
167 g = l->surface.primary->g;
168 g += g >> 1;
169 b = l->surface.primary->b;
170 b += b >> 1;
171 if (r > 0xFF) r = 0xFF;
172 if (g > 0xFF) g = 0xFF;
173 if (b > 0xFF) b = 0xFF;
174 g_assert(!l->surface.bevel_light);
175 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
176
177 /* dark color */
178 r = l->surface.primary->r;
179 r = (r >> 1) + (r >> 2);
180 g = l->surface.primary->g;
181 g = (g >> 1) + (g >> 2);
182 b = l->surface.primary->b;
183 b = (b >> 1) + (b >> 2);
184 g_assert(!l->surface.bevel_dark);
185 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
186 }
187
188 static void gradient_solid(RrAppearance *l, gint w, gint h)
189 {
190 RrPixel32 pix;
191 gint i, a, b;
192 RrSurface *sp = &l->surface;
193 gint left = 0, top = 0, right = w - 1, bottom = h - 1;
194
195 pix = (sp->primary->r << RrDefaultRedOffset)
196 + (sp->primary->g << RrDefaultGreenOffset)
197 + (sp->primary->b << RrDefaultBlueOffset);
198
199 for (a = 0; a < w; a++)
200 for (b = 0; b < h; b++)
201 sp->pixel_data[a + b * w] = pix;
202
203 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
204 0, 0, w, h);
205
206 if (sp->interlaced) {
207 for (i = 0; i < h; i += 2)
208 XDrawLine(RrDisplay(l->inst), l->pixmap,
209 RrColorGC(sp->interlace_color),
210 0, i, w, i);
211 }
212
213 switch (sp->relief) {
214 case RR_RELIEF_RAISED:
215 if (!sp->bevel_dark)
216 create_bevel_colors(l);
217
218 switch (sp->bevel) {
219 case RR_BEVEL_1:
220 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
221 left, bottom, right, bottom);
222 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
223 right, bottom, right, top);
224
225 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
226 left, top, right, top);
227 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
228 left, bottom, left, top);
229 break;
230 case RR_BEVEL_2:
231 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
232 left + 1, bottom - 2, right - 2, bottom - 2);
233 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
234 right - 2, bottom - 2, right - 2, top + 1);
235
236 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
237 left + 1, top + 1, right - 2, top + 1);
238 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
239 left + 1, bottom - 2, left + 1, top + 1);
240 break;
241 default:
242 g_assert_not_reached(); /* unhandled BevelType */
243 }
244 break;
245 case RR_RELIEF_SUNKEN:
246 if (!sp->bevel_dark)
247 create_bevel_colors(l);
248
249 switch (sp->bevel) {
250 case RR_BEVEL_1:
251 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
252 left, bottom, right, bottom);
253 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
254 right, bottom, right, top);
255
256 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
257 left, top, right, top);
258 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
259 left, bottom, left, top);
260 break;
261 case RR_BEVEL_2:
262 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
263 left + 1, bottom - 2, right - 2, bottom - 2);
264 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
265 right - 2, bottom - 2, right - 2, top + 1);
266
267 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
268 left + 1, top + 1, right - 2, top + 1);
269 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
270 left + 1, bottom - 2, left + 1, top + 1);
271
272 break;
273 default:
274 g_assert_not_reached(); /* unhandled BevelType */
275 }
276 break;
277 case RR_RELIEF_FLAT:
278 if (sp->border) {
279 XDrawRectangle(RrDisplay(l->inst), l->pixmap,
280 RrColorGC(sp->border_color),
281 left, top, right, bottom);
282 }
283 break;
284 default:
285 g_assert_not_reached(); /* unhandled ReliefType */
286 }
287 }
288
289 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
290
291 #define VARS(x) \
292 guint color##x[3]; \
293 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
294 gboolean bigslope##x[3] /* color slope > 1 */
295
296 #define SETUP(x, from, to, w) \
297 len##x = w; \
298 \
299 color##x[0] = from->r; \
300 color##x[1] = from->g; \
301 color##x[2] = from->b; \
302 \
303 cdelta##x[0] = to->r - from->r; \
304 cdelta##x[1] = to->g - from->g; \
305 cdelta##x[2] = to->b - from->b; \
306 \
307 if (cdelta##x[0] < 0) { \
308 cdelta##x[0] = -cdelta##x[0]; \
309 inc##x[0] = -1; \
310 } else \
311 inc##x[0] = 1; \
312 if (cdelta##x[1] < 0) { \
313 cdelta##x[1] = -cdelta##x[1]; \
314 inc##x[1] = -1; \
315 } else \
316 inc##x[1] = 1; \
317 if (cdelta##x[2] < 0) { \
318 cdelta##x[2] = -cdelta##x[2]; \
319 inc##x[2] = -1; \
320 } else \
321 inc##x[2] = 1; \
322 bigslope##x[0] = cdelta##x[0] > w;\
323 bigslope##x[1] = cdelta##x[1] > w;\
324 bigslope##x[2] = cdelta##x[2] > w
325
326 #define COLOR_RR(x, c) \
327 c->r = color##x[0]; \
328 c->g = color##x[1]; \
329 c->b = color##x[2]
330
331 #define COLOR(x) \
332 ((color##x[0] << RrDefaultRedOffset) + \
333 (color##x[1] << RrDefaultGreenOffset) + \
334 (color##x[2] << RrDefaultBlueOffset))
335
336 #define INCREMENT(x, i) \
337 (inc##x[i])
338
339 #define NEXT(x) \
340 { \
341 gint i; \
342 for (i = 2; i >= 0; --i) { \
343 if (!cdelta##x[i]) continue; \
344 \
345 if (!bigslope##x[i]) { \
346 /* Y (color) is dependant on X */ \
347 error##x[i] += cdelta##x[i]; \
348 if ((error##x[i] << 1) >= len##x) { \
349 color##x[i] += INCREMENT(x, i); \
350 error##x[i] -= len##x; \
351 } \
352 } else { \
353 /* X is dependant on Y (color) */ \
354 while (1) { \
355 color##x[i] += INCREMENT(x, i); \
356 error##x[i] += len##x; \
357 if ((error##x[i] << 1) >= cdelta##x[i]) { \
358 error##x[i] -= cdelta##x[i]; \
359 break; \
360 } \
361 } \
362 } \
363 } \
364 }
365
366 static void gradient_split(RrAppearance *a, gint w, gint h)
367 {
368 gint x, y1, y3, r, g, b;
369 RrSurface *sf = &a->surface;
370 RrPixel32 *data = sf->pixel_data;
371 RrPixel32 current;
372 RrColor *primary_light, *secondary_light;
373
374 r = sf->primary->r;
375 r += r >> 2;
376 g = sf->primary->g;
377 g += g >> 2;
378 b = sf->primary->b;
379 b += b >> 2;
380 if (r > 0xFF) r = 0xFF;
381 if (g > 0xFF) g = 0xFF;
382 if (b > 0xFF) b = 0xFF;
383 primary_light = RrColorNew(a->inst, r, g, b);
384
385 r = sf->secondary->r;
386 r += r >> 4;
387 g = sf->secondary->g;
388 g += g >> 4;
389 b = sf->secondary->b;
390 b += b >> 4;
391 if (r > 0xFF) r = 0xFF;
392 if (g > 0xFF) g = 0xFF;
393 if (b > 0xFF) b = 0xFF;
394 secondary_light = RrColorNew(a->inst, r, g, b);
395
396 VARS(y1);
397 SETUP(y1, primary_light, sf->primary, (h / 2) -1);
398
399 VARS(y3);
400 SETUP(y3, sf->secondary, secondary_light, (h / 2) -1);
401
402 for (y1 = h - 1; y1 > (h / 2) -1; --y1) { /* 0 -> h-1 */
403 current = COLOR(y1);
404 for (x = w - 1; x >= 0; --x) /* 0 -> w */
405 *(data++) = current;
406
407 NEXT(y1);
408 }
409
410
411 for (y3 = (h / 2) - 1; y3 > 0; --y3) {
412 current = COLOR(y3);
413 for (x = w - 1; x >= 0; --x)
414 *(data++) = current;
415
416 NEXT(y3);
417 }
418
419 current = COLOR(y3);
420 for (x = w - 1; x >= 0; --x) /* 0 -> w */
421 *(data++) = current;
422
423 RrColorFree(primary_light);
424 RrColorFree(secondary_light);
425 }
426
427 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
428 {
429 gint x, y;
430 RrPixel32 *data = sf->pixel_data, *datav;
431 RrPixel32 current;
432
433 VARS(x);
434 SETUP(x, sf->primary, sf->secondary, w);
435
436 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
437 current = COLOR(x);
438 datav = data;
439 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
440 *datav = current;
441 datav += w;
442 }
443 ++data;
444
445 NEXT(x);
446 }
447 current = COLOR(x);
448 for (y = h - 1; y >= 0; --y) /* 0 -> h */
449 *(data + y * w) = current;
450 }
451
452 static void gradient_vertical(RrSurface *sf, gint w, gint h)
453 {
454 gint x, y;
455 RrPixel32 *data = sf->pixel_data;
456 RrPixel32 current;
457
458 VARS(y);
459 SETUP(y, sf->primary, sf->secondary, h);
460
461 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
462 current = COLOR(y);
463 for (x = w - 1; x >= 0; --x) /* 0 -> w */
464 *(data++) = current;
465
466 NEXT(y);
467 }
468 current = COLOR(y);
469 for (x = w - 1; x >= 0; --x) /* 0 -> w */
470 *(data++) = current;
471 }
472
473
474 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
475 {
476 gint x, y;
477 RrPixel32 *data = sf->pixel_data;
478 RrColor left, right;
479 RrColor extracorner;
480
481 VARS(lefty);
482 VARS(righty);
483 VARS(x);
484
485 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
486 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
487 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
488
489 SETUP(lefty, sf->primary, (&extracorner), h);
490 SETUP(righty, (&extracorner), sf->secondary, h);
491
492 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
493 COLOR_RR(lefty, (&left));
494 COLOR_RR(righty, (&right));
495
496 SETUP(x, (&left), (&right), w);
497
498 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
499 *(data++) = COLOR(x);
500
501 NEXT(x);
502 }
503 *(data++) = COLOR(x);
504
505 NEXT(lefty);
506 NEXT(righty);
507 }
508 COLOR_RR(lefty, (&left));
509 COLOR_RR(righty, (&right));
510
511 SETUP(x, (&left), (&right), w);
512
513 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
514 *(data++) = COLOR(x);
515
516 NEXT(x);
517 }
518 *data = COLOR(x);
519 }
520
521 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
522 {
523 gint x, y;
524 RrPixel32 *data = sf->pixel_data;
525 RrColor left, right;
526 RrColor extracorner;
527
528 VARS(lefty);
529 VARS(righty);
530 VARS(x);
531
532 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
533 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
534 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
535
536 SETUP(lefty, (&extracorner), sf->secondary, h);
537 SETUP(righty, sf->primary, (&extracorner), h);
538
539 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
540 COLOR_RR(lefty, (&left));
541 COLOR_RR(righty, (&right));
542
543 SETUP(x, (&left), (&right), w);
544
545 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
546 *(data++) = COLOR(x);
547
548 NEXT(x);
549 }
550 *(data++) = COLOR(x);
551
552 NEXT(lefty);
553 NEXT(righty);
554 }
555 COLOR_RR(lefty, (&left));
556 COLOR_RR(righty, (&right));
557
558 SETUP(x, (&left), (&right), w);
559
560 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
561 *(data++) = COLOR(x);
562
563 NEXT(x);
564 }
565 *data = COLOR(x);
566 }
567
568 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
569 {
570 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
571 RrPixel32 *data = sf->pixel_data;
572 RrPixel32 *end = data + inw*inh - 1;
573 RrPixel32 current;
574 RrColor left, right;
575 RrColor extracorner;
576
577 VARS(lefty);
578 VARS(righty);
579 VARS(x);
580
581 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
582 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
583 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
584
585 SETUP(lefty, (&extracorner), sf->secondary, h);
586 SETUP(righty, sf->primary, (&extracorner), h);
587
588 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
589 COLOR_RR(lefty, (&left));
590 COLOR_RR(righty, (&right));
591
592 SETUP(x, (&left), (&right), w);
593
594 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
595 current = COLOR(x);
596 *(data+x) = current;
597 *(data+inw-x) = current;
598 *(end-x) = current;
599 *(end-(inw-x)) = current;
600
601 NEXT(x);
602 }
603 current = COLOR(x);
604 *(data+x) = current;
605 *(data+inw-x) = current;
606 *(end-x) = current;
607 *(end-(inw-x)) = current;
608
609 data+=inw;
610 end-=inw;
611
612 NEXT(lefty);
613 NEXT(righty);
614 }
615 COLOR_RR(lefty, (&left));
616 COLOR_RR(righty, (&right));
617
618 SETUP(x, (&left), (&right), w);
619
620 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
621 current = COLOR(x);
622 *(data+x) = current;
623 *(data+inw-x) = current;
624 *(end-x) = current;
625 *(end-(inw-x)) = current;
626
627 NEXT(x);
628 }
629 current = COLOR(x);
630 *(data+x) = current;
631 *(data+inw-x) = current;
632 *(end-x) = current;
633 *(end-(inw-x)) = current;
634 }
635
This page took 0.064426 seconds and 4 git commands to generate.