]> Dogcows Code - chaz/openbox/blob - render/gradient.c
okay that parent didnt point where i thought
[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
424 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
425 {
426 gint x, y;
427 RrPixel32 *data = sf->pixel_data, *datav;
428 RrPixel32 current;
429
430 VARS(x);
431 SETUP(x, sf->primary, sf->secondary, w);
432
433 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
434 current = COLOR(x);
435 datav = data;
436 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
437 *datav = current;
438 datav += w;
439 }
440 ++data;
441
442 NEXT(x);
443 }
444 current = COLOR(x);
445 for (y = h - 1; y >= 0; --y) /* 0 -> h */
446 *(data + y * w) = current;
447 }
448
449 static void gradient_vertical(RrSurface *sf, gint w, gint h)
450 {
451 gint x, y;
452 RrPixel32 *data = sf->pixel_data;
453 RrPixel32 current;
454
455 VARS(y);
456 SETUP(y, sf->primary, sf->secondary, h);
457
458 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
459 current = COLOR(y);
460 for (x = w - 1; x >= 0; --x) /* 0 -> w */
461 *(data++) = current;
462
463 NEXT(y);
464 }
465 current = COLOR(y);
466 for (x = w - 1; x >= 0; --x) /* 0 -> w */
467 *(data++) = current;
468 }
469
470
471 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
472 {
473 gint x, y;
474 RrPixel32 *data = sf->pixel_data;
475 RrColor left, right;
476 RrColor extracorner;
477
478 VARS(lefty);
479 VARS(righty);
480 VARS(x);
481
482 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
483 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
484 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
485
486 SETUP(lefty, sf->primary, (&extracorner), h);
487 SETUP(righty, (&extracorner), sf->secondary, h);
488
489 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
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 NEXT(lefty);
503 NEXT(righty);
504 }
505 COLOR_RR(lefty, (&left));
506 COLOR_RR(righty, (&right));
507
508 SETUP(x, (&left), (&right), w);
509
510 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
511 *(data++) = COLOR(x);
512
513 NEXT(x);
514 }
515 *data = COLOR(x);
516 }
517
518 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
519 {
520 gint x, y;
521 RrPixel32 *data = sf->pixel_data;
522 RrColor left, right;
523 RrColor extracorner;
524
525 VARS(lefty);
526 VARS(righty);
527 VARS(x);
528
529 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
530 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
531 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
532
533 SETUP(lefty, (&extracorner), sf->secondary, h);
534 SETUP(righty, sf->primary, (&extracorner), h);
535
536 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
537 COLOR_RR(lefty, (&left));
538 COLOR_RR(righty, (&right));
539
540 SETUP(x, (&left), (&right), w);
541
542 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
543 *(data++) = COLOR(x);
544
545 NEXT(x);
546 }
547 *(data++) = COLOR(x);
548
549 NEXT(lefty);
550 NEXT(righty);
551 }
552 COLOR_RR(lefty, (&left));
553 COLOR_RR(righty, (&right));
554
555 SETUP(x, (&left), (&right), w);
556
557 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
558 *(data++) = COLOR(x);
559
560 NEXT(x);
561 }
562 *data = COLOR(x);
563 }
564
565 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
566 {
567 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
568 RrPixel32 *data = sf->pixel_data;
569 RrPixel32 *end = data + inw*inh - 1;
570 RrPixel32 current;
571 RrColor left, right;
572 RrColor extracorner;
573
574 VARS(lefty);
575 VARS(righty);
576 VARS(x);
577
578 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
579 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
580 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
581
582 SETUP(lefty, (&extracorner), sf->secondary, h);
583 SETUP(righty, sf->primary, (&extracorner), h);
584
585 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
586 COLOR_RR(lefty, (&left));
587 COLOR_RR(righty, (&right));
588
589 SETUP(x, (&left), (&right), w);
590
591 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
592 current = COLOR(x);
593 *(data+x) = current;
594 *(data+inw-x) = current;
595 *(end-x) = current;
596 *(end-(inw-x)) = current;
597
598 NEXT(x);
599 }
600 current = COLOR(x);
601 *(data+x) = current;
602 *(data+inw-x) = current;
603 *(end-x) = current;
604 *(end-(inw-x)) = current;
605
606 data+=inw;
607 end-=inw;
608
609 NEXT(lefty);
610 NEXT(righty);
611 }
612 COLOR_RR(lefty, (&left));
613 COLOR_RR(righty, (&right));
614
615 SETUP(x, (&left), (&right), w);
616
617 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
618 current = COLOR(x);
619 *(data+x) = current;
620 *(data+inw-x) = current;
621 *(end-x) = current;
622 *(end-(inw-x)) = current;
623
624 NEXT(x);
625 }
626 current = COLOR(x);
627 *(data+x) = current;
628 *(data+inw-x) = current;
629 *(end-x) = current;
630 *(end-(inw-x)) = current;
631 }
632
This page took 0.06457 seconds and 5 git commands to generate.