]> Dogcows Code - chaz/openbox/blob - src/Color.hh
caps
[chaz/openbox] / src / Color.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Color.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef COLOR_HH
25 #define COLOR_HH
26
27 extern "C" {
28 #include <X11/Xlib.h>
29 }
30
31 #include <map>
32 #include <string>
33
34 class BaseDisplay;
35
36 class BColor {
37 public:
38 BColor(const BaseDisplay * const _display = 0, unsigned int _screen = ~(0u));
39 BColor(int _r, int _g, int _b,
40 const BaseDisplay * const _display, unsigned int _screen = ~(0u));
41 BColor(const std::string &_name,
42 const BaseDisplay * const _display, unsigned int _screen = ~(0u));
43 ~BColor(void);
44
45 inline const std::string &name(void) const { return colorname; }
46
47 inline int red(void) const { return r; }
48 inline int green(void) const { return g; }
49 inline int blue(void) const { return b; }
50 void setRGB(int _r, int _g, int _b) {
51 deallocate();
52 r = _r;
53 g = _g;
54 b = _b;
55 }
56
57 inline const BaseDisplay *display(void) const { return dpy; }
58 inline unsigned int screen(void) const { return scrn; }
59 void setDisplay(const BaseDisplay * const _display,
60 unsigned int _screen = ~(0u));
61
62 inline bool isAllocated(void) const { return allocated; }
63
64 inline bool isValid(void) const { return r != -1 && g != -1 && b != -1; }
65
66 unsigned long pixel(void) const;
67
68 // operators
69 BColor &operator=(const BColor &c);
70 inline bool operator==(const BColor &c) const
71 { return (r == c.r && b == c.b && b == c.b); }
72 inline bool operator!=(const BColor &c) const
73 { return (! operator==(c)); }
74
75 static void cleanupColorCache(void);
76
77 private:
78 void parseColorName(void);
79 void allocate(void);
80 void deallocate(void);
81
82 bool allocated;
83 int r, g, b;
84 unsigned long p;
85 const BaseDisplay *dpy;
86 unsigned int scrn;
87 std::string colorname;
88
89 // global color allocator/deallocator
90 struct RGB {
91 const BaseDisplay* const display;
92 const unsigned int screen;
93 const int r, g, b;
94
95 RGB(void) : display(0), screen(~(0u)), r(-1), g(-1), b(-1) { }
96 RGB(const BaseDisplay * const a, const unsigned int b,
97 const int x, const int y, const int z)
98 : display(a), screen(b), r(x), g(y), b(z) {}
99 RGB(const RGB &x)
100 : display(x.display), screen(x.screen), r(x.r), g(x.g), b(x.b) {}
101
102 inline bool operator==(const RGB &x) const {
103 return display == x.display &&
104 screen == x.screen &&
105 r == x.r && g == x.g && b == x.b;
106 }
107
108 inline bool operator<(const RGB &x) const {
109 unsigned long p1, p2;
110 p1 = (screen << 24 | r << 16 | g << 8 | b) & 0x00ffffff;
111 p2 = (x.screen << 24 | x.r << 16 | x.g << 8 | x.b) & 0x00ffffff;
112 return p1 < p2;
113 }
114 };
115 struct PixelRef {
116 const unsigned long p;
117 unsigned int count;
118 inline PixelRef(void) : p(0), count(0) { }
119 inline PixelRef(const unsigned long x) : p(x), count(1) { }
120 };
121 typedef std::map<RGB,PixelRef> ColorCache;
122 typedef ColorCache::value_type ColorCacheItem;
123 static ColorCache colorcache;
124 static bool cleancache;
125 static void doCacheCleanup(void);
126 };
127
128 #endif // COLOR_HH
This page took 0.039182 seconds and 4 git commands to generate.