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 Ben 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(RrPixel32
*x
, RrPixel32
*y
, gboolean raised
);
27 static void gradient_solid(RrAppearance
*l
, gint w
, gint h
);
28 static void gradient_splitvertical(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_mirrorhorizontal(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
);
36 void RrRender(RrAppearance
*a
, gint w
, gint h
)
38 RrPixel32
*data
= a
->surface
.pixel_data
;
43 switch (a
->surface
.grad
) {
44 case RR_SURFACE_SOLID
:
45 gradient_solid(a
, w
, h
);
47 case RR_SURFACE_SPLIT_VERTICAL
:
48 gradient_splitvertical(a
, w
, h
);
50 case RR_SURFACE_VERTICAL
:
51 gradient_vertical(&a
->surface
, w
, h
);
53 case RR_SURFACE_HORIZONTAL
:
54 gradient_horizontal(&a
->surface
, w
, h
);
56 case RR_SURFACE_MIRROR_HORIZONTAL
:
57 gradient_mirrorhorizontal(&a
->surface
, w
, h
);
59 case RR_SURFACE_DIAGONAL
:
60 gradient_diagonal(&a
->surface
, w
, h
);
62 case RR_SURFACE_CROSS_DIAGONAL
:
63 gradient_crossdiagonal(&a
->surface
, w
, h
);
65 case RR_SURFACE_PYRAMID
:
66 gradient_pyramid(&a
->surface
, w
, h
);
69 g_assert_not_reached(); /* unhandled gradient */
73 if (a
->surface
.interlaced
) {
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
);
84 for (i
= 0; i
< h
; i
+= 2, p
+= w
)
85 for (x
= 0; x
< w
; ++x
, ++p
)
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
;
100 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
101 *(data
+ (off
* w
)) = current
;
102 *(data
+ (off
* w
) + w
- 1) = current
;
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
);
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
);
131 static void highlight(RrPixel32
*x
, RrPixel32
*y
, gboolean raised
)
135 RrPixel32
*up
, *down
;
143 r
= (*up
>> RrDefaultRedOffset
) & 0xFF;
145 g
= (*up
>> RrDefaultGreenOffset
) & 0xFF;
147 b
= (*up
>> RrDefaultBlueOffset
) & 0xFF;
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
);
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
);
165 static void create_bevel_colors(RrAppearance
*l
)
170 r
= l
->surface
.primary
->r
;
172 g
= l
->surface
.primary
->g
;
174 b
= l
->surface
.primary
->b
;
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
);
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
);
193 static void gradient_solid(RrAppearance
*l
, gint w
, gint h
)
197 RrPixel32
*data
= l
->surface
.pixel_data
;
198 RrSurface
*sp
= &l
->surface
;
199 gint left
= 0, top
= 0, right
= w
- 1, bottom
= h
- 1;
201 pix
= (sp
->primary
->r
<< RrDefaultRedOffset
)
202 + (sp
->primary
->g
<< RrDefaultGreenOffset
)
203 + (sp
->primary
->b
<< RrDefaultBlueOffset
);
205 for (i
= 0; i
< w
* h
; i
++)
211 XFillRectangle(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->primary
),
214 switch (sp
->relief
) {
215 case RR_RELIEF_RAISED
:
217 create_bevel_colors(l
);
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
);
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
);
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);
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);
243 g_assert_not_reached(); /* unhandled BevelType */
246 case RR_RELIEF_SUNKEN
:
248 create_bevel_colors(l
);
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
);
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
);
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);
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);
274 g_assert_not_reached(); /* unhandled BevelType */
279 XDrawRectangle(RrDisplay(l
->inst
), l
->pixmap
,
280 RrColorGC(sp
->border_color
),
281 left
, top
, right
, bottom
);
285 g_assert_not_reached(); /* unhandled ReliefType */
289 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
293 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
294 gboolean bigslope##x[3] /* color slope > 1 */
296 #define SETUP(x, from, to, w) \
299 color##x[0] = from->r; \
300 color##x[1] = from->g; \
301 color##x[2] = from->b; \
303 cdelta##x[0] = to->r - from->r; \
304 cdelta##x[1] = to->g - from->g; \
305 cdelta##x[2] = to->b - from->b; \
307 if (cdelta##x[0] < 0) { \
308 cdelta##x[0] = -cdelta##x[0]; \
312 if (cdelta##x[1] < 0) { \
313 cdelta##x[1] = -cdelta##x[1]; \
317 if (cdelta##x[2] < 0) { \
318 cdelta##x[2] = -cdelta##x[2]; \
322 bigslope##x[0] = cdelta##x[0] > w;\
323 bigslope##x[1] = cdelta##x[1] > w;\
324 bigslope##x[2] = cdelta##x[2] > w
326 #define COLOR_RR(x, c) \
327 c->r = color##x[0]; \
328 c->g = color##x[1]; \
332 ((color##x[0] << RrDefaultRedOffset) + \
333 (color##x[1] << RrDefaultGreenOffset) + \
334 (color##x[2] << RrDefaultBlueOffset))
336 #define INCREMENT(x, i) \
342 for (i = 2; i >= 0; --i) { \
343 if (!cdelta##x[i]) continue; \
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; \
353 /* X is dependant on Y (color) */ \
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]; \
366 static void gradient_splitvertical(RrAppearance
*a
, gint w
, gint h
)
368 gint x
, y1
, y3
, r
, g
, b
;
369 RrSurface
*sf
= &a
->surface
;
370 RrPixel32
*data
= sf
->pixel_data
;
372 RrColor
*primary_light
, *secondary_light
;
383 if (r
> 0xFF) r
= 0xFF;
384 if (g
> 0xFF) g
= 0xFF;
385 if (b
> 0xFF) b
= 0xFF;
386 primary_light
= RrColorNew(a
->inst
, r
, g
, b
);
388 r
= sf
->secondary
->r
;
390 g
= sf
->secondary
->g
;
392 b
= sf
->secondary
->b
;
394 if (r
> 0xFF) r
= 0xFF;
395 if (g
> 0xFF) g
= 0xFF;
396 if (b
> 0xFF) b
= 0xFF;
397 secondary_light
= RrColorNew(a
->inst
, r
, g
, b
);
399 SETUP(y1
, primary_light
, sf
->primary
, (h
/ 2) -1);
400 SETUP(y3
, sf
->secondary
, secondary_light
, (h
/ 2) -1);
402 for (y1
= h
- 1; y1
> (h
/ 2) -1; --y1
) { /* 0 -> h-1 */
404 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
411 for (y3
= (h
/ 2) - 1; y3
> 0; --y3
) {
413 for (x
= w
- 1; x
>= 0; --x
)
420 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
423 RrColorFree(primary_light
);
424 RrColorFree(secondary_light
);
427 static void gradient_horizontal(RrSurface
*sf
, gint w
, gint h
)
430 RrPixel32
*data
= sf
->pixel_data
, *datav
;
434 SETUP(x
, sf
->primary
, sf
->secondary
, w
);
436 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
439 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
448 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
449 *(data
+ y
* w
) = current
;
452 static void gradient_mirrorhorizontal(RrSurface
*sf
, gint w
, gint h
)
455 RrPixel32
*data
= sf
->pixel_data
, *datav
;
459 SETUP(x
, sf
->primary
, sf
->secondary
, w
/2);
462 for (x
= w
- 1; x
> w
/2-1; --x
) { /* 0 -> w-1 */
465 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
473 SETUP(x
, sf
->secondary
, sf
->primary
, w
/2);
474 for (x
= w
/2 - 1; x
> 0; --x
) { /* 0 -> w-1 */
477 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
487 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
488 *(data
+ y
* w
) = current
;
491 static void gradient_vertical(RrSurface
*sf
, gint w
, gint h
)
494 RrPixel32
*data
= sf
->pixel_data
;
498 SETUP(y
, sf
->primary
, sf
->secondary
, h
);
500 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
502 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
508 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
513 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
)
516 RrPixel32
*data
= sf
->pixel_data
;
524 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
525 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
526 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
528 SETUP(lefty
, sf
->primary
, (&extracorner
), h
);
529 SETUP(righty
, (&extracorner
), sf
->secondary
, h
);
531 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
532 COLOR_RR(lefty
, (&left
));
533 COLOR_RR(righty
, (&right
));
535 SETUP(x
, (&left
), (&right
), w
);
537 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
538 *(data
++) = COLOR(x
);
542 *(data
++) = COLOR(x
);
547 COLOR_RR(lefty
, (&left
));
548 COLOR_RR(righty
, (&right
));
550 SETUP(x
, (&left
), (&right
), w
);
552 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
553 *(data
++) = COLOR(x
);
560 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
)
563 RrPixel32
*data
= sf
->pixel_data
;
571 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
572 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
573 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
575 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
576 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
578 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
579 COLOR_RR(lefty
, (&left
));
580 COLOR_RR(righty
, (&right
));
582 SETUP(x
, (&left
), (&right
), w
);
584 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
585 *(data
++) = COLOR(x
);
589 *(data
++) = COLOR(x
);
594 COLOR_RR(lefty
, (&left
));
595 COLOR_RR(righty
, (&right
));
597 SETUP(x
, (&left
), (&right
), w
);
599 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
600 *(data
++) = COLOR(x
);
607 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
)
609 gint x
, y
, w
= (inw
>> 1) + 1, h
= (inh
>> 1) + 1;
610 RrPixel32
*data
= sf
->pixel_data
;
611 RrPixel32
*end
= data
+ inw
*inh
- 1;
620 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
621 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
622 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
624 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
625 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
627 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
628 COLOR_RR(lefty
, (&left
));
629 COLOR_RR(righty
, (&right
));
631 SETUP(x
, (&left
), (&right
), w
);
633 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
636 *(data
+inw
-x
) = current
;
638 *(end
-(inw
-x
)) = current
;
644 *(data
+inw
-x
) = current
;
646 *(end
-(inw
-x
)) = current
;
654 COLOR_RR(lefty
, (&left
));
655 COLOR_RR(righty
, (&right
));
657 SETUP(x
, (&left
), (&right
), w
);
659 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
662 *(data
+inw
-x
) = current
;
664 *(end
-(inw
-x
)) = current
;
670 *(data
+inw
-x
) = current
;
672 *(end
-(inw
-x
)) = current
;