1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
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
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 #include <X11/Xutil.h>
29 void RrColorAllocateGC(RrColor *in)
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);
40 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
44 g_assert(colorname != NULL);
45 /* get rgb values from colorname */
51 if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
52 g_warning("unable to parse color '%s'", colorname);
55 return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
58 /*#define NO_COLOR_CACHE*/
63 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
65 /* this should be replaced with something far cooler */
70 key = (r << 24) + (g << 16) + (b << 8);
71 #ifndef NO_COLOR_CACHE
72 if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
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);
82 out->r = xcol.red >> 8;
83 out->g = xcol.green >> 8;
84 out->b = xcol.blue >> 8;
86 out->pixel = xcol.pixel;
92 #ifndef NO_COLOR_CACHE
93 g_hash_table_insert(RrColorHash(inst), &out->key, out);
100 void RrColorFree(RrColor *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);
108 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
110 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
116 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
120 RrPixel32 *p32 = (RrPixel32 *) im->data;
121 RrPixel16 *p16 = (RrPixel16 *) im->data;
122 guchar *p8 = (guchar *)im->data;
123 switch (im->bits_per_pixel) {
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));
140 } else im->data = (gchar*) data;
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));
156 p16 += im->bytes_per_line/2;
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;
169 p8 += im->bytes_per_line;
173 g_warning("your bit depth is currently unhandled\n");
177 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
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))) +
187 static void swap_byte_order(XImage *im)
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];
197 switch (im->bits_per_pixel) {
210 g_warning("Your bit depth is currently unhandled");
213 di += im->bytes_per_line;
216 if (im->byte_order == LSBFirst)
217 im->byte_order = MSBFirst;
219 im->byte_order = LSBFirst;
222 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
226 RrPixel32 *p32 = (RrPixel32 *) im->data;
227 RrPixel16 *p16 = (RrPixel16 *) im->data;
228 guchar *p8 = (guchar *)im->data;
230 if (im->byte_order != LSBFirst)
233 switch (im->bits_per_pixel) {
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);
246 p32 += im->bytes_per_line/4;
250 for (y = 0; y < im->height; y++) {
251 for (x = 0; x < im->width; x++) {
252 r = (p16[x] & RrRedMask(inst)) >>
255 g = (p16[x] & RrGreenMask(inst)) >>
256 RrGreenOffset(inst) <<
258 b = (p16[x] & RrBlueMask(inst)) >>
259 RrBlueOffset(inst) <<
261 data[x] = (r << RrDefaultRedOffset)
262 + (g << RrDefaultGreenOffset)
263 + (b << RrDefaultBlueOffset)
264 + (0xff << RrDefaultAlphaOffset);
267 p16 += im->bytes_per_line/2;
271 g_warning("this image bit depth is currently unhandled");
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 */
279 data[x] = 0xffffffff; /* white */
282 p8 += im->bytes_per_line;
286 g_warning("this image bit depth is currently unhandled");
290 gint RrColorRed(const RrColor *c)
295 gint RrColorGreen(const RrColor *c)
300 gint RrColorBlue(const RrColor *c)
305 gulong RrColorPixel(const RrColor *c)
310 GC RrColorGC(RrColor *c)
313 RrColorAllocateGC(c);