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_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
);
34 void RrRender(RrAppearance
*a
, gint w
, gint h
)
36 RrPixel32
*data
= a
->surface
.pixel_data
;
41 switch (a
->surface
.grad
) {
42 case RR_SURFACE_SOLID
:
43 gradient_solid(a
, w
, h
);
45 case RR_SURFACE_SPLIT
:
46 gradient_split(a
, w
, h
);
48 case RR_SURFACE_VERTICAL
:
49 gradient_vertical(&a
->surface
, w
, h
);
51 case RR_SURFACE_HORIZONTAL
:
52 gradient_horizontal(&a
->surface
, w
, h
);
54 case RR_SURFACE_DIAGONAL
:
55 gradient_diagonal(&a
->surface
, w
, h
);
57 case RR_SURFACE_CROSS_DIAGONAL
:
58 gradient_crossdiagonal(&a
->surface
, w
, h
);
60 case RR_SURFACE_PYRAMID
:
61 gradient_pyramid(&a
->surface
, w
, h
);
64 g_assert_not_reached(); /* unhandled gradient */
68 if (a
->surface
.interlaced
) {
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
);
79 for (i
= 0; i
< h
; i
+= 2, p
+= w
)
80 for (x
= 0; x
< w
; ++x
, ++p
)
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
;
95 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
96 *(data
+ (off
* w
)) = current
;
97 *(data
+ (off
* w
) + w
- 1) = current
;
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
);
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
);
126 static void highlight(RrPixel32
*x
, RrPixel32
*y
, gboolean raised
)
130 RrPixel32
*up
, *down
;
138 r
= (*up
>> RrDefaultRedOffset
) & 0xFF;
140 g
= (*up
>> RrDefaultGreenOffset
) & 0xFF;
142 b
= (*up
>> RrDefaultBlueOffset
) & 0xFF;
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
);
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
);
160 static void create_bevel_colors(RrAppearance
*l
)
165 r
= l
->surface
.primary
->r
;
167 g
= l
->surface
.primary
->g
;
169 b
= l
->surface
.primary
->b
;
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
);
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
);
188 static void gradient_solid(RrAppearance
*l
, gint w
, gint h
)
192 RrSurface
*sp
= &l
->surface
;
193 gint left
= 0, top
= 0, right
= w
- 1, bottom
= h
- 1;
195 pix
= (sp
->primary
->r
<< RrDefaultRedOffset
)
196 + (sp
->primary
->g
<< RrDefaultGreenOffset
)
197 + (sp
->primary
->b
<< RrDefaultBlueOffset
);
199 for (a
= 0; a
< w
; a
++)
200 for (b
= 0; b
< h
; b
++)
201 sp
->pixel_data
[a
+ b
* w
] = pix
;
203 XFillRectangle(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->primary
),
206 if (sp
->interlaced
) {
207 for (i
= 0; i
< h
; i
+= 2)
208 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,
209 RrColorGC(sp
->interlace_color
),
213 switch (sp
->relief
) {
214 case RR_RELIEF_RAISED
:
216 create_bevel_colors(l
);
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
);
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
);
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);
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);
242 g_assert_not_reached(); /* unhandled BevelType */
245 case RR_RELIEF_SUNKEN
:
247 create_bevel_colors(l
);
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
);
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
);
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);
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);
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_split(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_vertical(RrSurface
*sf
, gint w
, gint h
)
455 RrPixel32
*data
= sf
->pixel_data
;
459 SETUP(y
, sf
->primary
, sf
->secondary
, h
);
461 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
463 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
469 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
474 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
)
477 RrPixel32
*data
= sf
->pixel_data
;
485 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
486 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
487 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
489 SETUP(lefty
, sf
->primary
, (&extracorner
), h
);
490 SETUP(righty
, (&extracorner
), sf
->secondary
, h
);
492 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
493 COLOR_RR(lefty
, (&left
));
494 COLOR_RR(righty
, (&right
));
496 SETUP(x
, (&left
), (&right
), w
);
498 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
499 *(data
++) = COLOR(x
);
503 *(data
++) = COLOR(x
);
508 COLOR_RR(lefty
, (&left
));
509 COLOR_RR(righty
, (&right
));
511 SETUP(x
, (&left
), (&right
), w
);
513 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
514 *(data
++) = COLOR(x
);
521 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
)
524 RrPixel32
*data
= sf
->pixel_data
;
532 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
533 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
534 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
536 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
537 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
539 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
540 COLOR_RR(lefty
, (&left
));
541 COLOR_RR(righty
, (&right
));
543 SETUP(x
, (&left
), (&right
), w
);
545 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
546 *(data
++) = COLOR(x
);
550 *(data
++) = COLOR(x
);
555 COLOR_RR(lefty
, (&left
));
556 COLOR_RR(righty
, (&right
));
558 SETUP(x
, (&left
), (&right
), w
);
560 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
561 *(data
++) = COLOR(x
);
568 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
)
570 gint x
, y
, w
= (inw
>> 1) + 1, h
= (inh
>> 1) + 1;
571 RrPixel32
*data
= sf
->pixel_data
;
572 RrPixel32
*end
= data
+ inw
*inh
- 1;
581 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
582 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
583 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
585 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
586 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
588 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
589 COLOR_RR(lefty
, (&left
));
590 COLOR_RR(righty
, (&right
));
592 SETUP(x
, (&left
), (&right
), w
);
594 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
597 *(data
+inw
-x
) = current
;
599 *(end
-(inw
-x
)) = current
;
605 *(data
+inw
-x
) = current
;
607 *(end
-(inw
-x
)) = current
;
615 COLOR_RR(lefty
, (&left
));
616 COLOR_RR(righty
, (&right
));
618 SETUP(x
, (&left
), (&right
), w
);
620 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
623 *(data
+inw
-x
) = current
;
625 *(end
-(inw
-x
)) = current
;
631 *(data
+inw
-x
) = current
;
633 *(end
-(inw
-x
)) = current
;