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)
35 RenderColor::RenderColor(int screen, RGB rgb)
44 void RenderColor::create() const
46 unsigned long color = _blue | _green << 8 | _red << 16;
48 // try get a gc from the cache
49 CacheItem *item = _cache[_screen][color];
58 // allocate a color and GC from the server
59 const ScreenInfo *info = display->screenInfo(_screen);
61 XColor xcol; // convert from 0-0xff to 0-0xffff
62 xcol.red = (_red << 8) | _red;
63 xcol.green = (_green << 8) | _green;
64 xcol.blue = (_blue << 8) | _blue;
67 if (! XAllocColor(**display, info->colormap(), &xcol)) {
68 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
74 gcv.foreground = _pixel;
75 gcv.cap_style = CapProjecting;
76 _gc = XCreateGC(**display, info->rootWindow(),
77 GCForeground | GCCapStyle, &gcv);
80 // insert into the cache
81 item = new CacheItem(_gc, _pixel);
82 _cache[_screen][color] = item;
89 unsigned long RenderColor::pixel() const
91 if (!_allocated) create();
95 GC RenderColor::gc() const
97 if (!_allocated) create();
101 RenderColor::~RenderColor()
103 unsigned long color = _blue | _green << 8 | _red << 16;
106 CacheItem *item = _cache[_screen][color];
107 assert(item); // better be...
109 if (--item->count <= 0) {
110 // remove from the cache
111 XFreeGC(**display, _gc);
112 _cache[_screen][color] = 0;
115 const ScreenInfo *info = display->screenInfo(_screen);
116 XFreeColors(**display, info->colormap(), &_pixel, 1, 0);