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
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
)
44 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
45 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
48 color
= wmalloc(sizeof(WMColor
));
52 color
->color
= xcolor
;
54 color
->flags
.exact
= 1;
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);
69 color
= WMBlackColor(scr
);
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);
82 color
= WMBlackColor(scr
);
88 void WMGetComponentsFromColor(WMColor
*color
, unsigned short *r
, unsigned short *g
, unsigned short *b
, unsigned short *a
)
91 *g
= color
->color
.green
;
92 *b
= color
->color
.blue
;
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
);
105 color
= WMBlackColor(scr
);
110 WMColor
*WMCreateNamedColor(WMScreen
* scr
, char *name
, Bool exact
)
115 if (!XParseColor(scr
->display
, scr
->colormap
, name
, &xcolor
))
118 if (scr
->visual
->class == TrueColor
)
121 if (!exact
|| !(color
= createRGBAColor(scr
, xcolor
.red
, xcolor
.green
, xcolor
.blue
, 0xffff))) {
122 color
= findCloseColor(scr
, xcolor
.red
, xcolor
.green
, xcolor
.blue
, 0xffff);
127 WMColor
*WMRetainColor(WMColor
* color
)
129 assert(color
!= NULL
);
136 void WMReleaseColor(WMColor
* color
)
140 if (color
->refCount
< 1) {
141 XFreeColors(color
->screen
->display
, color
->screen
->colormap
, &(color
->color
.pixel
), 1, 0);
143 XFreeGC(color
->screen
->display
, color
->gc
);
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
)
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
);
179 void WMSetColorInGC(WMColor
* color
, GC gc
)
182 XSetForeground(color
->screen
->display
, gc
, color
->color
.pixel
);
186 /* "system" colors */
187 WMColor
*WMWhiteColor(WMScreen
* scr
)
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
);
198 WMColor
*WMBlackColor(WMScreen
* scr
)
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
);
208 WMColor
*WMGrayColor(WMScreen
* scr
)
213 if (scr
->depth
== 1) {
215 WMColor
*white
= WMWhiteColor(scr
);
216 WMColor
*black
= WMBlackColor(scr
);
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
);
237 color
= WMCreateRGBColor(scr
, 0xaeba, 0xaaaa, 0xaeba, True
);
238 if (!color
->flags
.exact
)
239 wwarning(_("could not allocate %s color"), _("gray"));
243 return WMRetainColor(scr
->gray
);
246 WMColor
*WMDarkGrayColor(WMScreen
* scr
)
249 if (!scr
->darkGray
) {
252 if (scr
->depth
== 1) {
254 WMColor
*white
= WMWhiteColor(scr
);
255 WMColor
*black
= WMBlackColor(scr
);
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
);
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
);
288 unsigned short WMRedComponentOfColor(WMColor
* color
)
290 return color
->color
.red
;
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
)
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);
318 void WMColorSpecSet(cairo_t
*cairo
, WMColorSpec
*color
)
320 cairo_set_source_rgba(cairo
,
327 WMColorSpec
WMBlackColorSpec()
329 WMColorSpec spec
= {0, 0, 0, 255};
334 WMColorSpec
WMGrayColorSpec()
336 WMColorSpec spec
= {0xae, 0xaa, 0xae, 0xff};
340 WMColorSpec
WMWhiteColorSpec()
342 WMColorSpec spec
= {255, 255, 255, 255};
346 WMColorSpec
WMDarkGrayColorSpec()
348 WMColorSpec spec
= {0x51, 0x55, 0x51, 0xff};