1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "rendercolor.hh"
7 #include "screeninfo.hh"
8 #include "rendercontrol.hh"
14 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
16 void RenderColor::initialize()
18 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
21 void RenderColor::destroy()
26 RenderColor::RenderColor(int screen, unsigned char red,
27 unsigned char green, unsigned char blue)
37 RenderColor::RenderColor(int screen, RGB rgb)
47 void RenderColor::create() const
49 unsigned long color = _blue | _green << 8 | _red << 16;
51 // try get a gc from the cache
52 CacheItem *item = _cache[_screen][color];
61 // allocate a color and GC from the server
62 const ScreenInfo *info = display->screenInfo(_screen);
64 XColor xcol; // convert from 0-0xff to 0-0xffff
65 xcol.red = (_red << 8) | _red;
66 xcol.green = (_green << 8) | _green;
67 xcol.blue = (_blue << 8) | _blue;
69 display->renderControl(_screen)->allocateColor(&xcol);
73 gcv.foreground = _pixel;
74 gcv.cap_style = CapProjecting;
75 _gc = XCreateGC(**display, info->rootWindow(),
76 GCForeground | GCCapStyle, &gcv);
79 // insert into the cache
80 item = new CacheItem(_gc, _pixel);
81 _cache[_screen][color] = item;
88 unsigned long RenderColor::pixel() const
90 if (!_created) create();
94 GC RenderColor::gc() const
96 if (!_created) create();
100 RenderColor::~RenderColor()
102 unsigned long color = _blue | _green << 8 | _red << 16;
105 CacheItem *item = _cache[_screen][color];
106 assert(item); // better be...
108 if (--item->count <= 0) {
109 // remove from the cache
110 XFreeGC(**display, _gc);
111 _cache[_screen][color] = 0;
115 const ScreenInfo *info = display->screenInfo(_screen);
116 XFreeColors(**display, info->colormap(), &_pixel, 1, 0);