1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "rendercolor.hh"
7 #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)
36 RenderColor::RenderColor(int screen, RGB rgb)
46 void RenderColor::create() const
48 unsigned long color = _blue | _green << 8 | _red << 16;
50 // try get a gc from the cache
51 CacheItem *item = _cache[_screen][color];
60 // allocate a color and GC from the server
61 const ScreenInfo *info = display->screenInfo(_screen);
63 XColor xcol; // convert from 0-0xff to 0-0xffff
64 xcol.red = (_red << 8) | _red;
65 xcol.green = (_green << 8) | _green;
66 xcol.blue = (_blue << 8) | _blue;
69 if (! XAllocColor(**display, info->colormap(), &xcol)) {
70 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
77 gcv.foreground = _pixel;
78 gcv.cap_style = CapProjecting;
79 _gc = XCreateGC(**display, info->rootWindow(),
80 GCForeground | GCCapStyle, &gcv);
83 // insert into the cache
84 item = new CacheItem(_gc, _pixel);
85 _cache[_screen][color] = item;
92 unsigned long RenderColor::pixel() const
94 if (!_created) create();
98 GC RenderColor::gc() const
100 if (!_created) create();
104 RenderColor::~RenderColor()
106 unsigned long color = _blue | _green << 8 | _red << 16;
109 CacheItem *item = _cache[_screen][color];
110 assert(item); // better be...
112 if (--item->count <= 0) {
113 // remove from the cache
114 XFreeGC(**display, _gc);
115 _cache[_screen][color] = 0;
119 const ScreenInfo *info = display->screenInfo(_screen);
120 XFreeColors(**display, info->colormap(), &_pixel, 1, 0);