Port WM2 branch onto next
[wmaker-crm.git] / WINGs / wcolor.c
blob69b416450ca0b9b1e2abbd63a8a94b6856d9d2dd
2 #include "WINGsP.h"
4 #include "wconfig.h"
7 #define LIGHT_STIPPLE_WIDTH 4
8 #define LIGHT_STIPPLE_HEIGHT 4
9 static char LIGHT_STIPPLE_BITS[] = {
10 0x05, 0x0a, 0x05, 0x0a
13 #define DARK_STIPPLE_WIDTH 4
14 #define DARK_STIPPLE_HEIGHT 4
15 static 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, unsigned short alpha);
23 * TODO: make the color creation code return the same WMColor for the
24 * same colors.
25 * make findCloseColor() find the closest color in the RContext pallette
26 * or in the other colors allocated by WINGs.
29 static WMColor *findCloseColor(WMScreen * scr, unsigned short red, unsigned short green,
30 unsigned short blue, unsigned short alpha)
32 return createRGBAColor(scr, red, green, blue, alpha);
35 static WMColor *createRGBAColor(WMScreen * scr, unsigned short red, unsigned short green,
36 unsigned short blue, unsigned short alpha)
38 WMColor *color;
39 XColor xcolor;
41 xcolor.red = red;
42 xcolor.green = green;
43 xcolor.blue = blue;
44 xcolor.flags = DoRed | DoGreen | DoBlue;
45 if (!XAllocColor(scr->display, scr->colormap, &xcolor))
46 return NULL;
48 color = wmalloc(sizeof(WMColor));
50 color->screen = scr;
51 color->refCount = 1;
52 color->color = xcolor;
53 color->alpha = alpha;
54 color->flags.exact = 1;
55 color->gc = NULL;
57 return color;
60 WMColor *WMCreateRGBColor(WMScreen * scr, unsigned short red, unsigned short green,
61 unsigned short blue, Bool exact)
63 WMColor *color = NULL;
65 if (!exact || !(color = createRGBAColor(scr, red, green, blue, 0xffff))) {
66 color = findCloseColor(scr, red, green, blue, 0xffff);
68 if (!color)
69 color = WMBlackColor(scr);
71 return color;
74 WMColor* WMCreateColorWithSpec(WMScreen *scr, WMColorSpec *spec)
76 WMColor *color = NULL;
78 if (!(color=createRGBAColor(scr, spec->red<<8, spec->green<<8, spec->blue<<8, 0xffff)))
79 color = findCloseColor(scr, spec->red<<8, spec->green<<8, spec->blue<<8, 0xffff);
81 if (!color)
82 color = WMBlackColor(scr);
84 return color;
88 void WMGetComponentsFromColor(WMColor *color, unsigned short *r, unsigned short *g, unsigned short *b, unsigned short *a)
90 *r= color->color.red;
91 *g= color->color.green;
92 *b= color->color.blue;
93 *a= color->alpha;
96 WMColor *WMCreateRGBAColor(WMScreen * scr, unsigned short red, unsigned short green,
97 unsigned short blue, unsigned short alpha, Bool exact)
99 WMColor *color = NULL;
101 if (!exact || !(color = createRGBAColor(scr, red, green, blue, alpha))) {
102 color = findCloseColor(scr, red, green, blue, alpha);
104 if (!color)
105 color = WMBlackColor(scr);
107 return color;
110 WMColor *WMCreateNamedColor(WMScreen * scr, char *name, Bool exact)
112 WMColor *color;
113 XColor xcolor;
115 if (!XParseColor(scr->display, scr->colormap, name, &xcolor))
116 return NULL;
118 if (scr->visual->class == TrueColor)
119 exact = True;
121 if (!exact || !(color = createRGBAColor(scr, xcolor.red, xcolor.green, xcolor.blue, 0xffff))) {
122 color = findCloseColor(scr, xcolor.red, xcolor.green, xcolor.blue, 0xffff);
124 return color;
127 WMColor *WMRetainColor(WMColor * color)
129 assert(color != NULL);
131 color->refCount++;
133 return color;
136 void WMReleaseColor(WMColor * color)
138 color->refCount--;
140 if (color->refCount < 1) {
141 XFreeColors(color->screen->display, color->screen->colormap, &(color->color.pixel), 1, 0);
142 if (color->gc)
143 XFreeGC(color->screen->display, color->gc);
144 wfree(color);
148 void WMSetColorAlpha(WMColor * color, unsigned short alpha)
150 color->alpha = alpha;
153 void WMPaintColorSwatch(WMColor * color, Drawable d, int x, int y, unsigned int width, unsigned int height)
155 XFillRectangle(color->screen->display, d, WMColorGC(color), x, y, width, height);
158 WMPixel WMColorPixel(WMColor * color)
160 return color->color.pixel;
163 GC WMColorGC(WMColor * color)
165 #ifdef obsolete
166 if (!color->gc) {
167 XGCValues gcv;
168 WMScreen *scr = color->screen;
170 gcv.foreground = color->color.pixel;
171 gcv.graphics_exposures = False;
172 color->gc = XCreateGC(scr->display, scr->rcontext->drawable,
173 GCForeground | GCGraphicsExposures, &gcv);
175 #endif
176 return color->gc;
179 void WMSetColorInGC(WMColor * color, GC gc)
181 #ifdef obsolete
182 XSetForeground(color->screen->display, gc, color->color.pixel);
183 #endif
186 /* "system" colors */
187 WMColor *WMWhiteColor(WMScreen * scr)
189 if (!scr->white) {
190 scr->white = WMCreateRGBColor(scr, 0xffff, 0xffff, 0xffff, True);
191 if (!scr->white->flags.exact)
192 wwarning(_("could not allocate %s color"), _("white"));
194 return WMRetainColor(scr->white);
196 #ifdef obsolete
198 WMColor *WMBlackColor(WMScreen * scr)
200 if (!scr->black) {
201 scr->black = WMCreateRGBColor(scr, 0, 0, 0, True);
202 if (!scr->black->flags.exact)
203 wwarning(_("could not allocate %s color"), _("black"));
205 return WMRetainColor(scr->black);
207 #endif
208 WMColor *WMGrayColor(WMScreen * scr)
210 if (!scr->gray) {
211 WMColor *color;
213 if (scr->depth == 1) {
214 Pixmap stipple;
215 WMColor *white = WMWhiteColor(scr);
216 WMColor *black = WMBlackColor(scr);
217 XGCValues gcv;
219 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
220 LIGHT_STIPPLE_BITS, LIGHT_STIPPLE_WIDTH,
221 LIGHT_STIPPLE_HEIGHT);
223 color = createRGBAColor(scr, 0xffff, 0xffff, 0xffff, 0xffff);
225 gcv.foreground = white->color.pixel;
226 gcv.background = black->color.pixel;
227 gcv.fill_style = FillStippled;
228 gcv.stipple = stipple;
229 color->gc = XCreateGC(scr->display, W_DRAWABLE(scr), GCForeground
230 | GCBackground | GCStipple | GCFillStyle
231 | GCGraphicsExposures, &gcv);
233 XFreePixmap(scr->display, stipple);
234 WMReleaseColor(white);
235 WMReleaseColor(black);
236 } else {
237 color = WMCreateRGBColor(scr, 0xaeba, 0xaaaa, 0xaeba, True);
238 if (!color->flags.exact)
239 wwarning(_("could not allocate %s color"), _("gray"));
241 scr->gray = color;
243 return WMRetainColor(scr->gray);
246 WMColor *WMDarkGrayColor(WMScreen * scr)
248 #ifdef obsolete
249 if (!scr->darkGray) {
250 WMColor *color;
252 if (scr->depth == 1) {
253 Pixmap stipple;
254 WMColor *white = WMWhiteColor(scr);
255 WMColor *black = WMBlackColor(scr);
256 XGCValues gcv;
258 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
259 DARK_STIPPLE_BITS, DARK_STIPPLE_WIDTH,
260 DARK_STIPPLE_HEIGHT);
262 color = createRGBAColor(scr, 0, 0, 0, 0xffff);
264 gcv.foreground = white->color.pixel;
265 gcv.background = black->color.pixel;
266 gcv.fill_style = FillStippled;
267 gcv.stipple = stipple;
268 color->gc = XCreateGC(scr->display, W_DRAWABLE(scr), GCForeground
269 | GCBackground | GCStipple | GCFillStyle
270 | GCGraphicsExposures, &gcv);
272 XFreePixmap(scr->display, stipple);
273 WMReleaseColor(white);
274 WMReleaseColor(black);
275 } else {
276 color = WMCreateRGBColor(scr, 0x5144, 0x5555, 0x5144, True);
277 if (!color->flags.exact)
278 wwarning(_("could not allocate %s color"), _("dark gray"));
280 scr->darkGray = color;
282 return WMRetainColor(scr->darkGray);
283 #endif
284 if (!scr->darkGray)
285 return NULL;
288 unsigned short WMRedComponentOfColor(WMColor * color)
290 return color->color.red;
293 #ifdef obsolete
294 unsigned short WMGreenComponentOfColor(WMColor * color)
296 return color->color.green;
299 unsigned short WMBlueComponentOfColor(WMColor * color)
301 return color->color.blue;
304 unsigned short WMGetColorAlpha(WMColor * color)
306 return color->alpha;
309 char *WMGetColorRGBDescription(WMColor * color)
311 char *str = wmalloc(32);
313 sprintf(str, "#%02x%02x%02x", color->color.red >> 8, color->color.green >> 8, color->color.blue >> 8);
315 return str;
317 #endif
318 void WMColorSpecSet(cairo_t *cairo, WMColorSpec *color)
320 cairo_set_source_rgba(cairo,
321 color->red/255.0,
322 color->green/255.0,
323 color->blue/255.0,
324 color->alpha/255.0);
327 WMColorSpec WMBlackColorSpec()
329 WMColorSpec spec= {0, 0, 0, 255};
330 return spec;
334 WMColorSpec WMGrayColorSpec()
336 WMColorSpec spec= {0xae, 0xaa, 0xae, 0xff};
337 return spec;
340 WMColorSpec WMWhiteColorSpec()
342 WMColorSpec spec= {255, 255, 255, 255};
343 return spec;
346 WMColorSpec WMDarkGrayColorSpec()
348 WMColorSpec spec= {0x51, 0x55, 0x51, 0xff};
349 return spec;