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>
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
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));
45 inline const std::string &name(void) const { return colorname; }
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) {
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));
62 inline bool isAllocated(void) const { return allocated; }
64 inline bool isValid(void) const { return r != -1 && g != -1 && b != -1; }
66 unsigned long pixel(void) const;
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)); }
75 static void cleanupColorCache(void);
78 void parseColorName(void);
80 void deallocate(void);
85 const BaseDisplay *dpy;
87 std::string colorname;
89 // global color allocator/deallocator
91 const BaseDisplay* const display;
92 const unsigned int screen;
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) {}
100 : display(x.display), screen(x.screen), r(x.r), g(x.g), b(x.b) {}
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;
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;
116 const unsigned long p;
118 inline PixelRef(void) : p(0), count(0) { }
119 inline PixelRef(const unsigned long x) : p(x), count(1) { }
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);