1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 gradient.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
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_osx(RrSurface *sf, gint w, gint h);
31 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
32 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
33 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
35 void RrRender(RrAppearance *a, gint w, gint h)
37 RrPixel32 *data = a->surface.pixel_data;
42 switch (a->surface.grad) {
43 case RR_SURFACE_SOLID:
44 gradient_solid(a, w, h);
46 case RR_SURFACE_SPLIT:
47 gradient_split(a, w, h);
49 case RR_SURFACE_VERTICAL:
50 gradient_vertical(&a->surface, w, h);
52 case RR_SURFACE_HORIZONTAL:
53 gradient_horizontal(&a->surface, w, h);
56 gradient_osx(&a->surface, w, h);
58 case RR_SURFACE_DIAGONAL:
59 gradient_diagonal(&a->surface, w, h);
61 case RR_SURFACE_CROSS_DIAGONAL:
62 gradient_crossdiagonal(&a->surface, w, h);
64 case RR_SURFACE_PYRAMID:
65 gradient_pyramid(&a->surface, w, h);
68 g_assert_not_reached(); /* unhandled gradient */
72 if (a->surface.interlaced) {
76 r = a->surface.interlace_color->r;
77 g = a->surface.interlace_color->g;
78 b = a->surface.interlace_color->b;
79 current = (r << RrDefaultRedOffset)
80 + (g << RrDefaultGreenOffset)
81 + (b << RrDefaultBlueOffset);
83 for (i = 0; i < h; i += 2, p += w)
84 for (x = 0; x < w; ++x, ++p)
88 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
89 r = a->surface.border_color->r;
90 g = a->surface.border_color->g;
91 b = a->surface.border_color->b;
92 current = (r << RrDefaultRedOffset)
93 + (g << RrDefaultGreenOffset)
94 + (b << RrDefaultBlueOffset);
95 for (off = 0, x = 0; x < w; ++x, off++) {
96 *(data + off) = current;
97 *(data + off + ((h-1) * w)) = current;
99 for (off = 0, x = 0; x < h; ++x, off++) {
100 *(data + (off * w)) = current;
101 *(data + (off * w) + w - 1) = current;
105 if (a->surface.relief != RR_RELIEF_FLAT) {
106 if (a->surface.bevel == RR_BEVEL_1) {
107 for (off = 1, x = 1; x < w - 1; ++x, off++)
108 highlight(data + off,
109 data + off + (h-1) * w,
110 a->surface.relief==RR_RELIEF_RAISED);
111 for (off = 0, x = 0; x < h; ++x, off++)
112 highlight(data + off * w,
113 data + off * w + w - 1,
114 a->surface.relief==RR_RELIEF_RAISED);
117 if (a->surface.bevel == RR_BEVEL_2) {
118 for (off = 2, x = 2; x < w - 2; ++x, off++)
119 highlight(data + off + w,
120 data + off + (h-2) * w,
121 a->surface.relief==RR_RELIEF_RAISED);
122 for (off = 1, x = 1; x < h-1; ++x, off++)
123 highlight(data + off * w + 1,
124 data + off * w + w - 2,
125 a->surface.relief==RR_RELIEF_RAISED);
130 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised)
134 RrPixel32 *up, *down;
142 r = (*up >> RrDefaultRedOffset) & 0xFF;
144 g = (*up >> RrDefaultGreenOffset) & 0xFF;
146 b = (*up >> RrDefaultBlueOffset) & 0xFF;
148 if (r > 0xFF) r = 0xFF;
149 if (g > 0xFF) g = 0xFF;
150 if (b > 0xFF) b = 0xFF;
151 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
152 + (b << RrDefaultBlueOffset);
154 r = (*down >> RrDefaultRedOffset) & 0xFF;
155 r = (r >> 1) + (r >> 2);
156 g = (*down >> RrDefaultGreenOffset) & 0xFF;
157 g = (g >> 1) + (g >> 2);
158 b = (*down >> RrDefaultBlueOffset) & 0xFF;
159 b = (b >> 1) + (b >> 2);
160 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
161 + (b << RrDefaultBlueOffset);
164 static void create_bevel_colors(RrAppearance *l)
169 r = l->surface.primary->r;
171 g = l->surface.primary->g;
173 b = l->surface.primary->b;
175 if (r > 0xFF) r = 0xFF;
176 if (g > 0xFF) g = 0xFF;
177 if (b > 0xFF) b = 0xFF;
178 g_assert(!l->surface.bevel_light);
179 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
182 r = l->surface.primary->r;
183 r = (r >> 1) + (r >> 2);
184 g = l->surface.primary->g;
185 g = (g >> 1) + (g >> 2);
186 b = l->surface.primary->b;
187 b = (b >> 1) + (b >> 2);
188 g_assert(!l->surface.bevel_dark);
189 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
192 static void gradient_solid(RrAppearance *l, gint w, gint h)
196 RrSurface *sp = &l->surface;
197 gint left = 0, top = 0, right = w - 1, bottom = h - 1;
199 pix = (sp->primary->r << RrDefaultRedOffset)
200 + (sp->primary->g << RrDefaultGreenOffset)
201 + (sp->primary->b << RrDefaultBlueOffset);
203 for (a = 0; a < w; a++)
204 for (b = 0; b < h; b++)
205 sp->pixel_data[a + b * w] = pix;
207 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
210 if (sp->interlaced) {
211 for (i = 0; i < h; i += 2)
212 XDrawLine(RrDisplay(l->inst), l->pixmap,
213 RrColorGC(sp->interlace_color),
217 switch (sp->relief) {
218 case RR_RELIEF_RAISED:
220 create_bevel_colors(l);
224 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
225 left, bottom, right, bottom);
226 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
227 right, bottom, right, top);
229 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
230 left, top, right, top);
231 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
232 left, bottom, left, top);
235 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
236 left + 1, bottom - 2, right - 2, bottom - 2);
237 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
238 right - 2, bottom - 2, right - 2, top + 1);
240 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
241 left + 1, top + 1, right - 2, top + 1);
242 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
243 left + 1, bottom - 2, left + 1, top + 1);
246 g_assert_not_reached(); /* unhandled BevelType */
249 case RR_RELIEF_SUNKEN:
251 create_bevel_colors(l);
255 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
256 left, bottom, right, bottom);
257 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
258 right, bottom, right, top);
260 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
261 left, top, right, top);
262 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
263 left, bottom, left, top);
266 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
267 left + 1, bottom - 2, right - 2, bottom - 2);
268 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
269 right - 2, bottom - 2, right - 2, top + 1);
271 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
272 left + 1, top + 1, right - 2, top + 1);
273 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
274 left + 1, bottom - 2, left + 1, top + 1);
278 g_assert_not_reached(); /* unhandled BevelType */
283 XDrawRectangle(RrDisplay(l->inst), l->pixmap,
284 RrColorGC(sp->border_color),
285 left, top, right, bottom);
289 g_assert_not_reached(); /* unhandled ReliefType */
293 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
297 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
298 gboolean bigslope##x[3] /* color slope > 1 */
300 #define SETUP(x, from, to, w) \
303 color##x[0] = from->r; \
304 color##x[1] = from->g; \
305 color##x[2] = from->b; \
307 cdelta##x[0] = to->r - from->r; \
308 cdelta##x[1] = to->g - from->g; \
309 cdelta##x[2] = to->b - from->b; \
311 if (cdelta##x[0] < 0) { \
312 cdelta##x[0] = -cdelta##x[0]; \
316 if (cdelta##x[1] < 0) { \
317 cdelta##x[1] = -cdelta##x[1]; \
321 if (cdelta##x[2] < 0) { \
322 cdelta##x[2] = -cdelta##x[2]; \
326 bigslope##x[0] = cdelta##x[0] > w;\
327 bigslope##x[1] = cdelta##x[1] > w;\
328 bigslope##x[2] = cdelta##x[2] > w
330 #define COLOR_RR(x, c) \
331 c->r = color##x[0]; \
332 c->g = color##x[1]; \
336 ((color##x[0] << RrDefaultRedOffset) + \
337 (color##x[1] << RrDefaultGreenOffset) + \
338 (color##x[2] << RrDefaultBlueOffset))
340 #define INCREMENT(x, i) \
346 for (i = 2; i >= 0; --i) { \
347 if (!cdelta##x[i]) continue; \
349 if (!bigslope##x[i]) { \
350 /* Y (color) is dependant on X */ \
351 error##x[i] += cdelta##x[i]; \
352 if ((error##x[i] << 1) >= len##x) { \
353 color##x[i] += INCREMENT(x, i); \
354 error##x[i] -= len##x; \
357 /* X is dependant on Y (color) */ \
359 color##x[i] += INCREMENT(x, i); \
360 error##x[i] += len##x; \
361 if ((error##x[i] << 1) >= cdelta##x[i]) { \
362 error##x[i] -= cdelta##x[i]; \
370 static void gradient_split(RrAppearance *a, gint w, gint h)
372 gint x, y1, y3, r, g, b;
373 RrSurface *sf = &a->surface;
374 RrPixel32 *data = sf->pixel_data;
376 RrColor *primary_light, *secondary_light;
384 if (r > 0xFF) r = 0xFF;
385 if (g > 0xFF) g = 0xFF;
386 if (b > 0xFF) b = 0xFF;
387 primary_light = RrColorNew(a->inst, r, g, b);
389 r = sf->secondary->r;
391 g = sf->secondary->g;
393 b = sf->secondary->b;
395 if (r > 0xFF) r = 0xFF;
396 if (g > 0xFF) g = 0xFF;
397 if (b > 0xFF) b = 0xFF;
398 secondary_light = RrColorNew(a->inst, r, g, b);
401 SETUP(y1, primary_light, sf->primary, (h / 2) -1);
404 SETUP(y3, sf->secondary, secondary_light, (h / 2) -1);
406 for (y1 = h - 1; y1 > (h / 2) -1; --y1) { /* 0 -> h-1 */
408 for (x = w - 1; x >= 0; --x) /* 0 -> w */
415 for (y3 = (h / 2) - 1; y3 > 0; --y3) {
417 for (x = w - 1; x >= 0; --x)
424 for (x = w - 1; x >= 0; --x) /* 0 -> w */
427 RrColorFree(primary_light);
428 RrColorFree(secondary_light);
431 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
434 RrPixel32 *data = sf->pixel_data, *datav;
438 SETUP(x, sf->primary, sf->secondary, w);
440 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
443 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
452 for (y = h - 1; y >= 0; --y) /* 0 -> h */
453 *(data + y * w) = current;
456 static void gradient_osx(RrSurface *sf, gint w, gint h)
459 RrPixel32 *data = sf->pixel_data, *datav;
463 SETUP(x, sf->primary, sf->secondary, w/2);
465 for (x = w - 1; x > w/2-1; --x) { /* 0 -> w-1 */
468 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
476 SETUP(x, sf->secondary, sf->primary, w/2);
477 for (x = w/2 - 1; x > 0; --x) { /* 0 -> w-1 */
480 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
489 for (y = h - 1; y >= 0; --y) /* 0 -> h */
490 *(data + y * w) = current;
493 static void gradient_vertical(RrSurface *sf, gint w, gint h)
496 RrPixel32 *data = sf->pixel_data;
500 SETUP(y, sf->primary, sf->secondary, h);
502 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
504 for (x = w - 1; x >= 0; --x) /* 0 -> w */
510 for (x = w - 1; x >= 0; --x) /* 0 -> w */
515 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
518 RrPixel32 *data = sf->pixel_data;
526 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
527 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
528 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
530 SETUP(lefty, sf->primary, (&extracorner), h);
531 SETUP(righty, (&extracorner), sf->secondary, h);
533 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
534 COLOR_RR(lefty, (&left));
535 COLOR_RR(righty, (&right));
537 SETUP(x, (&left), (&right), w);
539 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
540 *(data++) = COLOR(x);
544 *(data++) = COLOR(x);
549 COLOR_RR(lefty, (&left));
550 COLOR_RR(righty, (&right));
552 SETUP(x, (&left), (&right), w);
554 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
555 *(data++) = COLOR(x);
562 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
565 RrPixel32 *data = sf->pixel_data;
573 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
574 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
575 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
577 SETUP(lefty, (&extracorner), sf->secondary, h);
578 SETUP(righty, sf->primary, (&extracorner), h);
580 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
581 COLOR_RR(lefty, (&left));
582 COLOR_RR(righty, (&right));
584 SETUP(x, (&left), (&right), w);
586 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
587 *(data++) = COLOR(x);
591 *(data++) = COLOR(x);
596 COLOR_RR(lefty, (&left));
597 COLOR_RR(righty, (&right));
599 SETUP(x, (&left), (&right), w);
601 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
602 *(data++) = COLOR(x);
609 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
611 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
612 RrPixel32 *data = sf->pixel_data;
613 RrPixel32 *end = data + inw*inh - 1;
622 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
623 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
624 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
626 SETUP(lefty, (&extracorner), sf->secondary, h);
627 SETUP(righty, sf->primary, (&extracorner), h);
629 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
630 COLOR_RR(lefty, (&left));
631 COLOR_RR(righty, (&right));
633 SETUP(x, (&left), (&right), w);
635 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
638 *(data+inw-x) = current;
640 *(end-(inw-x)) = current;
646 *(data+inw-x) = current;
648 *(end-(inw-x)) = current;
656 COLOR_RR(lefty, (&left));
657 COLOR_RR(righty, (&right));
659 SETUP(x, (&left), (&right), w);
661 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
664 *(data+inw-x) = current;
666 *(end-(inw-x)) = current;
672 *(data+inw-x) = current;
674 *(end-(inw-x)) = current;