Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wcolor.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
2#include "WINGsP.h"
3
882b9a8e kojima2001-07-23 20:31:32 +00004#include "wconfig.h"
5
9d2e6ef9 scottc1998-09-29 22:36:29 +00006#include <wraster.h>
7
8#define LIGHT_STIPPLE_WIDTH 4
9#define LIGHT_STIPPLE_HEIGHT 4
c0317dda dan2005-08-22 23:58:19 +000010static char LIGHT_STIPPLE_BITS[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020011 0x05, 0x0a, 0x05, 0x0a
12};
9d2e6ef9 scottc1998-09-29 22:36:29 +000013
14#define DARK_STIPPLE_WIDTH 4
15#define DARK_STIPPLE_HEIGHT 4
c0317dda dan2005-08-22 23:58:19 +000016static char DARK_STIPPLE_BITS[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020017 0x0a, 0x04, 0x0a, 0x01
18};
9d2e6ef9 scottc1998-09-29 22:36:29 +000019
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020020static WMColor *createRGBAColor(WMScreen * scr, unsigned short red,
21 unsigned short green, unsigned short blue, unsigned short alpha);
9d2e6ef9 scottc1998-09-29 22:36:29 +000022
23/*
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.
28 */
29
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020030static WMColor *findCloseColor(WMScreen * scr, unsigned short red, unsigned short green,
31 unsigned short blue, unsigned short alpha)
9d2e6ef9 scottc1998-09-29 22:36:29 +000032{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020033 WMColor *color;
34 XColor xcolor;
35 RColor rcolor;
9d2e6ef9 scottc1998-09-29 22:36:29 +000036
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020037 rcolor.red = red >> 8;
38 rcolor.green = green >> 8;
39 rcolor.blue = blue >> 8;
40 rcolor.alpha = alpha >> 8;
9d2e6ef9 scottc1998-09-29 22:36:29 +000041
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042 if (!RGetClosestXColor(scr->rcontext, &rcolor, &xcolor))
43 return NULL;
6830b057 dan2004-10-12 21:28:27 +000044
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020045 if (!XAllocColor(scr->display, scr->colormap, &xcolor))
46 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000047
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020048 color = wmalloc(sizeof(WMColor));
9d2e6ef9 scottc1998-09-29 22:36:29 +000049
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020050 color->screen = scr;
51 color->refCount = 1;
52 color->color = xcolor;
53 color->alpha = alpha;
54 color->flags.exact = 1;
55 color->gc = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000056
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020057 return color;
9d2e6ef9 scottc1998-09-29 22:36:29 +000058}
59
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020060static WMColor *createRGBAColor(WMScreen * scr, unsigned short red, unsigned short green,
61 unsigned short blue, unsigned short alpha)
9d2e6ef9 scottc1998-09-29 22:36:29 +000062{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020063 WMColor *color;
64 XColor xcolor;
65
66 xcolor.red = red;
67 xcolor.green = green;
68 xcolor.blue = blue;
69 xcolor.flags = DoRed | DoGreen | DoBlue;
70 if (!XAllocColor(scr->display, scr->colormap, &xcolor))
71 return NULL;
72
73 color = wmalloc(sizeof(WMColor));
74
75 color->screen = scr;
76 color->refCount = 1;
77 color->color = xcolor;
78 color->alpha = alpha;
79 color->flags.exact = 1;
80 color->gc = NULL;
81
82 return color;
9d2e6ef9 scottc1998-09-29 22:36:29 +000083}
84
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020085WMColor *WMCreateRGBColor(WMScreen * scr, unsigned short red, unsigned short green,
86 unsigned short blue, Bool exact)
9d2e6ef9 scottc1998-09-29 22:36:29 +000087{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088 WMColor *color = NULL;
a2b404b5 dan2002-10-08 08:26:06 +000089
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020090 if (!exact || !(color = createRGBAColor(scr, red, green, blue, 0xffff))) {
91 color = findCloseColor(scr, red, green, blue, 0xffff);
92 }
93 if (!color)
94 color = WMBlackColor(scr);
a2b404b5 dan2002-10-08 08:26:06 +000095
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020096 return color;
a2b404b5 dan2002-10-08 08:26:06 +000097}
98
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099RColor WMGetRColorFromColor(WMColor * color)
f274afdd kojima2004-10-25 00:07:51 +0000100{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200101 RColor rcolor;
f274afdd kojima2004-10-25 00:07:51 +0000102
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200103 rcolor.red = color->color.red >> 8;
104 rcolor.green = color->color.green >> 8;
105 rcolor.blue = color->color.blue >> 8;
106 rcolor.alpha = color->alpha >> 8;
107
108 return rcolor;
109}
f274afdd kojima2004-10-25 00:07:51 +0000110
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200111WMColor *WMCreateRGBAColor(WMScreen * scr, unsigned short red, unsigned short green,
112 unsigned short blue, unsigned short alpha, Bool exact)
a2b404b5 dan2002-10-08 08:26:06 +0000113{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200114 WMColor *color = NULL;
6830b057 dan2004-10-12 21:28:27 +0000115
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200116 if (!exact || !(color = createRGBAColor(scr, red, green, blue, alpha))) {
117 color = findCloseColor(scr, red, green, blue, alpha);
118 }
119 if (!color)
120 color = WMBlackColor(scr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000121
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200122 return color;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000123}
124
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200125WMColor *WMCreateNamedColor(WMScreen * scr, char *name, Bool exact)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000126{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200127 WMColor *color;
128 XColor xcolor;
6830b057 dan2004-10-12 21:28:27 +0000129
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200130 if (!XParseColor(scr->display, scr->colormap, name, &xcolor))
131 return NULL;
088c0ac2 dan1999-03-09 14:58:01 +0000132
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200133 if (scr->visual->class == TrueColor)
134 exact = True;
088c0ac2 dan1999-03-09 14:58:01 +0000135
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200136 if (!exact || !(color = createRGBAColor(scr, xcolor.red, xcolor.green, xcolor.blue, 0xffff))) {
137 color = findCloseColor(scr, xcolor.red, xcolor.green, xcolor.blue, 0xffff);
138 }
139 return color;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000140}
141
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200142WMColor *WMRetainColor(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000143{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200144 assert(color != NULL);
6830b057 dan2004-10-12 21:28:27 +0000145
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200146 color->refCount++;
6830b057 dan2004-10-12 21:28:27 +0000147
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200148 return color;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000149}
150
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200151void WMReleaseColor(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000152{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200153 color->refCount--;
154
155 if (color->refCount < 1) {
156 XFreeColors(color->screen->display, color->screen->colormap, &(color->color.pixel), 1, 0);
157 if (color->gc)
158 XFreeGC(color->screen->display, color->gc);
159 wfree(color);
160 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000161}
162
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163void WMSetColorAlpha(WMColor * color, unsigned short alpha)
a2b404b5 dan2002-10-08 08:26:06 +0000164{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165 color->alpha = alpha;
a2b404b5 dan2002-10-08 08:26:06 +0000166}
167
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200168void WMPaintColorSwatch(WMColor * color, Drawable d, int x, int y, unsigned int width, unsigned int height)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000169{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170 XFillRectangle(color->screen->display, d, WMColorGC(color), x, y, width, height);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171}
172
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200173WMPixel WMColorPixel(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000174{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 return color->color.pixel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000176}
177
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200178GC WMColorGC(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000179{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200180 if (!color->gc) {
181 XGCValues gcv;
182 WMScreen *scr = color->screen;
088c0ac2 dan1999-03-09 14:58:01 +0000183
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200184 gcv.foreground = color->color.pixel;
185 gcv.graphics_exposures = False;
186 color->gc = XCreateGC(scr->display, scr->rcontext->drawable,
187 GCForeground | GCGraphicsExposures, &gcv);
188 }
088c0ac2 dan1999-03-09 14:58:01 +0000189
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200190 return color->gc;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000191}
192
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200193void WMSetColorInGC(WMColor * color, GC gc)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000194{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200195 XSetForeground(color->screen->display, gc, color->color.pixel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000196}
197
9d2e6ef9 scottc1998-09-29 22:36:29 +0000198/* "system" colors */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199WMColor *WMWhiteColor(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000200{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200201 if (!scr->white) {
202 scr->white = WMCreateRGBColor(scr, 0xffff, 0xffff, 0xffff, True);
203 if (!scr->white->flags.exact)
204 wwarning(_("could not allocate %s color"), _("white"));
205 }
206 return WMRetainColor(scr->white);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000207}
208
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200209WMColor *WMBlackColor(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000210{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200211 if (!scr->black) {
212 scr->black = WMCreateRGBColor(scr, 0, 0, 0, True);
213 if (!scr->black->flags.exact)
214 wwarning(_("could not allocate %s color"), _("black"));
215 }
216 return WMRetainColor(scr->black);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000217}
218
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200219WMColor *WMGrayColor(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000220{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200221 if (!scr->gray) {
222 WMColor *color;
223
224 if (scr->depth == 1) {
225 Pixmap stipple;
226 WMColor *white = WMWhiteColor(scr);
227 WMColor *black = WMBlackColor(scr);
228 XGCValues gcv;
229
230 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
231 LIGHT_STIPPLE_BITS, LIGHT_STIPPLE_WIDTH,
232 LIGHT_STIPPLE_HEIGHT);
233
234 color = createRGBAColor(scr, 0xffff, 0xffff, 0xffff, 0xffff);
235
236 gcv.foreground = white->color.pixel;
237 gcv.background = black->color.pixel;
238 gcv.fill_style = FillStippled;
239 gcv.stipple = stipple;
240 color->gc = XCreateGC(scr->display, W_DRAWABLE(scr), GCForeground
241 | GCBackground | GCStipple | GCFillStyle
242 | GCGraphicsExposures, &gcv);
243
244 XFreePixmap(scr->display, stipple);
245 WMReleaseColor(white);
246 WMReleaseColor(black);
247 } else {
248 color = WMCreateRGBColor(scr, 0xaeba, 0xaaaa, 0xaeba, True);
249 if (!color->flags.exact)
250 wwarning(_("could not allocate %s color"), _("gray"));
251 }
252 scr->gray = color;
253 }
254 return WMRetainColor(scr->gray);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000255}
256
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200257WMColor *WMDarkGrayColor(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000258{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200259 if (!scr->darkGray) {
260 WMColor *color;
261
262 if (scr->depth == 1) {
263 Pixmap stipple;
264 WMColor *white = WMWhiteColor(scr);
265 WMColor *black = WMBlackColor(scr);
266 XGCValues gcv;
267
268 stipple = XCreateBitmapFromData(scr->display, W_DRAWABLE(scr),
269 DARK_STIPPLE_BITS, DARK_STIPPLE_WIDTH,
270 DARK_STIPPLE_HEIGHT);
271
272 color = createRGBAColor(scr, 0, 0, 0, 0xffff);
273
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);
281
282 XFreePixmap(scr->display, stipple);
283 WMReleaseColor(white);
284 WMReleaseColor(black);
285 } else {
286 color = WMCreateRGBColor(scr, 0x5144, 0x5555, 0x5144, True);
287 if (!color->flags.exact)
288 wwarning(_("could not allocate %s color"), _("dark gray"));
289 }
290 scr->darkGray = color;
291 }
292 return WMRetainColor(scr->darkGray);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000293}
294
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200295unsigned short WMRedComponentOfColor(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000296{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200297 return color->color.red;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000298}
299
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200300unsigned short WMGreenComponentOfColor(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000301{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200302 return color->color.green;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000303}
304
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200305unsigned short WMBlueComponentOfColor(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000306{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200307 return color->color.blue;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000308}
309
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200310unsigned short WMGetColorAlpha(WMColor * color)
1e922744 dan2002-10-16 04:05:45 +0000311{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200312 return color->alpha;
1e922744 dan2002-10-16 04:05:45 +0000313}
314
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200315char *WMGetColorRGBDescription(WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000316{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200317 char *str = wmalloc(32);
6830b057 dan2004-10-12 21:28:27 +0000318
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200319 sprintf(str, "#%02x%02x%02x", color->color.red >> 8, color->color.green >> 8, color->color.blue >> 8);
6830b057 dan2004-10-12 21:28:27 +0000320
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200321 return str;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000322}