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 printf("CREATE COLOR: %lx\n", color);
58 // try get a gc from the cache
59 CacheItem *item = _cache[_screen][color];
68 // allocate a color and GC from the server
69 const ScreenInfo *info = display->screenInfo(_screen);
71 XColor xcol; // convert from 0-0xff to 0-0xffff
72 xcol.red = _red; xcol.red |= xcol.red << 8;
73 xcol.green = _green; xcol.green |= xcol.green << 8;
74 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
77 if (! XAllocColor(**display, info->colormap(), &xcol)) {
78 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
84 gcv.foreground = _pixel;
85 gcv.cap_style = CapProjecting;
86 _gc = XCreateGC(**display, info->rootWindow(),
87 GCForeground | GCCapStyle, &gcv);
90 // insert into the cache
91 item = new CacheItem(_gc, _pixel);
92 _cache[_screen][color] = item;
97 RenderColor::~RenderColor()
99 unsigned long color = _blue | _green << 8 | _red << 16;
101 CacheItem *item = _cache[_screen][color];
102 assert(item); // it better be in the cache ...
104 printf("DESTROY COLOR: %lx %d\n", color, item->count);
106 if (--item->count <= 0) {
107 // remove from the cache
108 XFreeGC(**display, _gc);
109 _cache[_screen][color] = 0;