applied patch to rename .AppInfo directory
[wmaker-crm.git] / WINGs / wcolor.c
blobf35252bdb7eac208a3fe5eccd0fd64556879dc13
2 #include "WINGsP.h"
4 #include "wconfig.h"
6 #include <wraster.h>
8 #define LIGHT_STIPPLE_WIDTH 4
9 #define LIGHT_STIPPLE_HEIGHT 4
10 static unsigned char LIGHT_STIPPLE_BITS[] = {
11 0x05, 0x0a, 0x05, 0x0a};
13 #define DARK_STIPPLE_WIDTH 4
14 #define DARK_STIPPLE_HEIGHT 4
15 static unsigned char DARK_STIPPLE_BITS[] = {
16 0x0a, 0x04, 0x0a, 0x01};
19 static WMColor *createRGBAColor(WMScreen *scr, unsigned short red,
20 unsigned short green, unsigned short blue,
21 unsigned short alpha);
24 * TODO: make the color creation code return the same WMColor for the
25 * same colors.
26 * make findCloseColor() find the closest color in the RContext pallette
27 * or in the other colors allocated by WINGs.
30 static WMColor*
31 findCloseColor(WMScreen *scr, unsigned short red, unsigned short green,
32 unsigned short blue, unsigned short alpha)
34 WMColor *color;
35 XColor xcolor;
36 RColor rcolor;
38 rcolor.red = red>>8;
39 rcolor.green = green>>8;
40 rcolor.blue = blue>>8;
41 rcolor.alpha = alpha>>8;
43 if (!RGetClosestXColor(scr->rcontext, &rcolor, &xcolor))
44 return NULL;
46 if (!XAllocColor(scr->display, scr->colormap, &xcolor))
47 return NULL;
49 color = wmalloc(sizeof(WMColor));
51 color->screen = scr;
52 color->refCount = 1;
53 color->color = xcolor;
54 color->alpha = alpha;
55 color->flags.exact = 1;
56 color->gc = NULL;
58 return color;
63 static WMColor*
64 createRGBAColor(WMScreen *scr, unsigned short red, unsigned short green,
65 unsigned short blue, unsigned short alpha)
67 WMColor *color;
68 XColor xcolor;
70 xcolor.red = red;
71 xcolor.green = green;
72 xcolor.blue = blue;
73 xcolor.flags = DoRed|DoGreen|DoBlue;
74 if (!XAllocColor(scr->display, scr->colormap, &xcolor))
75 return NULL;
77 color = wmalloc(sizeof(WMColor));
79 color->screen = scr;
80 color->refCount = 1;
81 color->color = xcolor;
82 color->alpha = alpha;
83 color->flags.exact = 1;
84 color->gc = NULL;
86 return color;
90 WMColor*
91 WMCreateRGBColor(WMScreen *scr, unsigned short red, unsigned short green,
92 unsigned short blue, Bool exact)
94 WMColor *color = NULL;
96 if (!exact || !(color=createRGBAColor(scr, red, green, blue, 0xffff))) {
97 color = findCloseColor(scr, red, green, blue, 0xffff);
99 if (!color)
100 color = WMBlackColor(scr);
102 return color;
106 RColor WMGetRColorFromColor(WMColor *color)
108 RColor rcolor;
110 rcolor.red= color->color.red>>8;
111 rcolor.green= color->color.green>>8;
112 rcolor.blue= color->color.blue>>8;
113 rcolor.alpha= color->alpha>>8;
115 return rcolor;
119 WMColor*
120 WMCreateRGBAColor(WMScreen *scr, unsigned short red, unsigned short green,
121 unsigned short blue, unsigned short alpha, Bool exact)
123 WMColor *color = NULL;
125 if (!exact || !(color=createRGBAColor(scr, red, green, blue, alpha))) {
126 color = findCloseColor(scr, red, green, blue, alpha);
128 if (!color)
129 color = WMBlackColor(scr);
131 return color;
135 WMColor*
136 WMCreateNamedColor(WMScreen *scr, char *name, Bool exact)
138 WMColor *color;
139 XColor xcolor;
141 if (!XParseColor(scr->display, scr->colormap, name, &xcolor))
142 return NULL;
144 if (scr->visual->class == TrueColor)
145 exact = True;
147 if (!exact || !(color=createRGBAColor(scr, xcolor.red, xcolor.green,
148 xcolor.blue, 0xffff))) {
149 color = findCloseColor(scr, xcolor.red, xcolor.green, xcolor.blue, 0xffff);
151 return color;
156 WMColor*
157 WMRetainColor(WMColor *color)
159 assert(color!=NULL);
161 color->refCount++;
163 return color;
167 void
168 WMReleaseColor(WMColor *color)
170 color->refCount--;
172 if (color->refCount < 1) {
173 XFreeColors(color->screen->display, color->screen->colormap,
174 &(color->color.pixel), 1, 0);
175 if (color->gc)
176 XFreeGC(color->screen->display, color->gc);
177 wfree(color);
182 void
183 WMSetColorAlpha(WMColor *color, unsigned short alpha)
185 color->alpha = alpha;
189 void
190 WMPaintColorSwatch(WMColor *color, Drawable d, int x, int y,
191 unsigned int width, unsigned int height)
193 XFillRectangle(color->screen->display, d, WMColorGC(color),
194 x, y, width, height);
198 WMPixel
199 WMColorPixel(WMColor *color)
201 return color->color.pixel;
206 WMColorGC(WMColor *color)
208 if (!color->gc) {
209 XGCValues gcv;
210 WMScreen *scr = color->screen;
212 gcv.foreground = color->color.pixel;
213 gcv.graphics_exposures = False;
214 color->gc = XCreateGC(scr->display, scr->rcontext->drawable,
215 GCForeground|GCGraphicsExposures, &gcv);
218 return color->gc;
222 void
223 WMSetColorInGC(WMColor *color, GC gc)
225 XSetForeground(color->screen->display, gc, color->color.pixel);
230 /* "system" colors */
231 WMColor*
232 WMWhiteColor(WMScreen *scr)
234 if (!scr->white) {
235 scr->white = WMCreateRGBColor(scr, 0xffff, 0xffff, 0xffff, True);
236 if (!scr->white->flags.exact)
237 wwarning(_("could not allocate %s color"), _("white"));
239 return WMRetainColor(scr->white);
243 WMColor*
244 WMBlackColor(WMScreen *scr)
246 if (!scr->black) {
247 scr->black = WMCreateRGBColor(scr, 0, 0, 0, True);
248 if (!scr->black->flags.exact)
249 wwarning(_("could not allocate %s color"), _("black"));
251 return WMRetainColor(scr->black);
256 WMColor*
257 WMGrayColor(WMScreen *scr)
259 if (!scr->gray) {
260 WMColor *color;
262 if (scr->depth == 1) {
263 Pixmap stipple;
264 WMColor *white = WMWhiteColor(scr);
265 WMColor *black = WMBlackColor(scr);
266 XGCValues gcv;
268 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
269 LIGHT_STIPPLE_BITS, LIGHT_STIPPLE_WIDTH,
270 LIGHT_STIPPLE_HEIGHT);
272 color = createRGBAColor(scr, 0xffff, 0xffff, 0xffff, 0xffff);
274 gcv.foreground = white->color.pixel;
275 gcv.background = black->color.pixel;
276 gcv.fill_style = FillStippled;
277 gcv.stipple = stipple;
278 color->gc = XCreateGC(scr->display, W_DRAWABLE(scr), GCForeground
279 |GCBackground|GCStipple|GCFillStyle
280 |GCGraphicsExposures, &gcv);
282 XFreePixmap(scr->display, stipple);
283 WMReleaseColor(white);
284 WMReleaseColor(black);
285 } else {
286 color = WMCreateRGBColor(scr, 0xaeba, 0xaaaa, 0xaeba, True);
287 if (!color->flags.exact)
288 wwarning(_("could not allocate %s color"), _("gray"));
290 scr->gray = color;
292 return WMRetainColor(scr->gray);
297 WMColor*
298 WMDarkGrayColor(WMScreen *scr)
300 if (!scr->darkGray) {
301 WMColor *color;
303 if (scr->depth == 1) {
304 Pixmap stipple;
305 WMColor *white = WMWhiteColor(scr);
306 WMColor *black = WMBlackColor(scr);
307 XGCValues gcv;
309 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
310 DARK_STIPPLE_BITS, DARK_STIPPLE_WIDTH,
311 DARK_STIPPLE_HEIGHT);
313 color = createRGBAColor(scr, 0, 0, 0, 0xffff);
315 gcv.foreground = white->color.pixel;
316 gcv.background = black->color.pixel;
317 gcv.fill_style = FillStippled;
318 gcv.stipple = stipple;
319 color->gc = XCreateGC(scr->display, W_DRAWABLE(scr), GCForeground
320 |GCBackground|GCStipple|GCFillStyle
321 |GCGraphicsExposures, &gcv);
323 XFreePixmap(scr->display, stipple);
324 WMReleaseColor(white);
325 WMReleaseColor(black);
326 } else {
327 color = WMCreateRGBColor(scr, 0x5144, 0x5555, 0x5144, True);
328 if (!color->flags.exact)
329 wwarning(_("could not allocate %s color"), _("dark gray"));
331 scr->darkGray = color;
333 return WMRetainColor(scr->darkGray);
337 unsigned short
338 WMRedComponentOfColor(WMColor *color)
340 return color->color.red;
344 unsigned short
345 WMGreenComponentOfColor(WMColor *color)
347 return color->color.green;
351 unsigned short
352 WMBlueComponentOfColor(WMColor *color)
354 return color->color.blue;
358 unsigned short
359 WMGetColorAlpha(WMColor *color)
361 return color->alpha;
365 char*
366 WMGetColorRGBDescription(WMColor *color)
368 char *str = wmalloc(32);
370 sprintf(str, "#%02x%02x%02x", color->color.red>>8, color->color.green>>8,
371 color->color.blue>>8);
373 return str;