]> icculus.org git repositories - mikachu/openbox.git/blob - otk/color.hh
add --copy
[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   BColor &operator=(const BColor &c);
44   inline bool operator==(const BColor &c) const
45   { return (r == c.r && b == c.b && b == c.b); }
46   inline bool operator!=(const BColor &c) const
47   { return (! operator==(c)); }
48
49   static void cleanupColorCache(void);
50
51 private:
52   void parseColorName(void);
53   void allocate(void);
54   void deallocate(void);
55
56   bool allocated;
57   int r, g, b;
58   unsigned long p;
59   unsigned int scrn;
60   std::string colorname;
61
62   // global color allocator/deallocator
63   struct RGB {
64     const int screen;
65     const int r, g, b;
66
67     RGB(void) : screen(~(0u)), r(-1), g(-1), b(-1) { }
68     RGB(const int b, const int x, const int y, const int z)
69       : screen(b), r(x), g(y), b(z) {}
70     RGB(const RGB &x)
71       : screen(x.screen), r(x.r), g(x.g), b(x.b) {}
72
73     inline bool operator==(const RGB &x) const {
74       return screen == x.screen &&
75              r == x.r && g == x.g && b == x.b;
76     }
77
78     inline bool operator<(const RGB &x) const {
79       unsigned long p1, p2;
80       p1 = (screen << 24 | r << 16 | g << 8 | b) & 0x00ffffff;
81       p2 = (x.screen << 24 | x.r << 16 | x.g << 8 | x.b) & 0x00ffffff;
82       return p1 < p2;
83     }
84   };
85   struct PixelRef {
86     const unsigned long p;
87     unsigned int count;
88     inline PixelRef(void) : p(0), count(0) { }
89     inline PixelRef(const unsigned long x) : p(x), count(1) { }
90   };
91   typedef std::map<RGB,PixelRef> ColorCache;
92   typedef ColorCache::value_type ColorCacheItem;
93   static ColorCache colorcache;
94   static bool cleancache;
95   static void doCacheCleanup(void);
96 };
97
98 }
99
100 #endif // COLOR_HH