1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 color.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 #include <X11/Xutil.h>
28 void RrColorAllocateGC(RrColor *in)
32 gcv.foreground = in->pixel;
33 gcv.cap_style = CapProjecting;
34 in->gc = XCreateGC(RrDisplay(in->inst),
35 RrRootWindow(in->inst),
36 GCForeground | GCCapStyle, &gcv);
39 RrColor *RrColorParse(const RrInstance *inst, gchar *colorname)
43 g_assert(colorname != NULL);
44 /* get rgb values from colorname */
50 if (!XParseColor(RrDisplay(inst), RrColormap(inst), colorname, &xcol)) {
51 g_warning("unable to parse color '%s'", colorname);
54 return RrColorNew(inst, xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
57 /*#define NO_COLOR_CACHE*/
62 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
64 /* this should be replaced with something far cooler */
69 key = (r << 24) + (g << 16) + (b << 8);
70 #ifndef NO_COLOR_CACHE
71 if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
75 xcol.red = (r << 8) | r;
76 xcol.green = (g << 8) | g;
77 xcol.blue = (b << 8) | b;
78 if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
79 out = g_new(RrColor, 1);
81 out->r = xcol.red >> 8;
82 out->g = xcol.green >> 8;
83 out->b = xcol.blue >> 8;
85 out->pixel = xcol.pixel;
91 #ifndef NO_COLOR_CACHE
92 g_hash_table_insert(RrColorHash(inst), &out->key, out);
99 void RrColorFree(RrColor *c)
102 if (--c->refcount < 1) {
103 #ifndef NO_COLOR_CACHE
104 g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
105 g_hash_table_remove(RrColorHash(c->inst), &c->key);
107 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
109 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
115 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
119 RrPixel32 *p32 = (RrPixel32 *) im->data;
120 RrPixel16 *p16 = (RrPixel16 *) im->data;
121 guchar *p8 = (guchar *)im->data;
122 switch (im->bits_per_pixel) {
124 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
125 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
126 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
127 for (y = 0; y < im->height; y++) {
128 for (x = 0; x < im->width; x++) {
129 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
130 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
131 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
132 p32[x] = (r << RrRedOffset(inst))
133 + (g << RrGreenOffset(inst))
134 + (b << RrBlueOffset(inst));
139 } else im->data = (gchar*) data;
142 for (y = 0; y < im->height; y++) {
143 for (x = 0; x < im->width; x++) {
144 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
145 r = r >> RrRedShift(inst);
146 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
147 g = g >> RrGreenShift(inst);
148 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
149 b = b >> RrBlueShift(inst);
150 p16[x] = (r << RrRedOffset(inst))
151 + (g << RrGreenOffset(inst))
152 + (b << RrBlueOffset(inst));
155 p16 += im->bytes_per_line/2;
159 g_assert(RrVisual(inst)->class != TrueColor);
160 for (y = 0; y < im->height; y++) {
161 for (x = 0; x < im->width; x++) {
162 p8[x] = RrPickColor(inst,
163 data[x] >> RrDefaultRedOffset,
164 data[x] >> RrDefaultGreenOffset,
165 data[x] >> RrDefaultBlueOffset)->pixel;
168 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) {
209 g_warning("your bit depth is currently unhandled");
212 di += im->bytes_per_line;
215 if (im->byte_order == LSBFirst)
216 im->byte_order = MSBFirst;
218 im->byte_order = LSBFirst;
221 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
225 RrPixel32 *p32 = (RrPixel32 *) im->data;
226 RrPixel16 *p16 = (RrPixel16 *) im->data;
227 guchar *p8 = (guchar *)im->data;
229 if (im->byte_order != LSBFirst)
232 switch (im->bits_per_pixel) {
234 for (y = 0; y < im->height; y++) {
235 for (x = 0; x < im->width; x++) {
236 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
237 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
238 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
239 data[x] = (r << RrDefaultRedOffset)
240 + (g << RrDefaultGreenOffset)
241 + (b << RrDefaultBlueOffset)
242 + (0xff << RrDefaultAlphaOffset);
245 p32 += im->bytes_per_line/4;
249 for (y = 0; y < im->height; y++) {
250 for (x = 0; x < im->width; x++) {
251 r = (p16[x] & RrRedMask(inst)) >>
254 g = (p16[x] & RrGreenMask(inst)) >>
255 RrGreenOffset(inst) <<
257 b = (p16[x] & RrBlueMask(inst)) >>
258 RrBlueOffset(inst) <<
260 data[x] = (r << RrDefaultRedOffset)
261 + (g << RrDefaultGreenOffset)
262 + (b << RrDefaultBlueOffset)
263 + (0xff << RrDefaultAlphaOffset);
266 p16 += im->bytes_per_line/2;
270 g_warning("this image bit depth is currently unhandled");
273 for (y = 0; y < im->height; y++) {
274 for (x = 0; x < im->width; x++) {
275 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
276 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
278 data[x] = 0xffffffff; /* white */
281 p8 += im->bytes_per_line;
285 g_warning("this image bit depth is currently unhandled");
289 gint RrColorRed(const RrColor *c)
294 gint RrColorGreen(const RrColor *c)
299 gint RrColorBlue(const RrColor *c)
304 gulong RrColorPixel(const RrColor *c)
309 GC RrColorGC(RrColor *c)
312 RrColorAllocateGC(c);