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
26 * make findCloseColor() find the closest color in the RContext pallette
27 * or in the other colors allocated by WINGs.
31 findCloseColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
32 unsigned short blue
, unsigned short alpha
)
39 rcolor
.green
= green
>>8;
40 rcolor
.blue
= blue
>>8;
41 rcolor
.alpha
= alpha
>>8;
43 if (!RGetClosestXColor(scr
->rcontext
, &rcolor
, &xcolor
))
46 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
49 color
= wmalloc(sizeof(WMColor
));
53 color
->color
= xcolor
;
55 color
->flags
.exact
= 1;
64 createRGBAColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
65 unsigned short blue
, unsigned short alpha
)
73 xcolor
.flags
= DoRed
|DoGreen
|DoBlue
;
74 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
77 color
= wmalloc(sizeof(WMColor
));
81 color
->color
= xcolor
;
83 color
->flags
.exact
= 1;
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);
100 color
= WMBlackColor(scr
);
106 RColor
WMGetRColorFromColor(WMColor
*color
)
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;
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
);
129 color
= WMBlackColor(scr
);
136 WMCreateNamedColor(WMScreen
*scr
, char *name
, Bool exact
)
141 if (!XParseColor(scr
->display
, scr
->colormap
, name
, &xcolor
))
144 if (scr
->visual
->class == TrueColor
)
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);
157 WMRetainColor(WMColor
*color
)
168 WMReleaseColor(WMColor
*color
)
172 if (color
->refCount
< 1) {
173 XFreeColors(color
->screen
->display
, color
->screen
->colormap
,
174 &(color
->color
.pixel
), 1, 0);
176 XFreeGC(color
->screen
->display
, color
->gc
);
183 WMSetColorAlpha(WMColor
*color
, unsigned short alpha
)
185 color
->alpha
= alpha
;
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
);
199 WMColorPixel(WMColor
*color
)
201 return color
->color
.pixel
;
206 WMColorGC(WMColor
*color
)
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
);
223 WMSetColorInGC(WMColor
*color
, GC gc
)
225 XSetForeground(color
->screen
->display
, gc
, color
->color
.pixel
);
230 /* "system" colors */
232 WMWhiteColor(WMScreen
*scr
)
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
);
244 WMBlackColor(WMScreen
*scr
)
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
);
257 WMGrayColor(WMScreen
*scr
)
262 if (scr
->depth
== 1) {
264 WMColor
*white
= WMWhiteColor(scr
);
265 WMColor
*black
= WMBlackColor(scr
);
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
);
286 color
= WMCreateRGBColor(scr
, 0xaeba, 0xaaaa, 0xaeba, True
);
287 if (!color
->flags
.exact
)
288 wwarning(_("could not allocate %s color"), _("gray"));
292 return WMRetainColor(scr
->gray
);
298 WMDarkGrayColor(WMScreen
*scr
)
300 if (!scr
->darkGray
) {
303 if (scr
->depth
== 1) {
305 WMColor
*white
= WMWhiteColor(scr
);
306 WMColor
*black
= WMBlackColor(scr
);
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
);
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
);
338 WMRedComponentOfColor(WMColor
*color
)
340 return color
->color
.red
;
345 WMGreenComponentOfColor(WMColor
*color
)
347 return color
->color
.green
;
352 WMBlueComponentOfColor(WMColor
*color
)
354 return color
->color
.blue
;
359 WMGetColorAlpha(WMColor
*color
)
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);