]> Dogcows Code - chaz/openbox/blob - render/gradient.c
e4ec20a1000ce5f01a2409e460437572147223d1
[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) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6 Copyright (c) 2003 Derek Foreman
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include "render.h"
22 #include "gradient.h"
23 #include "color.h"
24 #include <glib.h>
25
26 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised);
27 static void gradient_solid(RrAppearance *l, gint w, gint h);
28 static void gradient_split(RrAppearance *a, gint w, gint h);
29 static void gradient_vertical(RrSurface *sf, gint w, gint h);
30 static void gradient_horizontal(RrSurface *sf, gint w, gint h);
31 static void gradient_osx(RrSurface *sf, gint w, gint h);
32 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
33 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
34 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
35
36 void RrRender(RrAppearance *a, gint w, gint h)
37 {
38 RrPixel32 *data = a->surface.pixel_data;
39 RrPixel32 current;
40 guint r,g,b;
41 gint off, x;
42
43 switch (a->surface.grad) {
44 case RR_SURFACE_SOLID:
45 gradient_solid(a, w, h);
46 break;
47 case RR_SURFACE_SPLIT:
48 gradient_split(a, w, h);
49 break;
50 case RR_SURFACE_VERTICAL:
51 gradient_vertical(&a->surface, w, h);
52 break;
53 case RR_SURFACE_HORIZONTAL:
54 gradient_horizontal(&a->surface, w, h);
55 break;
56 case RR_SURFACE_OSX:
57 gradient_osx(&a->surface, w, h);
58 break;
59 case RR_SURFACE_DIAGONAL:
60 gradient_diagonal(&a->surface, w, h);
61 break;
62 case RR_SURFACE_CROSS_DIAGONAL:
63 gradient_crossdiagonal(&a->surface, w, h);
64 break;
65 case RR_SURFACE_PYRAMID:
66 gradient_pyramid(&a->surface, w, h);
67 break;
68 default:
69 g_assert_not_reached(); /* unhandled gradient */
70 return;
71 }
72
73 if (a->surface.interlaced) {
74 gint i;
75 RrPixel32 *p;
76
77 r = a->surface.interlace_color->r;
78 g = a->surface.interlace_color->g;
79 b = a->surface.interlace_color->b;
80 current = (r << RrDefaultRedOffset)
81 + (g << RrDefaultGreenOffset)
82 + (b << RrDefaultBlueOffset);
83 p = data;
84 for (i = 0; i < h; i += 2, p += w)
85 for (x = 0; x < w; ++x, ++p)
86 *p = current;
87 }
88
89 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
90 r = a->surface.border_color->r;
91 g = a->surface.border_color->g;
92 b = a->surface.border_color->b;
93 current = (r << RrDefaultRedOffset)
94 + (g << RrDefaultGreenOffset)
95 + (b << RrDefaultBlueOffset);
96 for (off = 0, x = 0; x < w; ++x, off++) {
97 *(data + off) = current;
98 *(data + off + ((h-1) * w)) = current;
99 }
100 for (off = 0, x = 0; x < h; ++x, off++) {
101 *(data + (off * w)) = current;
102 *(data + (off * w) + w - 1) = current;
103 }
104 }
105
106 if (a->surface.relief != RR_RELIEF_FLAT) {
107 if (a->surface.bevel == RR_BEVEL_1) {
108 for (off = 1, x = 1; x < w - 1; ++x, off++)
109 highlight(data + off,
110 data + off + (h-1) * w,
111 a->surface.relief==RR_RELIEF_RAISED);
112 for (off = 0, x = 0; x < h; ++x, off++)
113 highlight(data + off * w,
114 data + off * w + w - 1,
115 a->surface.relief==RR_RELIEF_RAISED);
116 }
117
118 if (a->surface.bevel == RR_BEVEL_2) {
119 for (off = 2, x = 2; x < w - 2; ++x, off++)
120 highlight(data + off + w,
121 data + off + (h-2) * w,
122 a->surface.relief==RR_RELIEF_RAISED);
123 for (off = 1, x = 1; x < h-1; ++x, off++)
124 highlight(data + off * w + 1,
125 data + off * w + w - 2,
126 a->surface.relief==RR_RELIEF_RAISED);
127 }
128 }
129 }
130
131 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised)
132 {
133 gint r, g, b;
134
135 RrPixel32 *up, *down;
136 if (raised) {
137 up = x;
138 down = y;
139 } else {
140 up = y;
141 down = x;
142 }
143 r = (*up >> RrDefaultRedOffset) & 0xFF;
144 r += r >> 1;
145 g = (*up >> RrDefaultGreenOffset) & 0xFF;
146 g += g >> 1;
147 b = (*up >> RrDefaultBlueOffset) & 0xFF;
148 b += b >> 1;
149 if (r > 0xFF) r = 0xFF;
150 if (g > 0xFF) g = 0xFF;
151 if (b > 0xFF) b = 0xFF;
152 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
153 + (b << RrDefaultBlueOffset);
154
155 r = (*down >> RrDefaultRedOffset) & 0xFF;
156 r = (r >> 1) + (r >> 2);
157 g = (*down >> RrDefaultGreenOffset) & 0xFF;
158 g = (g >> 1) + (g >> 2);
159 b = (*down >> RrDefaultBlueOffset) & 0xFF;
160 b = (b >> 1) + (b >> 2);
161 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
162 + (b << RrDefaultBlueOffset);
163 }
164
165 static void create_bevel_colors(RrAppearance *l)
166 {
167 gint r, g, b;
168
169 /* light color */
170 r = l->surface.primary->r;
171 r += r >> 1;
172 g = l->surface.primary->g;
173 g += g >> 1;
174 b = l->surface.primary->b;
175 b += b >> 1;
176 if (r > 0xFF) r = 0xFF;
177 if (g > 0xFF) g = 0xFF;
178 if (b > 0xFF) b = 0xFF;
179 g_assert(!l->surface.bevel_light);
180 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
181
182 /* dark color */
183 r = l->surface.primary->r;
184 r = (r >> 1) + (r >> 2);
185 g = l->surface.primary->g;
186 g = (g >> 1) + (g >> 2);
187 b = l->surface.primary->b;
188 b = (b >> 1) + (b >> 2);
189 g_assert(!l->surface.bevel_dark);
190 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
191 }
192
193 static void gradient_solid(RrAppearance *l, gint w, gint h)
194 {
195 gint i;
196 RrPixel32 pix;
197 RrPixel32 *data = l->surface.pixel_data;
198 RrSurface *sp = &l->surface;
199 gint left = 0, top = 0, right = w - 1, bottom = h - 1;
200
201 pix = (sp->primary->r << RrDefaultRedOffset)
202 + (sp->primary->g << RrDefaultGreenOffset)
203 + (sp->primary->b << RrDefaultBlueOffset);
204
205 for (i = 0; i < w * h; i++)
206 *data++ = pix;
207
208 if (sp->interlaced)
209 return;
210
211 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
212 0, 0, w, h);
213
214 switch (sp->relief) {
215 case RR_RELIEF_RAISED:
216 if (!sp->bevel_dark)
217 create_bevel_colors(l);
218
219 switch (sp->bevel) {
220 case RR_BEVEL_1:
221 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
222 left, bottom, right, bottom);
223 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
224 right, bottom, right, top);
225
226 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
227 left, top, right, top);
228 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
229 left, bottom, left, top);
230 break;
231 case RR_BEVEL_2:
232 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
233 left + 2, bottom - 1, right - 2, bottom - 1);
234 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
235 right - 1, bottom - 1, right - 1, top + 1);
236
237 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
238 left + 2, top + 1, right - 2, top + 1);
239 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
240 left + 1, bottom - 1, left + 1, top + 1);
241 break;
242 default:
243 g_assert_not_reached(); /* unhandled BevelType */
244 }
245 break;
246 case RR_RELIEF_SUNKEN:
247 if (!sp->bevel_dark)
248 create_bevel_colors(l);
249
250 switch (sp->bevel) {
251 case RR_BEVEL_1:
252 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
253 left, bottom, right, bottom);
254 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
255 right, bottom, right, top);
256
257 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
258 left, top, right, top);
259 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
260 left, bottom, left, top);
261 break;
262 case RR_BEVEL_2:
263 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
264 left + 2, bottom - 1, right - 2, bottom - 1);
265 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
266 right - 1, bottom - 1, right - 1, top + 1);
267
268 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
269 left + 2, top + 1, right - 2, top + 1);
270 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
271 left + 1, bottom - 1, left + 1, top + 1);
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_osx(RrSurface *sf, gint w, gint h)
453 {
454 gint x, y;
455 RrPixel32 *data = sf->pixel_data, *datav;
456 RrPixel32 current;
457
458 VARS(x);
459 SETUP(x, sf->primary, sf->secondary, w/2);
460
461 for (x = w - 1; x > w/2-1; --x) { /* 0 -> w-1 */
462 current = COLOR(x);
463 datav = data;
464 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
465 *datav = current;
466 datav += w;
467 }
468 ++data;
469
470 NEXT(x);
471 }
472 SETUP(x, sf->secondary, sf->primary, w/2);
473 for (x = w/2 - 1; x > 0; --x) { /* 0 -> w-1 */
474 current = COLOR(x);
475 datav = data;
476 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
477 *datav = current;
478 datav += w;
479 }
480 ++data;
481
482 NEXT(x);
483 }
484 current = COLOR(x);
485 for (y = h - 1; y >= 0; --y) /* 0 -> h */
486 *(data + y * w) = current;
487 }
488
489 static void gradient_vertical(RrSurface *sf, gint w, gint h)
490 {
491 gint x, y;
492 RrPixel32 *data = sf->pixel_data;
493 RrPixel32 current;
494
495 VARS(y);
496 SETUP(y, sf->primary, sf->secondary, h);
497
498 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
499 current = COLOR(y);
500 for (x = w - 1; x >= 0; --x) /* 0 -> w */
501 *(data++) = current;
502
503 NEXT(y);
504 }
505 current = COLOR(y);
506 for (x = w - 1; x >= 0; --x) /* 0 -> w */
507 *(data++) = current;
508 }
509
510
511 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
512 {
513 gint x, y;
514 RrPixel32 *data = sf->pixel_data;
515 RrColor left, right;
516 RrColor extracorner;
517
518 VARS(lefty);
519 VARS(righty);
520 VARS(x);
521
522 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
523 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
524 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
525
526 SETUP(lefty, sf->primary, (&extracorner), h);
527 SETUP(righty, (&extracorner), sf->secondary, h);
528
529 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
530 COLOR_RR(lefty, (&left));
531 COLOR_RR(righty, (&right));
532
533 SETUP(x, (&left), (&right), w);
534
535 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
536 *(data++) = COLOR(x);
537
538 NEXT(x);
539 }
540 *(data++) = COLOR(x);
541
542 NEXT(lefty);
543 NEXT(righty);
544 }
545 COLOR_RR(lefty, (&left));
546 COLOR_RR(righty, (&right));
547
548 SETUP(x, (&left), (&right), w);
549
550 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
551 *(data++) = COLOR(x);
552
553 NEXT(x);
554 }
555 *data = COLOR(x);
556 }
557
558 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
559 {
560 gint x, y;
561 RrPixel32 *data = sf->pixel_data;
562 RrColor left, right;
563 RrColor extracorner;
564
565 VARS(lefty);
566 VARS(righty);
567 VARS(x);
568
569 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
570 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
571 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
572
573 SETUP(lefty, (&extracorner), sf->secondary, h);
574 SETUP(righty, sf->primary, (&extracorner), h);
575
576 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
577 COLOR_RR(lefty, (&left));
578 COLOR_RR(righty, (&right));
579
580 SETUP(x, (&left), (&right), w);
581
582 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
583 *(data++) = COLOR(x);
584
585 NEXT(x);
586 }
587 *(data++) = COLOR(x);
588
589 NEXT(lefty);
590 NEXT(righty);
591 }
592 COLOR_RR(lefty, (&left));
593 COLOR_RR(righty, (&right));
594
595 SETUP(x, (&left), (&right), w);
596
597 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
598 *(data++) = COLOR(x);
599
600 NEXT(x);
601 }
602 *data = COLOR(x);
603 }
604
605 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
606 {
607 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
608 RrPixel32 *data = sf->pixel_data;
609 RrPixel32 *end = data + inw*inh - 1;
610 RrPixel32 current;
611 RrColor left, right;
612 RrColor extracorner;
613
614 VARS(lefty);
615 VARS(righty);
616 VARS(x);
617
618 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
619 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
620 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
621
622 SETUP(lefty, (&extracorner), sf->secondary, h);
623 SETUP(righty, sf->primary, (&extracorner), h);
624
625 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
626 COLOR_RR(lefty, (&left));
627 COLOR_RR(righty, (&right));
628
629 SETUP(x, (&left), (&right), w);
630
631 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
632 current = COLOR(x);
633 *(data+x) = current;
634 *(data+inw-x) = current;
635 *(end-x) = current;
636 *(end-(inw-x)) = current;
637
638 NEXT(x);
639 }
640 current = COLOR(x);
641 *(data+x) = current;
642 *(data+inw-x) = current;
643 *(end-x) = current;
644 *(end-(inw-x)) = current;
645
646 data+=inw;
647 end-=inw;
648
649 NEXT(lefty);
650 NEXT(righty);
651 }
652 COLOR_RR(lefty, (&left));
653 COLOR_RR(righty, (&right));
654
655 SETUP(x, (&left), (&right), w);
656
657 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
658 current = COLOR(x);
659 *(data+x) = current;
660 *(data+inw-x) = current;
661 *(end-x) = current;
662 *(end-(inw-x)) = current;
663
664 NEXT(x);
665 }
666 current = COLOR(x);
667 *(data+x) = current;
668 *(data+inw-x) = current;
669 *(end-x) = current;
670 *(end-(inw-x)) = current;
671 }
672
This page took 0.062764 seconds and 3 git commands to generate.