]> icculus.org git repositories - mikachu/openbox.git/blob - otk/color.hh
add masks
[mikachu/openbox.git] / 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 namespace otk {
13
14 class BColor {
15 public:
16   BColor(unsigned int _screen = ~(0u));
17   BColor(int _r, int _g, int _b, unsigned int _screen = ~(0u));
18   BColor(const std::string &_name, unsigned int _screen = ~(0u));
19   ~BColor(void);
20
21   inline const std::string &name(void) const { return colorname; }
22
23   inline int   red(void) const { return r; }
24   inline int green(void) const { return g; }
25   inline int  blue(void) const { return b; }
26   void setRGB(int _r, int _g, int _b) {
27     deallocate();
28     r = _r;
29     g = _g;
30     b = _b;
31   }
32
33   inline unsigned int screen(void) const { return scrn; }
34   void setScreen(unsigned int _screen = ~(0u));
35
36   inline bool isAllocated(void) const { return allocated; }
37
38   inline bool isValid(void) const { return r != -1 && g != -1 && b != -1; }
39
40   unsigned long pixel(void) const;
41
42   // operators
43 #ifndef SWIG
44   BColor &operator=(const BColor &c);
45 #endif
46   inline bool operator==(const BColor &c) const
47   { return (r == c.r && b == c.b && b == c.b); }
48   inline bool operator!=(const BColor &c) const
49   { return (! operator==(c)); }
50
51   static void cleanupColorCache(void);
52
53 private:
54   void parseColorName(void);
55   void allocate(void);
56   void deallocate(void);
57
58   bool allocated;
59   int r, g, b;
60   unsigned long p;
61   unsigned int scrn;
62   std::string colorname;
63
64   // global color allocator/deallocator
65   struct RGB {
66     const int screen;
67     const int r, g, b;
68
69     RGB(void) : screen(~(0u)), r(-1), g(-1), b(-1) { }
70     RGB(const int b, const int x, const int y, const int z)
71       : screen(b), r(x), g(y), b(z) {}
72     RGB(const RGB &x)
73       : screen(x.screen), r(x.r), g(x.g), b(x.b) {}
74
75     inline bool operator==(const RGB &x) const {
76       return screen == x.screen &&
77              r == x.r && g == x.g && b == x.b;
78     }
79
80     inline bool operator<(const RGB &x) const {
81       unsigned long p1, p2;
82       p1 = (screen << 24 | r << 16 | g << 8 | b) & 0x00ffffff;
83       p2 = (x.screen << 24 | x.r << 16 | x.g << 8 | x.b) & 0x00ffffff;
84       return p1 < p2;
85     }
86   };
87   struct PixelRef {
88     const unsigned long p;
89     unsigned int count;
90     inline PixelRef(void) : p(0), count(0) { }
91     inline PixelRef(const unsigned long x) : p(x), count(1) { }
92   };
93   typedef std::map<RGB,PixelRef> ColorCache;
94   typedef ColorCache::value_type ColorCacheItem;
95   static ColorCache colorcache;
96   static bool cleancache;
97   static void doCacheCleanup(void);
98 };
99
100 }
101
102 #endif // COLOR_HH