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_vertical(RrSurface
*sf
, gint w
, gint h
);
28 static void gradient_horizontal(RrSurface
*sf
, gint w
, gint h
);
29 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
);
30 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
);
31 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
);
33 void RrRender(RrAppearance
*a
, gint w
, gint h
)
35 RrPixel32
*data
= a
->surface
.pixel_data
;
40 switch (a
->surface
.grad
) {
41 case RR_SURFACE_SOLID
:
42 gradient_solid(a
, w
, h
);
44 case RR_SURFACE_VERTICAL
:
45 gradient_vertical(&a
->surface
, w
, h
);
47 case RR_SURFACE_HORIZONTAL
:
48 gradient_horizontal(&a
->surface
, w
, h
);
50 case RR_SURFACE_DIAGONAL
:
51 gradient_diagonal(&a
->surface
, w
, h
);
53 case RR_SURFACE_CROSS_DIAGONAL
:
54 gradient_crossdiagonal(&a
->surface
, w
, h
);
56 case RR_SURFACE_PYRAMID
:
57 gradient_pyramid(&a
->surface
, w
, h
);
60 g_assert_not_reached(); /* unhandled gradient */
64 if (a
->surface
.interlaced
) {
68 r
= a
->surface
.interlace_color
->r
;
69 g
= a
->surface
.interlace_color
->g
;
70 b
= a
->surface
.interlace_color
->b
;
71 current
= (r
<< RrDefaultRedOffset
)
72 + (g
<< RrDefaultGreenOffset
)
73 + (b
<< RrDefaultBlueOffset
);
75 for (i
= 0; i
< h
; i
+= 2, p
+= w
)
76 for (x
= 0; x
< w
; ++x
, ++p
)
80 if (a
->surface
.relief
== RR_RELIEF_FLAT
&& a
->surface
.border
) {
81 r
= a
->surface
.border_color
->r
;
82 g
= a
->surface
.border_color
->g
;
83 b
= a
->surface
.border_color
->b
;
84 current
= (r
<< RrDefaultRedOffset
)
85 + (g
<< RrDefaultGreenOffset
)
86 + (b
<< RrDefaultBlueOffset
);
87 for (off
= 0, x
= 0; x
< w
; ++x
, off
++) {
88 *(data
+ off
) = current
;
89 *(data
+ off
+ ((h
-1) * w
)) = current
;
91 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
92 *(data
+ (off
* w
)) = current
;
93 *(data
+ (off
* w
) + w
- 1) = current
;
97 if (a
->surface
.relief
!= RR_RELIEF_FLAT
) {
98 if (a
->surface
.bevel
== RR_BEVEL_1
) {
99 for (off
= 1, x
= 1; x
< w
- 1; ++x
, off
++)
100 highlight(data
+ off
,
101 data
+ off
+ (h
-1) * w
,
102 a
->surface
.relief
==RR_RELIEF_RAISED
);
103 for (off
= 0, x
= 0; x
< h
; ++x
, off
++)
104 highlight(data
+ off
* w
,
105 data
+ off
* w
+ w
- 1,
106 a
->surface
.relief
==RR_RELIEF_RAISED
);
109 if (a
->surface
.bevel
== RR_BEVEL_2
) {
110 for (off
= 2, x
= 2; x
< w
- 2; ++x
, off
++)
111 highlight(data
+ off
+ w
,
112 data
+ off
+ (h
-2) * w
,
113 a
->surface
.relief
==RR_RELIEF_RAISED
);
114 for (off
= 1, x
= 1; x
< h
-1; ++x
, off
++)
115 highlight(data
+ off
* w
+ 1,
116 data
+ off
* w
+ w
- 2,
117 a
->surface
.relief
==RR_RELIEF_RAISED
);
122 static void highlight(RrPixel32
*x
, RrPixel32
*y
, gboolean raised
)
126 RrPixel32
*up
, *down
;
134 r
= (*up
>> RrDefaultRedOffset
) & 0xFF;
136 g
= (*up
>> RrDefaultGreenOffset
) & 0xFF;
138 b
= (*up
>> RrDefaultBlueOffset
) & 0xFF;
140 if (r
> 0xFF) r
= 0xFF;
141 if (g
> 0xFF) g
= 0xFF;
142 if (b
> 0xFF) b
= 0xFF;
143 *up
= (r
<< RrDefaultRedOffset
) + (g
<< RrDefaultGreenOffset
)
144 + (b
<< RrDefaultBlueOffset
);
146 r
= (*down
>> RrDefaultRedOffset
) & 0xFF;
147 r
= (r
>> 1) + (r
>> 2);
148 g
= (*down
>> RrDefaultGreenOffset
) & 0xFF;
149 g
= (g
>> 1) + (g
>> 2);
150 b
= (*down
>> RrDefaultBlueOffset
) & 0xFF;
151 b
= (b
>> 1) + (b
>> 2);
152 *down
= (r
<< RrDefaultRedOffset
) + (g
<< RrDefaultGreenOffset
)
153 + (b
<< RrDefaultBlueOffset
);
156 static void create_bevel_colors(RrAppearance
*l
)
161 r
= l
->surface
.primary
->r
;
163 g
= l
->surface
.primary
->g
;
165 b
= l
->surface
.primary
->b
;
167 if (r
> 0xFF) r
= 0xFF;
168 if (g
> 0xFF) g
= 0xFF;
169 if (b
> 0xFF) b
= 0xFF;
170 g_assert(!l
->surface
.bevel_light
);
171 l
->surface
.bevel_light
= RrColorNew(l
->inst
, r
, g
, b
);
174 r
= l
->surface
.primary
->r
;
175 r
= (r
>> 1) + (r
>> 2);
176 g
= l
->surface
.primary
->g
;
177 g
= (g
>> 1) + (g
>> 2);
178 b
= l
->surface
.primary
->b
;
179 b
= (b
>> 1) + (b
>> 2);
180 g_assert(!l
->surface
.bevel_dark
);
181 l
->surface
.bevel_dark
= RrColorNew(l
->inst
, r
, g
, b
);
184 static void gradient_solid(RrAppearance
*l
, gint w
, gint h
)
188 RrSurface
*sp
= &l
->surface
;
189 gint left
= 0, top
= 0, right
= w
- 1, bottom
= h
- 1;
191 pix
= (sp
->primary
->r
<< RrDefaultRedOffset
)
192 + (sp
->primary
->g
<< RrDefaultGreenOffset
)
193 + (sp
->primary
->b
<< RrDefaultBlueOffset
);
195 for (a
= 0; a
< w
; a
++)
196 for (b
= 0; b
< h
; b
++)
197 sp
->pixel_data
[a
+ b
* w
] = pix
;
199 XFillRectangle(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->primary
),
202 if (sp
->interlaced
) {
203 for (i
= 0; i
< h
; i
+= 2)
204 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,
205 RrColorGC(sp
->interlace_color
),
209 switch (sp
->relief
) {
210 case RR_RELIEF_RAISED
:
212 create_bevel_colors(l
);
216 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
217 left
, bottom
, right
, bottom
);
218 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
219 right
, bottom
, right
, top
);
221 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
222 left
, top
, right
, top
);
223 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
224 left
, bottom
, left
, top
);
227 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
228 left
+ 1, bottom
- 2, right
- 2, bottom
- 2);
229 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
230 right
- 2, bottom
- 2, right
- 2, top
+ 1);
232 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
233 left
+ 1, top
+ 1, right
- 2, top
+ 1);
234 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
235 left
+ 1, bottom
- 2, left
+ 1, top
+ 1);
238 g_assert_not_reached(); /* unhandled BevelType */
241 case RR_RELIEF_SUNKEN
:
243 create_bevel_colors(l
);
247 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
248 left
, bottom
, right
, bottom
);
249 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
250 right
, bottom
, right
, top
);
252 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
253 left
, top
, right
, top
);
254 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
255 left
, bottom
, left
, top
);
258 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
259 left
+ 1, bottom
- 2, right
- 2, bottom
- 2);
260 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
261 right
- 2, bottom
- 2, right
- 2, top
+ 1);
263 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
264 left
+ 1, top
+ 1, right
- 2, top
+ 1);
265 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
266 left
+ 1, bottom
- 2, left
+ 1, top
+ 1);
270 g_assert_not_reached(); /* unhandled BevelType */
275 XDrawRectangle(RrDisplay(l
->inst
), l
->pixmap
,
276 RrColorGC(sp
->border_color
),
277 left
, top
, right
, bottom
);
281 g_assert_not_reached(); /* unhandled ReliefType */
285 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
289 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
290 gboolean bigslope##x[3] /* color slope > 1 */
292 #define SETUP(x, from, to, w) \
295 color##x[0] = from->r; \
296 color##x[1] = from->g; \
297 color##x[2] = from->b; \
299 cdelta##x[0] = to->r - from->r; \
300 cdelta##x[1] = to->g - from->g; \
301 cdelta##x[2] = to->b - from->b; \
303 if (cdelta##x[0] < 0) { \
304 cdelta##x[0] = -cdelta##x[0]; \
308 if (cdelta##x[1] < 0) { \
309 cdelta##x[1] = -cdelta##x[1]; \
313 if (cdelta##x[2] < 0) { \
314 cdelta##x[2] = -cdelta##x[2]; \
318 bigslope##x[0] = cdelta##x[0] > w;\
319 bigslope##x[1] = cdelta##x[1] > w;\
320 bigslope##x[2] = cdelta##x[2] > w
322 #define COLOR_RR(x, c) \
323 c->r = color##x[0]; \
324 c->g = color##x[1]; \
328 ((color##x[0] << RrDefaultRedOffset) + \
329 (color##x[1] << RrDefaultGreenOffset) + \
330 (color##x[2] << RrDefaultBlueOffset))
332 #define INCREMENT(x, i) \
338 for (i = 2; i >= 0; --i) { \
339 if (!cdelta##x[i]) continue; \
341 if (!bigslope##x[i]) { \
342 /* Y (color) is dependant on X */ \
343 error##x[i] += cdelta##x[i]; \
344 if ((error##x[i] << 1) >= len##x) { \
345 color##x[i] += INCREMENT(x, i); \
346 error##x[i] -= len##x; \
349 /* X is dependant on Y (color) */ \
351 color##x[i] += INCREMENT(x, i); \
352 error##x[i] += len##x; \
353 if ((error##x[i] << 1) >= cdelta##x[i]) { \
354 error##x[i] -= cdelta##x[i]; \
362 static void gradient_horizontal(RrSurface
*sf
, gint w
, gint h
)
365 RrPixel32
*data
= sf
->pixel_data
, *datav
;
369 SETUP(x
, sf
->primary
, sf
->secondary
, w
);
371 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
374 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
383 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
384 *(data
+ y
* w
) = current
;
387 static void gradient_vertical(RrSurface
*sf
, gint w
, gint h
)
390 RrPixel32
*data
= sf
->pixel_data
;
394 SETUP(y
, sf
->primary
, sf
->secondary
, h
);
396 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
398 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
404 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
409 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
)
412 RrPixel32
*data
= sf
->pixel_data
;
420 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
421 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
422 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
424 SETUP(lefty
, sf
->primary
, (&extracorner
), h
);
425 SETUP(righty
, (&extracorner
), sf
->secondary
, h
);
427 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
428 COLOR_RR(lefty
, (&left
));
429 COLOR_RR(righty
, (&right
));
431 SETUP(x
, (&left
), (&right
), w
);
433 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
434 *(data
++) = COLOR(x
);
438 *(data
++) = COLOR(x
);
443 COLOR_RR(lefty
, (&left
));
444 COLOR_RR(righty
, (&right
));
446 SETUP(x
, (&left
), (&right
), w
);
448 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
449 *(data
++) = COLOR(x
);
456 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
)
459 RrPixel32
*data
= sf
->pixel_data
;
467 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
468 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
469 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
471 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
472 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
474 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
475 COLOR_RR(lefty
, (&left
));
476 COLOR_RR(righty
, (&right
));
478 SETUP(x
, (&left
), (&right
), w
);
480 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
481 *(data
++) = COLOR(x
);
485 *(data
++) = COLOR(x
);
490 COLOR_RR(lefty
, (&left
));
491 COLOR_RR(righty
, (&right
));
493 SETUP(x
, (&left
), (&right
), w
);
495 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
496 *(data
++) = COLOR(x
);
503 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
)
505 gint x
, y
, w
= (inw
>> 1) + 1, h
= (inh
>> 1) + 1;
506 RrPixel32
*data
= sf
->pixel_data
;
507 RrPixel32
*end
= data
+ inw
*inh
- 1;
516 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
517 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
518 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
520 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
521 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
523 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
524 COLOR_RR(lefty
, (&left
));
525 COLOR_RR(righty
, (&right
));
527 SETUP(x
, (&left
), (&right
), w
);
529 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
532 *(data
+inw
-x
) = current
;
534 *(end
-(inw
-x
)) = current
;
540 *(data
+inw
-x
) = current
;
542 *(end
-(inw
-x
)) = current
;
550 COLOR_RR(lefty
, (&left
));
551 COLOR_RR(righty
, (&right
));
553 SETUP(x
, (&left
), (&right
), w
);
555 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
558 *(data
+inw
-x
) = current
;
560 *(end
-(inw
-x
)) = current
;
566 *(data
+inw
-x
) = current
;
568 *(end
-(inw
-x
)) = current
;