6 #define LIGHT_STIPPLE_WIDTH 4
7 #define LIGHT_STIPPLE_HEIGHT 4
8 static unsigned char LIGHT_STIPPLE_BITS
[] = {
9 0x05, 0x0a, 0x05, 0x0a};
11 #define DARK_STIPPLE_WIDTH 4
12 #define DARK_STIPPLE_HEIGHT 4
13 static unsigned char DARK_STIPPLE_BITS
[] = {
14 0x0a, 0x04, 0x0a, 0x01};
17 static WMColor
*createRGBColor(WMScreen
*scr
, unsigned short red
,
18 unsigned short green
, unsigned short blue
);
21 * TODO: make the color creation code return the same WMColor for the
23 * make findCloseColor() find the closest color in the RContext pallette
24 * or in the other colors allocated by WINGs.
28 findCloseColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
36 rcolor
.green
= green
>>8;
37 rcolor
.blue
= blue
>>8;
39 if (!RGetClosestXColor(scr
->rcontext
, &rcolor
, &xcolor
))
42 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
45 color
= wmalloc(sizeof(WMColor
));
49 color
->color
= xcolor
;
50 color
->flags
.exact
= 1;
59 createRGBColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
68 xcolor
.flags
= DoRed
|DoGreen
|DoBlue
;
69 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
72 color
= wmalloc(sizeof(WMColor
));
76 color
->color
= xcolor
;
77 color
->flags
.exact
= 1;
86 WMCreateRGBColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
87 unsigned short blue
, Bool exact
)
89 WMColor
*color
= NULL
;
91 if (!exact
|| !(color
=createRGBColor(scr
, red
, green
, blue
))) {
92 color
= findCloseColor(scr
, red
, green
, blue
);
95 color
= WMBlackColor(scr
);
102 WMCreateNamedColor(WMScreen
*scr
, char *name
, Bool exact
)
107 if (!XParseColor(scr
->display
, scr
->colormap
, name
, &xcolor
))
110 if (scr
->visual
->class == TrueColor
)
113 if (!exact
|| !(color
=createRGBColor(scr
, xcolor
.red
, xcolor
.green
,
115 color
= findCloseColor(scr
, xcolor
.red
, xcolor
.green
, xcolor
.blue
);
123 WMRetainColor(WMColor
*color
)
134 WMReleaseColor(WMColor
*color
)
138 if (color
->refCount
< 1) {
139 XFreeColors(color
->screen
->display
, color
->screen
->colormap
,
140 &(color
->color
.pixel
), 1, 0);
142 XFreeGC(color
->screen
->display
, color
->gc
);
149 WMPaintColorSwatch(WMColor
*color
, Drawable d
, int x
, int y
,
150 unsigned int width
, unsigned int height
)
152 XFillRectangle(color
->screen
->display
, d
, WMColorGC(color
),
153 x
, y
, width
, height
);
158 WMColorPixel(WMColor
*color
)
160 return color
->color
.pixel
;
165 WMColorGC(WMColor
*color
)
169 WMScreen
*scr
= color
->screen
;
171 gcv
.foreground
= color
->color
.pixel
;
172 gcv
.graphics_exposures
= False
;
173 color
->gc
= XCreateGC(scr
->display
, scr
->rcontext
->drawable
,
174 GCForeground
|GCGraphicsExposures
, &gcv
);
182 WMSetColorInGC(WMColor
*color
, GC gc
)
184 XSetForeground(color
->screen
->display
, gc
, color
->color
.pixel
);
189 /* "system" colors */
191 WMWhiteColor(WMScreen
*scr
)
194 scr
->white
= WMCreateRGBColor(scr
, 0xffff, 0xffff, 0xffff, True
);
195 if (!scr
->white
->flags
.exact
)
196 wwarning("could not allocate %s color", "white");
198 return WMRetainColor(scr
->white
);
204 WMBlackColor(WMScreen
*scr
)
207 scr
->black
= WMCreateRGBColor(scr
, 0, 0, 0, True
);
208 if (!scr
->black
->flags
.exact
)
209 wwarning("could not allocate %s color", "black");
211 return WMRetainColor(scr
->black
);
217 WMGrayColor(WMScreen
*scr
)
222 if (scr
->depth
== 1) {
224 WMColor
*white
= WMWhiteColor(scr
);
225 WMColor
*black
= WMBlackColor(scr
);
228 stipple
= XCreateBitmapFromData(scr
->display
, W_DRAWABLE(scr
),
229 LIGHT_STIPPLE_BITS
, LIGHT_STIPPLE_WIDTH
,
230 LIGHT_STIPPLE_HEIGHT
);
232 color
= createRGBColor(scr
, 0xffff, 0xffff, 0xffff);
234 gcv
.foreground
= white
->color
.pixel
;
235 gcv
.background
= black
->color
.pixel
;
236 gcv
.fill_style
= FillStippled
;
237 gcv
.stipple
= stipple
;
238 color
->gc
= XCreateGC(scr
->display
, W_DRAWABLE(scr
), GCForeground
239 |GCBackground
|GCStipple
|GCFillStyle
240 |GCGraphicsExposures
, &gcv
);
242 XFreePixmap(scr
->display
, stipple
);
243 WMReleaseColor(white
);
244 WMReleaseColor(black
);
246 color
= WMCreateRGBColor(scr
, 0xaeba, 0xaaaa, 0xaeba, True
);
247 if (!color
->flags
.exact
)
248 wwarning("could not allocate %s color", "gray");
252 return WMRetainColor(scr
->gray
);
258 WMDarkGrayColor(WMScreen
*scr
)
260 if (!scr
->darkGray
) {
263 if (scr
->depth
== 1) {
265 WMColor
*white
= WMWhiteColor(scr
);
266 WMColor
*black
= WMBlackColor(scr
);
269 stipple
= XCreateBitmapFromData(scr
->display
, W_DRAWABLE(scr
),
270 DARK_STIPPLE_BITS
, DARK_STIPPLE_WIDTH
,
271 DARK_STIPPLE_HEIGHT
);
273 color
= createRGBColor(scr
, 0, 0, 0);
275 gcv
.foreground
= white
->color
.pixel
;
276 gcv
.background
= black
->color
.pixel
;
277 gcv
.fill_style
= FillStippled
;
278 gcv
.stipple
= stipple
;
279 color
->gc
= XCreateGC(scr
->display
, W_DRAWABLE(scr
), GCForeground
280 |GCBackground
|GCStipple
|GCFillStyle
281 |GCGraphicsExposures
, &gcv
);
283 XFreePixmap(scr
->display
, stipple
);
284 WMReleaseColor(white
);
285 WMReleaseColor(black
);
287 color
= WMCreateRGBColor(scr
, 0x5144, 0x5555, 0x5144, True
);
288 if (!color
->flags
.exact
)
289 wwarning("could not allocate %s color", "dark gray");
291 scr
->darkGray
= color
;
293 return WMRetainColor(scr
->darkGray
);
298 WMRedComponentOfColor(WMColor
*color
)
300 return color
->color
.red
;
305 WMGreenComponentOfColor(WMColor
*color
)
307 return color
->color
.green
;
312 WMBlueComponentOfColor(WMColor
*color
)
314 return color
->color
.blue
;
319 WMGetColorRGBDescription(WMColor
*color
)
321 char *str
= wmalloc(32);
323 sprintf(str
, "#%02x%02x%02x", color
->color
.red
>>8, color
->color
.green
>>8,
324 color
->color
.blue
>>8);