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"
19 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
21 void RenderColor::initialize()
23 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
26 void RenderColor::destroy()
31 RenderColor::RenderColor(int screen, unsigned char red,
32 unsigned char green, unsigned char blue)
42 RenderColor::RenderColor(int screen, RGB rgb)
52 void RenderColor::create()
54 unsigned long color = _blue | _green << 8 | _red << 16;
56 // try get a gc from the cache
57 CacheItem *item = _cache[_screen][color];
66 // allocate a color and GC from the server
67 const ScreenInfo *info = display->screenInfo(_screen);
69 XColor xcol; // convert from 0-0xff to 0-0xffff
70 xcol.red = _red; xcol.red |= xcol.red << 8;
71 xcol.green = _green; xcol.green |= xcol.green << 8;
72 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
75 if (! XAllocColor(**display, info->colormap(), &xcol)) {
76 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
82 gcv.foreground = _pixel;
83 gcv.cap_style = CapProjecting;
84 _gc = XCreateGC(**display, info->rootWindow(),
85 GCForeground | GCCapStyle, &gcv);
88 // insert into the cache
89 item = new CacheItem(_gc, _pixel);
90 _cache[_screen][color] = item;
95 RenderColor::~RenderColor()
97 unsigned long color = _blue | _green << 8 | _red << 16;
99 CacheItem *item = _cache[_screen][color];
100 assert(item); // it better be in the cache ...
102 if (--item->count <= 0) {
103 // remove from the cache
104 XFreeGC(**display, _gc);
105 _cache[_screen][color] = 0;