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 RrColor *RrColorNew(const RrInstance *inst, gint r, gint g, gint b)
59 /* this should be replaced with something far cooler */
64 key = (r << 24) + (g << 16) + (b << 8);
65 if ((out = g_hash_table_lookup(RrColorHash(inst), &key))) {
68 xcol.red = (r << 8) | r;
69 xcol.green = (g << 8) | g;
70 xcol.blue = (b << 8) | b;
71 if (XAllocColor(RrDisplay(inst), RrColormap(inst), &xcol)) {
72 out = g_new(RrColor, 1);
74 out->r = xcol.red >> 8;
75 out->g = xcol.green >> 8;
76 out->b = xcol.blue >> 8;
78 out->pixel = xcol.pixel;
81 g_hash_table_replace(RrColorHash(inst), &out->key, out);
87 void RrColorFree(RrColor *c)
90 if (--c->refcount < 1) {
91 g_hash_table_remove(RrColorHash(c->inst), &c->key);
92 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
94 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
100 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
104 RrPixel32 *p32 = (RrPixel32 *) im->data;
105 RrPixel16 *p16 = (RrPixel16 *) im->data;
106 unsigned char *p8 = (unsigned char *)im->data;
107 switch (im->bits_per_pixel) {
109 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
110 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
111 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
112 for (y = 0; y < im->height; y++) {
113 for (x = 0; x < im->width; x++) {
114 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
115 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
116 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
117 p32[x] = (r << RrRedOffset(inst))
118 + (g << RrGreenOffset(inst))
119 + (b << RrBlueOffset(inst));
124 } else im->data = (char*) data;
127 for (y = 0; y < im->height; y++) {
128 for (x = 0; x < im->width; x++) {
129 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
130 r = r >> RrRedShift(inst);
131 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
132 g = g >> RrGreenShift(inst);
133 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
134 b = b >> RrBlueShift(inst);
135 p16[x] = (r << RrRedOffset(inst))
136 + (g << RrGreenOffset(inst))
137 + (b << RrBlueOffset(inst));
140 p16 += im->bytes_per_line/2;
144 g_assert(RrVisual(inst)->class != TrueColor);
145 for (y = 0; y < im->height; y++) {
146 for (x = 0; x < im->width; x++) {
147 p8[x] = RrPickColor(inst,
148 data[x] >> RrDefaultRedOffset,
149 data[x] >> RrDefaultGreenOffset,
150 data[x] >> RrDefaultBlueOffset)->pixel;
153 p8 += im->bytes_per_line;
158 g_warning("your bit depth is currently unhandled\n");
162 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
164 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
165 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
166 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
167 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
168 (g << (1*RrPseudoBPC(inst))) +
172 static void swap_byte_order(XImage *im)
177 for (y = 0; y < im->height; ++y) {
178 for (x = 0; x < im->height; ++x) {
179 char *c = &im->data[di + x * im->bits_per_pixel / 8];
182 switch (im->bits_per_pixel) {
194 g_warning("your bit depth is currently unhandled");
197 di += im->bytes_per_line;
200 if (im->byte_order == LSBFirst)
201 im->byte_order = MSBFirst;
203 im->byte_order = LSBFirst;
206 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
210 RrPixel32 *p32 = (RrPixel32 *) im->data;
211 RrPixel16 *p16 = (RrPixel16 *) im->data;
212 unsigned char *p8 = (unsigned char *)im->data;
214 if (im->byte_order != LSBFirst)
217 switch (im->bits_per_pixel) {
219 for (y = 0; y < im->height; y++) {
220 for (x = 0; x < im->width; x++) {
221 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
222 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
223 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
224 data[x] = (r << RrDefaultRedOffset)
225 + (g << RrDefaultGreenOffset)
226 + (b << RrDefaultBlueOffset)
227 + (0xff << RrDefaultAlphaOffset);
230 p32 += im->bytes_per_line/4;
234 for (y = 0; y < im->height; y++) {
235 for (x = 0; x < im->width; x++) {
236 r = (p16[x] & RrRedMask(inst)) >>
239 g = (p16[x] & RrGreenMask(inst)) >>
240 RrGreenOffset(inst) <<
242 b = (p16[x] & RrBlueMask(inst)) >>
243 RrBlueOffset(inst) <<
245 data[x] = (r << RrDefaultRedOffset)
246 + (g << RrDefaultGreenOffset)
247 + (b << RrDefaultBlueOffset)
248 + (0xff << RrDefaultAlphaOffset);
251 p16 += im->bytes_per_line/2;
255 g_warning("this image bit depth is currently unhandled");
258 for (y = 0; y < im->height; y++) {
259 for (x = 0; x < im->width; x++) {
260 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
261 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
263 data[x] = 0xffffffff; /* white */
266 p8 += im->bytes_per_line;
270 g_warning("this image bit depth is currently unhandled");
274 int RrColorRed(const RrColor *c)
279 int RrColorGreen(const RrColor *c)
284 int RrColorBlue(const RrColor *c)
289 gulong RrColorPixel(const RrColor *c)
294 GC RrColorGC(RrColor *c)
297 RrColorAllocateGC(c);