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
;
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
);
385 r
= sf
->secondary
->r
;
387 g
= sf
->secondary
->g
;
389 b
= sf
->secondary
->b
;
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
);
397 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);
461 for (x
= w
- 1; x
> w
/2-1; --x
) { /* 0 -> w-1 */
464 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
472 SETUP(x
, sf
->secondary
, sf
->primary
, w
/2);
473 for (x
= w
/2 - 1; x
> 0; --x
) { /* 0 -> w-1 */
476 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
485 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
486 *(data
+ y
* w
) = current
;
489 static void gradient_vertical(RrSurface
*sf
, gint w
, gint h
)
492 RrPixel32
*data
= sf
->pixel_data
;
496 SETUP(y
, sf
->primary
, sf
->secondary
, h
);
498 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
500 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
506 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
511 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
)
514 RrPixel32
*data
= sf
->pixel_data
;
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;
526 SETUP(lefty
, sf
->primary
, (&extracorner
), h
);
527 SETUP(righty
, (&extracorner
), sf
->secondary
, h
);
529 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
530 COLOR_RR(lefty
, (&left
));
531 COLOR_RR(righty
, (&right
));
533 SETUP(x
, (&left
), (&right
), w
);
535 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
536 *(data
++) = COLOR(x
);
540 *(data
++) = COLOR(x
);
545 COLOR_RR(lefty
, (&left
));
546 COLOR_RR(righty
, (&right
));
548 SETUP(x
, (&left
), (&right
), w
);
550 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
551 *(data
++) = COLOR(x
);
558 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
)
561 RrPixel32
*data
= sf
->pixel_data
;
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;
573 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
574 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
576 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
577 COLOR_RR(lefty
, (&left
));
578 COLOR_RR(righty
, (&right
));
580 SETUP(x
, (&left
), (&right
), w
);
582 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
583 *(data
++) = COLOR(x
);
587 *(data
++) = COLOR(x
);
592 COLOR_RR(lefty
, (&left
));
593 COLOR_RR(righty
, (&right
));
595 SETUP(x
, (&left
), (&right
), w
);
597 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
598 *(data
++) = COLOR(x
);
605 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
)
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;
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;
622 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
623 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
625 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
626 COLOR_RR(lefty
, (&left
));
627 COLOR_RR(righty
, (&right
));
629 SETUP(x
, (&left
), (&right
), w
);
631 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
634 *(data
+inw
-x
) = current
;
636 *(end
-(inw
-x
)) = current
;
642 *(data
+inw
-x
) = current
;
644 *(end
-(inw
-x
)) = current
;
652 COLOR_RR(lefty
, (&left
));
653 COLOR_RR(righty
, (&right
));
655 SETUP(x
, (&left
), (&right
), w
);
657 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
660 *(data
+inw
-x
) = current
;
662 *(end
-(inw
-x
)) = current
;
668 *(data
+inw
-x
) = current
;
670 *(end
-(inw
-x
)) = current
;