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_insert(RrColorHash(inst), &out->key, out);
87 void RrColorFree(RrColor *c)
90 if (--c->refcount < 1) {
91 g_assert(g_hash_table_lookup(RrColorHash(c->inst), &c->key));
92 g_hash_table_remove(RrColorHash(c->inst), &c->key);
93 if (c->pixel) XFreeColors(RrDisplay(c->inst), RrColormap(c->inst),
95 if (c->gc) XFreeGC(RrDisplay(c->inst), c->gc);
101 void RrReduceDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
105 RrPixel32 *p32 = (RrPixel32 *) im->data;
106 RrPixel16 *p16 = (RrPixel16 *) im->data;
107 unsigned char *p8 = (unsigned char *)im->data;
108 switch (im->bits_per_pixel) {
110 if ((RrRedOffset(inst) != RrDefaultRedOffset) ||
111 (RrBlueOffset(inst) != RrDefaultBlueOffset) ||
112 (RrGreenOffset(inst) != RrDefaultGreenOffset)) {
113 for (y = 0; y < im->height; y++) {
114 for (x = 0; x < im->width; x++) {
115 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
116 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
117 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
118 p32[x] = (r << RrRedOffset(inst))
119 + (g << RrGreenOffset(inst))
120 + (b << RrBlueOffset(inst));
125 } else im->data = (char*) data;
128 for (y = 0; y < im->height; y++) {
129 for (x = 0; x < im->width; x++) {
130 r = (data[x] >> RrDefaultRedOffset) & 0xFF;
131 r = r >> RrRedShift(inst);
132 g = (data[x] >> RrDefaultGreenOffset) & 0xFF;
133 g = g >> RrGreenShift(inst);
134 b = (data[x] >> RrDefaultBlueOffset) & 0xFF;
135 b = b >> RrBlueShift(inst);
136 p16[x] = (r << RrRedOffset(inst))
137 + (g << RrGreenOffset(inst))
138 + (b << RrBlueOffset(inst));
141 p16 += im->bytes_per_line/2;
145 g_assert(RrVisual(inst)->class != TrueColor);
146 for (y = 0; y < im->height; y++) {
147 for (x = 0; x < im->width; x++) {
148 p8[x] = RrPickColor(inst,
149 data[x] >> RrDefaultRedOffset,
150 data[x] >> RrDefaultGreenOffset,
151 data[x] >> RrDefaultBlueOffset)->pixel;
154 p8 += im->bytes_per_line;
159 g_warning("your bit depth is currently unhandled\n");
163 XColor *RrPickColor(const RrInstance *inst, gint r, gint g, gint b)
165 r = (r & 0xff) >> (8-RrPseudoBPC(inst));
166 g = (g & 0xff) >> (8-RrPseudoBPC(inst));
167 b = (b & 0xff) >> (8-RrPseudoBPC(inst));
168 return &RrPseudoColors(inst)[(r << (2*RrPseudoBPC(inst))) +
169 (g << (1*RrPseudoBPC(inst))) +
173 static void swap_byte_order(XImage *im)
178 for (y = 0; y < im->height; ++y) {
179 for (x = 0; x < im->height; ++x) {
180 char *c = &im->data[di + x * im->bits_per_pixel / 8];
183 switch (im->bits_per_pixel) {
195 g_warning("your bit depth is currently unhandled");
198 di += im->bytes_per_line;
201 if (im->byte_order == LSBFirst)
202 im->byte_order = MSBFirst;
204 im->byte_order = LSBFirst;
207 void RrIncreaseDepth(const RrInstance *inst, RrPixel32 *data, XImage *im)
211 RrPixel32 *p32 = (RrPixel32 *) im->data;
212 RrPixel16 *p16 = (RrPixel16 *) im->data;
213 unsigned char *p8 = (unsigned char *)im->data;
215 if (im->byte_order != LSBFirst)
218 switch (im->bits_per_pixel) {
220 for (y = 0; y < im->height; y++) {
221 for (x = 0; x < im->width; x++) {
222 r = (p32[x] >> RrRedOffset(inst)) & 0xff;
223 g = (p32[x] >> RrGreenOffset(inst)) & 0xff;
224 b = (p32[x] >> RrBlueOffset(inst)) & 0xff;
225 data[x] = (r << RrDefaultRedOffset)
226 + (g << RrDefaultGreenOffset)
227 + (b << RrDefaultBlueOffset)
228 + (0xff << RrDefaultAlphaOffset);
231 p32 += im->bytes_per_line/4;
235 for (y = 0; y < im->height; y++) {
236 for (x = 0; x < im->width; x++) {
237 r = (p16[x] & RrRedMask(inst)) >>
240 g = (p16[x] & RrGreenMask(inst)) >>
241 RrGreenOffset(inst) <<
243 b = (p16[x] & RrBlueMask(inst)) >>
244 RrBlueOffset(inst) <<
246 data[x] = (r << RrDefaultRedOffset)
247 + (g << RrDefaultGreenOffset)
248 + (b << RrDefaultBlueOffset)
249 + (0xff << RrDefaultAlphaOffset);
252 p16 += im->bytes_per_line/2;
256 g_warning("this image bit depth is currently unhandled");
259 for (y = 0; y < im->height; y++) {
260 for (x = 0; x < im->width; x++) {
261 if (!(((p8[x / 8]) >> (x % 8)) & 0x1))
262 data[x] = 0xff << RrDefaultAlphaOffset; /* black */
264 data[x] = 0xffffffff; /* white */
267 p8 += im->bytes_per_line;
271 g_warning("this image bit depth is currently unhandled");
275 int RrColorRed(const RrColor *c)
280 int RrColorGreen(const RrColor *c)
285 int RrColorBlue(const RrColor *c)
290 gulong RrColorPixel(const RrColor *c)
295 GC RrColorGC(RrColor *c)
298 RrColorAllocateGC(c);