1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 gradient.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6 Copyright (c) 2003 Derek Foreman
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.
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.
18 See the COPYING file for a copy of the GNU General Public License.
26 static void highlight(RrSurface *s, RrPixel32 *x, RrPixel32 *y,
28 static void gradient_parentrelative(RrAppearance *a, gint w, gint h);
29 static void gradient_solid(RrAppearance *l, gint w, gint h);
30 static void gradient_splitvertical(RrAppearance *a, gint w, gint h);
31 static void gradient_vertical(RrSurface *sf, gint w, gint h);
32 static void gradient_horizontal(RrSurface *sf, gint w, gint h);
33 static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h);
34 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
35 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
36 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
38 void RrRender(RrAppearance *a, gint w, gint h)
40 RrPixel32 *data = a->surface.pixel_data;
45 switch (a->surface.grad) {
46 case RR_SURFACE_PARENTREL:
47 gradient_parentrelative(a, w, h);
49 case RR_SURFACE_SOLID:
50 gradient_solid(a, w, h);
52 case RR_SURFACE_SPLIT_VERTICAL:
53 gradient_splitvertical(a, w, h);
55 case RR_SURFACE_VERTICAL:
56 gradient_vertical(&a->surface, w, h);
58 case RR_SURFACE_HORIZONTAL:
59 gradient_horizontal(&a->surface, w, h);
61 case RR_SURFACE_MIRROR_HORIZONTAL:
62 gradient_mirrorhorizontal(&a->surface, w, h);
64 case RR_SURFACE_DIAGONAL:
65 gradient_diagonal(&a->surface, w, h);
67 case RR_SURFACE_CROSS_DIAGONAL:
68 gradient_crossdiagonal(&a->surface, w, h);
70 case RR_SURFACE_PYRAMID:
71 gradient_pyramid(&a->surface, w, h);
74 g_assert_not_reached(); /* unhandled gradient */
78 if (a->surface.interlaced) {
82 r = a->surface.interlace_color->r;
83 g = a->surface.interlace_color->g;
84 b = a->surface.interlace_color->b;
85 current = (r << RrDefaultRedOffset)
86 + (g << RrDefaultGreenOffset)
87 + (b << RrDefaultBlueOffset);
89 for (i = 0; i < h; i += 2, p += w)
90 for (x = 0; x < w; ++x, ++p)
94 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
95 r = a->surface.border_color->r;
96 g = a->surface.border_color->g;
97 b = a->surface.border_color->b;
98 current = (r << RrDefaultRedOffset)
99 + (g << RrDefaultGreenOffset)
100 + (b << RrDefaultBlueOffset);
101 for (off = 0, x = 0; x < w; ++x, off++) {
102 *(data + off) = current;
103 *(data + off + ((h-1) * w)) = current;
105 for (off = 0, x = 0; x < h; ++x, off++) {
106 *(data + (off * w)) = current;
107 *(data + (off * w) + w - 1) = current;
111 if (a->surface.relief != RR_RELIEF_FLAT) {
112 if (a->surface.bevel == RR_BEVEL_1) {
113 for (off = 1, x = 1; x < w - 1; ++x, off++)
114 highlight(&a->surface, data + off,
115 data + off + (h-1) * w,
116 a->surface.relief==RR_RELIEF_RAISED);
117 for (off = 0, x = 0; x < h; ++x, off++)
118 highlight(&a->surface, data + off * w,
119 data + off * w + w - 1,
120 a->surface.relief==RR_RELIEF_RAISED);
123 if (a->surface.bevel == RR_BEVEL_2) {
124 for (off = 2, x = 2; x < w - 2; ++x, off++)
125 highlight(&a->surface, data + off + w,
126 data + off + (h-2) * w,
127 a->surface.relief==RR_RELIEF_RAISED);
128 for (off = 1, x = 1; x < h-1; ++x, off++)
129 highlight(&a->surface, data + off * w + 1,
130 data + off * w + w - 2,
131 a->surface.relief==RR_RELIEF_RAISED);
136 static void highlight(RrSurface *s, RrPixel32 *x, RrPixel32 *y, gboolean raised)
140 RrPixel32 *up, *down;
149 r = (*up >> RrDefaultRedOffset) & 0xFF;
150 r += (r * s->bevel_light_adjust) >> 8;
151 g = (*up >> RrDefaultGreenOffset) & 0xFF;
152 g += (g * s->bevel_light_adjust) >> 8;
153 b = (*up >> RrDefaultBlueOffset) & 0xFF;
154 b += (b * s->bevel_light_adjust) >> 8;
155 if (r > 0xFF) r = 0xFF;
156 if (g > 0xFF) g = 0xFF;
157 if (b > 0xFF) b = 0xFF;
158 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
159 + (b << RrDefaultBlueOffset);
161 r = (*down >> RrDefaultRedOffset) & 0xFF;
162 r -= (r * s->bevel_dark_adjust) >> 8;
163 g = (*down >> RrDefaultGreenOffset) & 0xFF;
164 g -= (g * s->bevel_dark_adjust) >> 8;
165 b = (*down >> RrDefaultBlueOffset) & 0xFF;
166 b -= (b * s->bevel_dark_adjust) >> 8;
167 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
168 + (b << RrDefaultBlueOffset);
171 static void create_bevel_colors(RrAppearance *l)
176 r = l->surface.primary->r;
177 r += (r * l->surface.bevel_light_adjust) >> 8;
178 g = l->surface.primary->g;
179 g += (g * l->surface.bevel_light_adjust) >> 8;
180 b = l->surface.primary->b;
181 b += (b * l->surface.bevel_light_adjust) >> 8;
182 if (r > 0xFF) r = 0xFF;
183 if (g > 0xFF) g = 0xFF;
184 if (b > 0xFF) b = 0xFF;
185 g_assert(!l->surface.bevel_light);
186 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
189 r = l->surface.primary->r;
190 r -= (r * l->surface.bevel_dark_adjust) >> 8;
191 g = l->surface.primary->g;
192 g -= (g * l->surface.bevel_dark_adjust) >> 8;
193 b = l->surface.primary->b;
194 b -= (b * l->surface.bevel_dark_adjust) >> 8;
195 g_assert(!l->surface.bevel_dark);
196 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
199 static void gradient_parentrelative(RrAppearance *a, gint w, gint h)
201 RrPixel32 *source, *dest;
202 gint sw, sh, partial_w, partial_h, i;
204 g_assert (a->surface.parent);
205 g_assert (a->surface.parent->w);
207 sw = a->surface.parent->w;
208 sh = a->surface.parent->h;
210 /* This is a little hack. When a texture is parentrelative, and the same
211 area as the parent, and has a bevel, it will draw its bevel on top
212 of the parent's, amplifying it. So instead, rerender the child with
213 the parent's settings, but the child's bevel and interlace */
214 if (a->surface.relief != RR_RELIEF_FLAT &&
215 (a->surface.parent->surface.relief != RR_RELIEF_FLAT ||
216 a->surface.parent->surface.border) &&
217 !a->surface.parentx && !a->surface.parenty &&
220 RrSurface old = a->surface;
221 a->surface = a->surface.parent->surface;
223 /* turn these off for the parent */
224 a->surface.relief = RR_RELIEF_FLAT;
225 a->surface.border = FALSE;
227 a->surface.pixel_data = old.pixel_data;
232 source = (a->surface.parent->surface.pixel_data +
233 a->surface.parentx + sw * a->surface.parenty);
234 dest = a->surface.pixel_data;
236 if (a->surface.parentx + w > sw) {
237 partial_w = sw - a->surface.parentx;
238 } else partial_w = w;
240 if (a->surface.parenty + h > sh) {
241 partial_h = sh - a->surface.parenty;
242 } else partial_h = h;
244 for (i = 0; i < partial_h; i++, source += sw, dest += w) {
245 memcpy(dest, source, partial_w * sizeof(RrPixel32));
250 static void gradient_solid(RrAppearance *l, gint w, gint h)
254 RrPixel32 *data = l->surface.pixel_data;
255 RrSurface *sp = &l->surface;
256 gint left = 0, top = 0, right = w - 1, bottom = h - 1;
258 pix = (sp->primary->r << RrDefaultRedOffset)
259 + (sp->primary->g << RrDefaultGreenOffset)
260 + (sp->primary->b << RrDefaultBlueOffset);
262 for (i = 0; i < w * h; i++)
268 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
271 switch (sp->relief) {
272 case RR_RELIEF_RAISED:
274 create_bevel_colors(l);
278 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
279 left, bottom, right, bottom);
280 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
281 right, bottom, right, top);
283 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
284 left, top, right, top);
285 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
286 left, bottom, left, top);
289 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
290 left + 2, bottom - 1, right - 2, bottom - 1);
291 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
292 right - 1, bottom - 1, right - 1, top + 1);
294 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
295 left + 2, top + 1, right - 2, top + 1);
296 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
297 left + 1, bottom - 1, left + 1, top + 1);
300 g_assert_not_reached(); /* unhandled BevelType */
303 case RR_RELIEF_SUNKEN:
305 create_bevel_colors(l);
309 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
310 left, bottom, right, bottom);
311 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
312 right, bottom, right, top);
314 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
315 left, top, right, top);
316 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
317 left, bottom, left, top);
320 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
321 left + 2, bottom - 1, right - 2, bottom - 1);
322 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
323 right - 1, bottom - 1, right - 1, top + 1);
325 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
326 left + 2, top + 1, right - 2, top + 1);
327 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
328 left + 1, bottom - 1, left + 1, top + 1);
331 g_assert_not_reached(); /* unhandled BevelType */
336 XDrawRectangle(RrDisplay(l->inst), l->pixmap,
337 RrColorGC(sp->border_color),
338 left, top, right, bottom);
342 g_assert_not_reached(); /* unhandled ReliefType */
346 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
350 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
351 gboolean bigslope##x[3] /* color slope > 1 */
353 #define SETUP(x, from, to, w) \
356 color##x[0] = from->r; \
357 color##x[1] = from->g; \
358 color##x[2] = from->b; \
360 cdelta##x[0] = to->r - from->r; \
361 cdelta##x[1] = to->g - from->g; \
362 cdelta##x[2] = to->b - from->b; \
364 if (cdelta##x[0] < 0) { \
365 cdelta##x[0] = -cdelta##x[0]; \
369 if (cdelta##x[1] < 0) { \
370 cdelta##x[1] = -cdelta##x[1]; \
374 if (cdelta##x[2] < 0) { \
375 cdelta##x[2] = -cdelta##x[2]; \
379 bigslope##x[0] = cdelta##x[0] > w;\
380 bigslope##x[1] = cdelta##x[1] > w;\
381 bigslope##x[2] = cdelta##x[2] > w
383 #define COLOR_RR(x, c) \
384 c->r = color##x[0]; \
385 c->g = color##x[1]; \
389 ((color##x[0] << RrDefaultRedOffset) + \
390 (color##x[1] << RrDefaultGreenOffset) + \
391 (color##x[2] << RrDefaultBlueOffset))
393 #define INCREMENT(x, i) \
399 for (i = 2; i >= 0; --i) { \
400 if (!cdelta##x[i]) continue; \
402 if (!bigslope##x[i]) { \
403 /* Y (color) is dependant on X */ \
404 error##x[i] += cdelta##x[i]; \
405 if ((error##x[i] << 1) >= len##x) { \
406 color##x[i] += INCREMENT(x, i); \
407 error##x[i] -= len##x; \
410 /* X is dependant on Y (color) */ \
412 color##x[i] += INCREMENT(x, i); \
413 error##x[i] += len##x; \
414 if ((error##x[i] << 1) >= cdelta##x[i]) { \
415 error##x[i] -= cdelta##x[i]; \
423 static void gradient_splitvertical(RrAppearance *a, gint w, gint h)
426 RrSurface *sf = &a->surface;
427 RrPixel32 *data = sf->pixel_data;
429 gint y1sz, y2sz, y3sz;
435 /* if h <= 5, then a 0 or 1px middle gradient.
436 if h > 5, then always a 1px middle gradient.
440 y2sz = (h < 3 ? 0 : h % 2);
444 y1sz = h/2 - (1 - (h % 2));
449 SETUP(y1, sf->split_primary, sf->primary, y1sz);
451 /* setup to get the colors _in between_ these other 2 */
452 SETUP(y2, sf->primary, sf->secondary, y2sz + 2);
453 NEXT(y2); /* skip the first one, its the same as the last of y1 */
455 SETUP(y3, sf->secondary, sf->split_secondary, y3sz);
457 for (y1 = y1sz; y1 > 0; --y1) {
459 for (x = w - 1; x >= 0; --x)
465 for (y2 = y2sz; y2 > 0; --y2) {
467 for (x = w - 1; x >= 0; --x)
473 for (y3 = y3sz; y3 > 0; --y3) {
475 for (x = w - 1; x >= 0; --x)
482 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
485 RrPixel32 *data = sf->pixel_data, *datav;
489 SETUP(x, sf->primary, sf->secondary, w);
491 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
494 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
503 for (y = h - 1; y >= 0; --y) /* 0 -> h */
504 *(data + y * w) = current;
507 static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h)
510 RrPixel32 *data = sf->pixel_data, *datav;
514 SETUP(x, sf->primary, sf->secondary, w/2);
517 for (x = w - 1; x > w/2-1; --x) { /* 0 -> w-1 */
520 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
528 SETUP(x, sf->secondary, sf->primary, w/2);
529 for (x = w/2 - 1; x > 0; --x) { /* 0 -> w-1 */
532 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
542 for (y = h - 1; y >= 0; --y) /* 0 -> h */
543 *(data + y * w) = current;
546 static void gradient_vertical(RrSurface *sf, gint w, gint h)
549 RrPixel32 *data = sf->pixel_data;
553 SETUP(y, sf->primary, sf->secondary, h);
555 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
557 for (x = w - 1; x >= 0; --x) /* 0 -> w */
563 for (x = w - 1; x >= 0; --x) /* 0 -> w */
568 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
571 RrPixel32 *data = sf->pixel_data;
579 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
580 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
581 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
583 SETUP(lefty, sf->primary, (&extracorner), h);
584 SETUP(righty, (&extracorner), sf->secondary, h);
586 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
587 COLOR_RR(lefty, (&left));
588 COLOR_RR(righty, (&right));
590 SETUP(x, (&left), (&right), w);
592 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
593 *(data++) = COLOR(x);
597 *(data++) = COLOR(x);
602 COLOR_RR(lefty, (&left));
603 COLOR_RR(righty, (&right));
605 SETUP(x, (&left), (&right), w);
607 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
608 *(data++) = COLOR(x);
615 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
618 RrPixel32 *data = sf->pixel_data;
626 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
627 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
628 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
630 SETUP(lefty, (&extracorner), sf->secondary, h);
631 SETUP(righty, sf->primary, (&extracorner), h);
633 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
634 COLOR_RR(lefty, (&left));
635 COLOR_RR(righty, (&right));
637 SETUP(x, (&left), (&right), w);
639 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
640 *(data++) = COLOR(x);
644 *(data++) = COLOR(x);
649 COLOR_RR(lefty, (&left));
650 COLOR_RR(righty, (&right));
652 SETUP(x, (&left), (&right), w);
654 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
655 *(data++) = COLOR(x);
662 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
664 gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
665 RrPixel32 *data = sf->pixel_data;
666 RrPixel32 *end = data + inw*inh - 1;
675 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
676 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
677 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
679 SETUP(lefty, (&extracorner), sf->secondary, h);
680 SETUP(righty, sf->primary, (&extracorner), h);
682 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
683 COLOR_RR(lefty, (&left));
684 COLOR_RR(righty, (&right));
686 SETUP(x, (&left), (&right), w);
688 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
691 *(data+inw-x) = current;
693 *(end-(inw-x)) = current;
699 *(data+inw-x) = current;
701 *(end-(inw-x)) = current;
709 COLOR_RR(lefty, (&left));
710 COLOR_RR(righty, (&right));
712 SETUP(x, (&left), (&right), w);
714 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
717 *(data+inw-x) = current;
719 *(end-(inw-x)) = current;
725 *(data+inw-x) = current;
727 *(end-(inw-x)) = current;