]> icculus.org git repositories - mikachu/openbox.git/blob - render/color.c
keep the default tint at 50, for theme compatibility
[mikachu/openbox.git] / render / color.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    color.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003        Ben Jansens
6    Copyright (c) 2003        Derek Foreman
7
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.
12
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.
17
18    See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include "render.h"
22 #include "color.h"
23 #include "instance.h"
24
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <string.h>
28
29 void RrColorAllocateGC(RrColor *in)
30 {
31     XGCValues gcv;
32
33     gcv.foreground = in->pixel;
34     gcv.cap_style = CapProjecting;
35     in->gc = XCreateGC(RrDisplay(in->inst),
36                        RrRootWindow(in->inst),
37                        GCForeground | GCCapStyle, &gcv);
38 }
39
40 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
41 {
42     XColor xcol;
43
44     g_assert(colorname != NULL);
45     /* get rgb values from colorname */
46
47     xcol.red = 0;
48     xcol.green = 0;
49     xcol.blue = 0;
50     xcol.pixel = 0;
51     if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
52         g_warning("unable to parse color '%s'", colorname);
53         return NULL;
54     }
55     return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
56 }
57
58 /*#define NO_COLOR_CACHE*/
59 #ifdef DEBUG
60 gint id;
61 #endif
62
63 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
64 {
65     /* this should be replaced with something far cooler */
66     RrColor *out = NULL;
67     XColor xcol;
68     gint key;
69
70     key = (r << 24) + (g << 16) + (b << 8);
71 #ifndef NO_COLOR_CACHE
72     if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
73         out->refcount++;
74     } else {
75 #endif
76         xcol.red = (r << 8) | r;
77         xcol.green = (g << 8) | g;
78         xcol.blue = (b << 8) | b;
79         if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
80             out = g_new(RrColor, 1);
81             out->inst = inst;
82             out->r = xcol.red >> 8;
83             out->g = xcol.green >> 8;
84             out->b = xcol.blue >> 8;
85             out->gc = None;
86             out->pixel = xcol.pixel;
87             out->key = key;
88             out->refcount = 1;
89 #ifdef DEBUG
90             out->id = id++;
91 #endif
92 #ifndef NO_COLOR_CACHE
93             g_hash_table_insert(RrColorHash(inst), &out->key, out);
94         }
95 #endif
96     }
97     return out;
98 }
99
100 void RrColorFree(RrColor *c)
101 {
102     if (c) {
103         if (--c->refcount < 1) {
104 #ifndef NO_COLOR_CACHE
105             g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
106             g_hash_table_remove(RrColorHash(c->inst), &c->key);
107 #endif
108             if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
109                                       &c->pixel, 1, 0);
110             if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
111             g_free(c);
112         }
113     }
114 }
115
116 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
117 {
118     gint r, g, b;
119     gint x,y;
120     RrPixel32 *p32 = (RrPixel32 *) im->data;
121     RrPixel16 *p16 = (RrPixel16 *) im->data;
122     guchar *p8 = (guchar *)im->data;
123     switch (im->bits_per_pixel) {
124     case 32:
125         if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
126             (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
127             (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
128             for (y = 0; y < im->height; y++) {
129                 for (x = 0; x < im->width; x++) {
130                     r = (data[x] >> RrDefaultRedOffset) & 0xFF;
131                     g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
132                     b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
133                     p32[x] = (r << RrRedOffset(inst))
134                            + (g << RrGreenOffset(inst))
135                            + (b << RrBlueOffset(inst));
136                 }
137                 data += im->width;
138                 p32 += im->width;
139             } 
140         } else im->data = (gchar*) data;
141         break;
142     case 16:
143         for (y = 0; y < im->height; y++) {
144             for (x = 0; x < im->width; x++) {
145                 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
146                 r = r >> RrRedShift(inst);
147                 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
148                 g = g >> RrGreenShift(inst);
149                 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
150                 b = b >> RrBlueShift(inst);
151                 p16[x] = (r << RrRedOffset(inst))
152                        + (g << RrGreenOffset(inst))
153                        + (b << RrBlueOffset(inst));
154             }
155             data += im->width;
156             p16 += im->bytes_per_line/2;
157         }
158         break;
159     case 8:
160         g_assert(RrVisual(inst)->class != TrueColor);
161         for (y = 0; y < im->height; y++) {
162             for (x = 0; x < im->width; x++) {
163                 p8[x] = RrPickColor(inst,
164                                     data[x] >> RrDefaultRedOffset,
165                                     data[x] >> RrDefaultGreenOffset,
166                                     data[x] >> RrDefaultBlueOffset)->pixel;
167             }
168             data += im->width;
169             p8 += im->bytes_per_line;
170         }
171         break;
172     default:
173         g_warning("your bit depth is currently unhandled\n");
174     }
175 }
176
177 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b) 
178 {
179   r = (r & 0xff) >> (8-RrPseudoBPC(inst));
180   g = (g & 0xff) >> (8-RrPseudoBPC(inst));
181   b = (b & 0xff) >> (8-RrPseudoBPC(inst));
182   return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
183                                (g << (1*RrPseudoBPC(inst))) +
184                                b];
185 }
186
187 static void swap_byte_order(XImage *im)
188 {
189     gint x, y, di;
190
191     di = 0;
192     for (y = 0; y < im->height; ++y) {
193         for (x = 0; x < im->height; ++x) {
194             gchar *c = &im->data[di + x * im->bits_per_pixel / 8];
195             gchar t;
196
197             switch (im->bits_per_pixel) {
198             case 32:
199                 t = c[2];
200                 c[2] = c[3];
201                 c[3] = t;
202             case 16:
203                 t = c[0];
204                 c[0] = c[1];
205                 c[1] = t;
206             case 8:
207             case 1:
208                 break;
209             default:
210                 g_warning("Your bit depth is currently unhandled");
211             }
212         }
213         di += im->bytes_per_line;
214     }
215
216     if (im->byte_order == LSBFirst)
217         im->byte_order = MSBFirst;
218     else
219         im->byte_order = LSBFirst;
220 }
221
222 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
223 {
224     gint r, g, b;
225     gint x,y;
226     RrPixel32 *p32 = (RrPixel32 *) im->data;
227     RrPixel16 *p16 = (RrPixel16 *) im->data;
228     guchar *p8 = (guchar *)im->data;
229
230     if (im->byte_order != LSBFirst)
231         swap_byte_order(im);
232
233     switch (im->bits_per_pixel) {
234     case 32:
235         for (y = 0; y < im->height; y++) {
236             for (x = 0; x < im->width; x++) {
237                 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
238                 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
239                 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
240                 data[x] = (r << RrDefaultRedOffset)
241                     + (g << RrDefaultGreenOffset)
242                     + (b << RrDefaultBlueOffset)
243                     + (0xff << RrDefaultAlphaOffset);
244             }
245             data += im->width;
246             p32 += im->bytes_per_line/4;
247         }
248         break;
249     case 16:
250         for (y = 0; y < im->height; y++) {
251             for (x = 0; x < im->width; x++) {
252                 r = (p16[x] & RrRedMask(inst)) >>
253                     RrRedOffset(inst) <<
254                     RrRedShift(inst);
255                 g = (p16[x] & RrGreenMask(inst)) >>
256                     RrGreenOffset(inst) <<
257                     RrGreenShift(inst);
258                 b = (p16[x] & RrBlueMask(inst)) >>
259                     RrBlueOffset(inst) <<
260                     RrBlueShift(inst);
261                 data[x] = (r << RrDefaultRedOffset)
262                     + (g << RrDefaultGreenOffset)
263                     + (b << RrDefaultBlueOffset)
264                     + (0xff << RrDefaultAlphaOffset);
265             }
266             data += im->width;
267             p16 += im->bytes_per_line/2;
268         }
269         break;
270     case 8:
271         g_warning("this image bit depth is currently unhandled");
272         break;
273     case 1:
274         for (y = 0; y < im->height; y++) {
275             for (x = 0; x < im->width; x++) {
276                 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
277                     data[x] = 0xff << RrDefaultAlphaOffset; /* black */
278                 else
279                     data[x] = 0xffffffff; /* white */
280             }
281             data += im->width;
282             p8 += im->bytes_per_line;
283         }
284         break;
285     default:
286         g_warning("this image bit depth is currently unhandled");
287     }
288 }
289
290 gint RrColorRed(const RrColor *c)
291 {
292     return c->r;
293 }
294
295 gint RrColorGreen(const RrColor *c)
296 {
297     return c->g;
298 }
299
300 gint RrColorBlue(const RrColor *c)
301 {
302     return c->b;
303 }
304
305 gulong RrColorPixel(const RrColor *c)
306 {
307     return c->pixel;
308 }
309
310 GC RrColorGC(RrColor *c)
311 {
312     if (!c->gc)
313         RrColorAllocateGC(c);
314     return c->gc;
315 }