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_osx(RrSurface
*sf
, gint w
, gint h
);
31 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
);
32 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
);
33 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
);
35 void RrRender(RrAppearance
*a
, gint w
, gint h
)
37 RrPixel32
*data
= a
->surface
.pixel_data
;
42 switch (a
->surface
.grad
) {
43 case RR_SURFACE_SOLID
:
44 gradient_solid(a
, w
, h
);
46 case RR_SURFACE_SPLIT
:
47 gradient_split(a
, w
, h
);
49 case RR_SURFACE_VERTICAL
:
50 gradient_vertical(&a
->surface
, w
, h
);
52 case RR_SURFACE_HORIZONTAL
:
53 gradient_horizontal(&a
->surface
, w
, h
);
56 gradient_osx(&a
->surface
, w
, h
);
58 case RR_SURFACE_DIAGONAL
:
59 gradient_diagonal(&a
->surface
, w
, h
);
61 case RR_SURFACE_CROSS_DIAGONAL
:
62 gradient_crossdiagonal(&a
->surface
, w
, h
);
64 case RR_SURFACE_PYRAMID
:
65 gradient_pyramid(&a
->surface
, w
, h
);
68 g_assert_not_reached(); /* unhandled gradient */
72 if (a
->surface
.interlaced
) {
76 r
= a
->surface
.interlace_color
->r
;
77 g
= a
->surface
.interlace_color
->g
;
78 b
= a
->surface
.interlace_color
->b
;
79 current
= (r
<< RrDefaultRedOffset
)
80 + (g
<< RrDefaultGreenOffset
)
81 + (b
<< RrDefaultBlueOffset
);
83 for (i
= 0; i
< h
; i
+= 2, p
+= w
)
84 for (x
= 0; x
< w
; ++x
, ++p
)
88 if (a
->surface
.relief
== RR_RELIEF_FLAT
&& a
->surface
.border
) {
89 r
= a
->surface
.border_color
->r
;
90 g
= a
->surface
.border_color
->g
;
91 b
= a
->surface
.border_color
->b
;
92 current
= (r
<< RrDefaultRedOffset
)
93 + (g
<< RrDefaultGreenOffset
)
94 + (b
<< RrDefaultBlueOffset
);
95 for (off
= 0, x
= 0; x
< w
; ++x
, off
++) {
96 *(data
+ off
) = current
;
97 *(data
+ off
+ ((h
-1) * w
)) = current
;
99 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
100 *(data
+ (off
* w
)) = current
;
101 *(data
+ (off
* w
) + w
- 1) = current
;
105 if (a
->surface
.relief
!= RR_RELIEF_FLAT
) {
106 if (a
->surface
.bevel
== RR_BEVEL_1
) {
107 for (off
= 1, x
= 1; x
< w
- 1; ++x
, off
++)
108 highlight(data
+ off
,
109 data
+ off
+ (h
-1) * w
,
110 a
->surface
.relief
==RR_RELIEF_RAISED
);
111 for (off
= 0, x
= 0; x
< h
; ++x
, off
++)
112 highlight(data
+ off
* w
,
113 data
+ off
* w
+ w
- 1,
114 a
->surface
.relief
==RR_RELIEF_RAISED
);
117 if (a
->surface
.bevel
== RR_BEVEL_2
) {
118 for (off
= 2, x
= 2; x
< w
- 2; ++x
, off
++)
119 highlight(data
+ off
+ w
,
120 data
+ off
+ (h
-2) * w
,
121 a
->surface
.relief
==RR_RELIEF_RAISED
);
122 for (off
= 1, x
= 1; x
< h
-1; ++x
, off
++)
123 highlight(data
+ off
* w
+ 1,
124 data
+ off
* w
+ w
- 2,
125 a
->surface
.relief
==RR_RELIEF_RAISED
);
130 static void highlight(RrPixel32
*x
, RrPixel32
*y
, gboolean raised
)
134 RrPixel32
*up
, *down
;
142 r
= (*up
>> RrDefaultRedOffset
) & 0xFF;
144 g
= (*up
>> RrDefaultGreenOffset
) & 0xFF;
146 b
= (*up
>> RrDefaultBlueOffset
) & 0xFF;
148 if (r
> 0xFF) r
= 0xFF;
149 if (g
> 0xFF) g
= 0xFF;
150 if (b
> 0xFF) b
= 0xFF;
151 *up
= (r
<< RrDefaultRedOffset
) + (g
<< RrDefaultGreenOffset
)
152 + (b
<< RrDefaultBlueOffset
);
154 r
= (*down
>> RrDefaultRedOffset
) & 0xFF;
155 r
= (r
>> 1) + (r
>> 2);
156 g
= (*down
>> RrDefaultGreenOffset
) & 0xFF;
157 g
= (g
>> 1) + (g
>> 2);
158 b
= (*down
>> RrDefaultBlueOffset
) & 0xFF;
159 b
= (b
>> 1) + (b
>> 2);
160 *down
= (r
<< RrDefaultRedOffset
) + (g
<< RrDefaultGreenOffset
)
161 + (b
<< RrDefaultBlueOffset
);
164 static void create_bevel_colors(RrAppearance
*l
)
169 r
= l
->surface
.primary
->r
;
171 g
= l
->surface
.primary
->g
;
173 b
= l
->surface
.primary
->b
;
175 if (r
> 0xFF) r
= 0xFF;
176 if (g
> 0xFF) g
= 0xFF;
177 if (b
> 0xFF) b
= 0xFF;
178 g_assert(!l
->surface
.bevel_light
);
179 l
->surface
.bevel_light
= RrColorNew(l
->inst
, r
, g
, b
);
182 r
= l
->surface
.primary
->r
;
183 r
= (r
>> 1) + (r
>> 2);
184 g
= l
->surface
.primary
->g
;
185 g
= (g
>> 1) + (g
>> 2);
186 b
= l
->surface
.primary
->b
;
187 b
= (b
>> 1) + (b
>> 2);
188 g_assert(!l
->surface
.bevel_dark
);
189 l
->surface
.bevel_dark
= RrColorNew(l
->inst
, r
, g
, b
);
192 static void gradient_solid(RrAppearance
*l
, gint w
, gint h
)
196 RrPixel32
*data
= l
->surface
.pixel_data
;
197 RrSurface
*sp
= &l
->surface
;
198 gint left
= 0, top
= 0, right
= w
- 1, bottom
= h
- 1;
200 pix
= (sp
->primary
->r
<< RrDefaultRedOffset
)
201 + (sp
->primary
->g
<< RrDefaultGreenOffset
)
202 + (sp
->primary
->b
<< RrDefaultBlueOffset
);
204 for (i
= 0; i
< w
* h
; i
++)
210 XFillRectangle(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->primary
),
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
+ 2, bottom
- 1, right
- 2, bottom
- 1);
233 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
234 right
- 1, bottom
- 1, right
- 1, top
+ 1);
236 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
237 left
+ 2, top
+ 1, right
- 2, top
+ 1);
238 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
239 left
+ 1, bottom
- 1, 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
+ 2, bottom
- 1, right
- 2, bottom
- 1);
264 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
,RrColorGC(sp
->bevel_light
),
265 right
- 1, bottom
- 1, right
- 1, top
+ 1);
267 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
268 left
+ 2, top
+ 1, right
- 2, top
+ 1);
269 XDrawLine(RrDisplay(l
->inst
), l
->pixmap
, RrColorGC(sp
->bevel_dark
),
270 left
+ 1, bottom
- 1, left
+ 1, top
+ 1);
273 g_assert_not_reached(); /* unhandled BevelType */
278 XDrawRectangle(RrDisplay(l
->inst
), l
->pixmap
,
279 RrColorGC(sp
->border_color
),
280 left
, top
, right
, bottom
);
284 g_assert_not_reached(); /* unhandled ReliefType */
288 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
292 gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
293 gboolean bigslope##x[3] /* color slope > 1 */
295 #define SETUP(x, from, to, w) \
298 color##x[0] = from->r; \
299 color##x[1] = from->g; \
300 color##x[2] = from->b; \
302 cdelta##x[0] = to->r - from->r; \
303 cdelta##x[1] = to->g - from->g; \
304 cdelta##x[2] = to->b - from->b; \
306 if (cdelta##x[0] < 0) { \
307 cdelta##x[0] = -cdelta##x[0]; \
311 if (cdelta##x[1] < 0) { \
312 cdelta##x[1] = -cdelta##x[1]; \
316 if (cdelta##x[2] < 0) { \
317 cdelta##x[2] = -cdelta##x[2]; \
321 bigslope##x[0] = cdelta##x[0] > w;\
322 bigslope##x[1] = cdelta##x[1] > w;\
323 bigslope##x[2] = cdelta##x[2] > w
325 #define COLOR_RR(x, c) \
326 c->r = color##x[0]; \
327 c->g = color##x[1]; \
331 ((color##x[0] << RrDefaultRedOffset) + \
332 (color##x[1] << RrDefaultGreenOffset) + \
333 (color##x[2] << RrDefaultBlueOffset))
335 #define INCREMENT(x, i) \
341 for (i = 2; i >= 0; --i) { \
342 if (!cdelta##x[i]) continue; \
344 if (!bigslope##x[i]) { \
345 /* Y (color) is dependant on X */ \
346 error##x[i] += cdelta##x[i]; \
347 if ((error##x[i] << 1) >= len##x) { \
348 color##x[i] += INCREMENT(x, i); \
349 error##x[i] -= len##x; \
352 /* X is dependant on Y (color) */ \
354 color##x[i] += INCREMENT(x, i); \
355 error##x[i] += len##x; \
356 if ((error##x[i] << 1) >= cdelta##x[i]) { \
357 error##x[i] -= cdelta##x[i]; \
365 static void gradient_split(RrAppearance
*a
, gint w
, gint h
)
367 gint x
, y1
, y3
, r
, g
, b
;
368 RrSurface
*sf
= &a
->surface
;
369 RrPixel32
*data
= sf
->pixel_data
;
371 RrColor
*primary_light
, *secondary_light
;
379 if (r
> 0xFF) r
= 0xFF;
380 if (g
> 0xFF) g
= 0xFF;
381 if (b
> 0xFF) b
= 0xFF;
382 primary_light
= RrColorNew(a
->inst
, r
, g
, b
);
384 r
= sf
->secondary
->r
;
386 g
= sf
->secondary
->g
;
388 b
= sf
->secondary
->b
;
390 if (r
> 0xFF) r
= 0xFF;
391 if (g
> 0xFF) g
= 0xFF;
392 if (b
> 0xFF) b
= 0xFF;
393 secondary_light
= RrColorNew(a
->inst
, r
, g
, b
);
396 SETUP(y1
, primary_light
, sf
->primary
, (h
/ 2) -1);
399 SETUP(y3
, sf
->secondary
, secondary_light
, (h
/ 2) -1);
401 for (y1
= h
- 1; y1
> (h
/ 2) -1; --y1
) { /* 0 -> h-1 */
403 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
410 for (y3
= (h
/ 2) - 1; y3
> 0; --y3
) {
412 for (x
= w
- 1; x
>= 0; --x
)
419 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
422 RrColorFree(primary_light
);
423 RrColorFree(secondary_light
);
426 static void gradient_horizontal(RrSurface
*sf
, gint w
, gint h
)
429 RrPixel32
*data
= sf
->pixel_data
, *datav
;
433 SETUP(x
, sf
->primary
, sf
->secondary
, w
);
435 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
438 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
447 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
448 *(data
+ y
* w
) = current
;
451 static void gradient_osx(RrSurface
*sf
, gint w
, gint h
)
454 RrPixel32
*data
= sf
->pixel_data
, *datav
;
458 SETUP(x
, sf
->primary
, sf
->secondary
, w
/2);
460 for (x
= w
- 1; x
> w
/2-1; --x
) { /* 0 -> w-1 */
463 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
471 SETUP(x
, sf
->secondary
, sf
->primary
, w
/2);
472 for (x
= w
/2 - 1; x
> 0; --x
) { /* 0 -> w-1 */
475 for (y
= h
- 1; y
>= 0; --y
) { /* 0 -> h */
484 for (y
= h
- 1; y
>= 0; --y
) /* 0 -> h */
485 *(data
+ y
* w
) = current
;
488 static void gradient_vertical(RrSurface
*sf
, gint w
, gint h
)
491 RrPixel32
*data
= sf
->pixel_data
;
495 SETUP(y
, sf
->primary
, sf
->secondary
, h
);
497 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
499 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
505 for (x
= w
- 1; x
>= 0; --x
) /* 0 -> w */
510 static void gradient_diagonal(RrSurface
*sf
, gint w
, gint h
)
513 RrPixel32
*data
= sf
->pixel_data
;
521 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
522 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
523 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
525 SETUP(lefty
, sf
->primary
, (&extracorner
), h
);
526 SETUP(righty
, (&extracorner
), sf
->secondary
, h
);
528 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
529 COLOR_RR(lefty
, (&left
));
530 COLOR_RR(righty
, (&right
));
532 SETUP(x
, (&left
), (&right
), w
);
534 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
535 *(data
++) = COLOR(x
);
539 *(data
++) = COLOR(x
);
544 COLOR_RR(lefty
, (&left
));
545 COLOR_RR(righty
, (&right
));
547 SETUP(x
, (&left
), (&right
), w
);
549 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
550 *(data
++) = COLOR(x
);
557 static void gradient_crossdiagonal(RrSurface
*sf
, gint w
, gint h
)
560 RrPixel32
*data
= sf
->pixel_data
;
568 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
569 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
570 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
572 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
573 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
575 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
576 COLOR_RR(lefty
, (&left
));
577 COLOR_RR(righty
, (&right
));
579 SETUP(x
, (&left
), (&right
), w
);
581 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
582 *(data
++) = COLOR(x
);
586 *(data
++) = COLOR(x
);
591 COLOR_RR(lefty
, (&left
));
592 COLOR_RR(righty
, (&right
));
594 SETUP(x
, (&left
), (&right
), w
);
596 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
597 *(data
++) = COLOR(x
);
604 static void gradient_pyramid(RrSurface
*sf
, gint inw
, gint inh
)
606 gint x
, y
, w
= (inw
>> 1) + 1, h
= (inh
>> 1) + 1;
607 RrPixel32
*data
= sf
->pixel_data
;
608 RrPixel32
*end
= data
+ inw
*inh
- 1;
617 extracorner
.r
= (sf
->primary
->r
+ sf
->secondary
->r
) / 2;
618 extracorner
.g
= (sf
->primary
->g
+ sf
->secondary
->g
) / 2;
619 extracorner
.b
= (sf
->primary
->b
+ sf
->secondary
->b
) / 2;
621 SETUP(lefty
, (&extracorner
), sf
->secondary
, h
);
622 SETUP(righty
, sf
->primary
, (&extracorner
), h
);
624 for (y
= h
- 1; y
> 0; --y
) { /* 0 -> h-1 */
625 COLOR_RR(lefty
, (&left
));
626 COLOR_RR(righty
, (&right
));
628 SETUP(x
, (&left
), (&right
), w
);
630 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
633 *(data
+inw
-x
) = current
;
635 *(end
-(inw
-x
)) = current
;
641 *(data
+inw
-x
) = current
;
643 *(end
-(inw
-x
)) = current
;
651 COLOR_RR(lefty
, (&left
));
652 COLOR_RR(righty
, (&right
));
654 SETUP(x
, (&left
), (&right
), w
);
656 for (x
= w
- 1; x
> 0; --x
) { /* 0 -> w-1 */
659 *(data
+inw
-x
) = current
;
661 *(end
-(inw
-x
)) = current
;
667 *(data
+inw
-x
) = current
;
669 *(end
-(inw
-x
)) = current
;