1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
7 #include "rendercolor.hh"
9 #include "screeninfo.hh"
13 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
15 void RenderColor::initialize()
17 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
20 void RenderColor::destroy()
25 RenderColor::RenderColor(int screen, unsigned char red,
26 unsigned char green, unsigned char blue)
33 unsigned long color = _blue | _green << 8 | _red << 16;
35 // try get a gc from the cache
36 CacheItem *item = _cache[_screen][color];
44 // allocate a color and GC from the server
45 const ScreenInfo *info = display->screenInfo(_screen);
47 XColor xcol; // convert from 0-0xff to 0-0xffff
48 xcol.red = _red; xcol.red |= xcol.red << 8;
49 xcol.green = _green; xcol.green |= xcol.green << 8;
50 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
53 if (! XAllocColor(**display, info->colormap(), &xcol)) {
54 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
59 gcv.foreground = xcol.pixel;
60 gcv.cap_style = CapProjecting;
61 _gc = XCreateGC(**display, info->rootWindow(),
62 GCForeground | GCCapStyle, &gcv);
65 // insert into the cache
66 _cache[_screen][color] = new CacheItem(_gc);
70 RenderColor::~RenderColor()
72 unsigned long color = _blue | _green << 8 | _red << 16;
74 CacheItem *item = _cache[_screen][color];
75 assert(item); // it better be in the cache ...
77 if (--item->count <= 0) {
78 // remove from the cache
79 XFreeGC(**display, _gc);
80 _cache[_screen][color] = 0;