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
*createRGBColor(WMScreen
*scr
, unsigned short red
,
20 unsigned short green
, unsigned short blue
);
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.
30 findCloseColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
38 rcolor
.green
= green
>>8;
39 rcolor
.blue
= blue
>>8;
41 if (!RGetClosestXColor(scr
->rcontext
, &rcolor
, &xcolor
))
44 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
47 color
= wmalloc(sizeof(WMColor
));
51 color
->color
= xcolor
;
52 color
->flags
.exact
= 1;
61 createRGBColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
70 xcolor
.flags
= DoRed
|DoGreen
|DoBlue
;
71 if (!XAllocColor(scr
->display
, scr
->colormap
, &xcolor
))
74 color
= wmalloc(sizeof(WMColor
));
78 color
->color
= xcolor
;
79 color
->flags
.exact
= 1;
88 WMCreateRGBColor(WMScreen
*scr
, unsigned short red
, unsigned short green
,
89 unsigned short blue
, Bool exact
)
91 WMColor
*color
= NULL
;
93 if (!exact
|| !(color
=createRGBColor(scr
, red
, green
, blue
))) {
94 color
= findCloseColor(scr
, red
, green
, blue
);
97 color
= WMBlackColor(scr
);
104 WMCreateNamedColor(WMScreen
*scr
, char *name
, Bool exact
)
109 if (!XParseColor(scr
->display
, scr
->colormap
, name
, &xcolor
))
112 if (scr
->visual
->class == TrueColor
)
115 if (!exact
|| !(color
=createRGBColor(scr
, xcolor
.red
, xcolor
.green
,
117 color
= findCloseColor(scr
, xcolor
.red
, xcolor
.green
, xcolor
.blue
);
125 WMRetainColor(WMColor
*color
)
136 WMReleaseColor(WMColor
*color
)
140 if (color
->refCount
< 1) {
141 XFreeColors(color
->screen
->display
, color
->screen
->colormap
,
142 &(color
->color
.pixel
), 1, 0);
144 XFreeGC(color
->screen
->display
, color
->gc
);
151 WMPaintColorSwatch(WMColor
*color
, Drawable d
, int x
, int y
,
152 unsigned int width
, unsigned int height
)
154 XFillRectangle(color
->screen
->display
, d
, WMColorGC(color
),
155 x
, y
, width
, height
);
160 WMColorPixel(WMColor
*color
)
162 return color
->color
.pixel
;
167 WMColorGC(WMColor
*color
)
171 WMScreen
*scr
= color
->screen
;
173 gcv
.foreground
= color
->color
.pixel
;
174 gcv
.graphics_exposures
= False
;
175 color
->gc
= XCreateGC(scr
->display
, scr
->rcontext
->drawable
,
176 GCForeground
|GCGraphicsExposures
, &gcv
);
184 WMSetColorInGC(WMColor
*color
, GC gc
)
186 XSetForeground(color
->screen
->display
, gc
, color
->color
.pixel
);
191 /* "system" colors */
193 WMWhiteColor(WMScreen
*scr
)
196 scr
->white
= WMCreateRGBColor(scr
, 0xffff, 0xffff, 0xffff, True
);
197 if (!scr
->white
->flags
.exact
)
198 wwarning(_("could not allocate %s color"), _("white"));
200 return WMRetainColor(scr
->white
);
206 WMBlackColor(WMScreen
*scr
)
209 scr
->black
= WMCreateRGBColor(scr
, 0, 0, 0, True
);
210 if (!scr
->black
->flags
.exact
)
211 wwarning(_("could not allocate %s color"), _("black"));
213 return WMRetainColor(scr
->black
);
219 WMGrayColor(WMScreen
*scr
)
224 if (scr
->depth
== 1) {
226 WMColor
*white
= WMWhiteColor(scr
);
227 WMColor
*black
= WMBlackColor(scr
);
230 stipple
= XCreateBitmapFromData(scr
->display
, W_DRAWABLE(scr
),
231 LIGHT_STIPPLE_BITS
, LIGHT_STIPPLE_WIDTH
,
232 LIGHT_STIPPLE_HEIGHT
);
234 color
= createRGBColor(scr
, 0xffff, 0xffff, 0xffff);
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
);
244 XFreePixmap(scr
->display
, stipple
);
245 WMReleaseColor(white
);
246 WMReleaseColor(black
);
248 color
= WMCreateRGBColor(scr
, 0xaeba, 0xaaaa, 0xaeba, True
);
249 if (!color
->flags
.exact
)
250 wwarning(_("could not allocate %s color"), _("gray"));
254 return WMRetainColor(scr
->gray
);
260 WMDarkGrayColor(WMScreen
*scr
)
262 if (!scr
->darkGray
) {
265 if (scr
->depth
== 1) {
267 WMColor
*white
= WMWhiteColor(scr
);
268 WMColor
*black
= WMBlackColor(scr
);
271 stipple
= XCreateBitmapFromData(scr
->display
, W_DRAWABLE(scr
),
272 DARK_STIPPLE_BITS
, DARK_STIPPLE_WIDTH
,
273 DARK_STIPPLE_HEIGHT
);
275 color
= createRGBColor(scr
, 0, 0, 0);
277 gcv
.foreground
= white
->color
.pixel
;
278 gcv
.background
= black
->color
.pixel
;
279 gcv
.fill_style
= FillStippled
;
280 gcv
.stipple
= stipple
;
281 color
->gc
= XCreateGC(scr
->display
, W_DRAWABLE(scr
), GCForeground
282 |GCBackground
|GCStipple
|GCFillStyle
283 |GCGraphicsExposures
, &gcv
);
285 XFreePixmap(scr
->display
, stipple
);
286 WMReleaseColor(white
);
287 WMReleaseColor(black
);
289 color
= WMCreateRGBColor(scr
, 0x5144, 0x5555, 0x5144, True
);
290 if (!color
->flags
.exact
)
291 wwarning(_("could not allocate %s color"), _("dark gray"));
293 scr
->darkGray
= color
;
295 return WMRetainColor(scr
->darkGray
);
300 WMRedComponentOfColor(WMColor
*color
)
302 return color
->color
.red
;
307 WMGreenComponentOfColor(WMColor
*color
)
309 return color
->color
.green
;
314 WMBlueComponentOfColor(WMColor
*color
)
316 return color
->color
.blue
;
321 WMGetColorRGBDescription(WMColor
*color
)
323 char *str
= wmalloc(32);
325 sprintf(str
, "#%02x%02x%02x", color
->color
.red
>>8, color
->color
.green
>>8,
326 color
->color
.blue
>>8);