]> Dogcows Code - chaz/openbox/blob - otk/color.hh
5f3cb95547fa6693ee43385718edaa0902f809aa
[chaz/openbox] / otk / color.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef COLOR_HH
3 #define COLOR_HH
4
5 extern "C" {
6 #include <X11/Xlib.h>
7 }
8
9 #include <map>
10 #include <string>
11
12 class BaseDisplay;
13
14 class BColor {
15 public:
16 BColor(const BaseDisplay * const _display = 0, unsigned int _screen = ~(0u));
17 BColor(int _r, int _g, int _b,
18 const BaseDisplay * const _display, unsigned int _screen = ~(0u));
19 BColor(const std::string &_name,
20 const BaseDisplay * const _display, unsigned int _screen = ~(0u));
21 ~BColor(void);
22
23 inline const std::string &name(void) const { return colorname; }
24
25 inline int red(void) const { return r; }
26 inline int green(void) const { return g; }
27 inline int blue(void) const { return b; }
28 void setRGB(int _r, int _g, int _b) {
29 deallocate();
30 r = _r;
31 g = _g;
32 b = _b;
33 }
34
35 inline const BaseDisplay *display(void) const { return dpy; }
36 inline unsigned int screen(void) const { return scrn; }
37 void setDisplay(const BaseDisplay * const _display,
38 unsigned int _screen = ~(0u));
39
40 inline bool isAllocated(void) const { return allocated; }
41
42 inline bool isValid(void) const { return r != -1 && g != -1 && b != -1; }
43
44 unsigned long pixel(void) const;
45
46 // operators
47 BColor &operator=(const BColor &c);
48 inline bool operator==(const BColor &c) const
49 { return (r == c.r && b == c.b && b == c.b); }
50 inline bool operator!=(const BColor &c) const
51 { return (! operator==(c)); }
52
53 static void cleanupColorCache(void);
54
55 private:
56 void parseColorName(void);
57 void allocate(void);
58 void deallocate(void);
59
60 bool allocated;
61 int r, g, b;
62 unsigned long p;
63 const BaseDisplay *dpy;
64 unsigned int scrn;
65 std::string colorname;
66
67 // global color allocator/deallocator
68 struct RGB {
69 const BaseDisplay* const display;
70 const unsigned int screen;
71 const int r, g, b;
72
73 RGB(void) : display(0), screen(~(0u)), r(-1), g(-1), b(-1) { }
74 RGB(const BaseDisplay * const a, const unsigned int b,
75 const int x, const int y, const int z)
76 : display(a), screen(b), r(x), g(y), b(z) {}
77 RGB(const RGB &x)
78 : display(x.display), screen(x.screen), r(x.r), g(x.g), b(x.b) {}
79
80 inline bool operator==(const RGB &x) const {
81 return display == x.display &&
82 screen == x.screen &&
83 r == x.r && g == x.g && b == x.b;
84 }
85
86 inline bool operator<(const RGB &x) const {
87 unsigned long p1, p2;
88 p1 = (screen << 24 | r << 16 | g << 8 | b) & 0x00ffffff;
89 p2 = (x.screen << 24 | x.r << 16 | x.g << 8 | x.b) & 0x00ffffff;
90 return p1 < p2;
91 }
92 };
93 struct PixelRef {
94 const unsigned long p;
95 unsigned int count;
96 inline PixelRef(void) : p(0), count(0) { }
97 inline PixelRef(const unsigned long x) : p(x), count(1) { }
98 };
99 typedef std::map<RGB,PixelRef> ColorCache;
100 typedef ColorCache::value_type ColorCacheItem;
101 static ColorCache colorcache;
102 static bool cleancache;
103 static void doCacheCleanup(void);
104 };
105
106 #endif // COLOR_HH
This page took 0.038744 seconds and 4 git commands to generate.