diff options
Diffstat (limited to 'src/Color.cc')
-rw-r--r-- | src/Color.cc | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/Color.cc b/src/Color.cc new file mode 100644 index 0000000..b234150 --- /dev/null +++ b/src/Color.cc | |||
@@ -0,0 +1,107 @@ | |||
1 | // Color.cc for Fluxbox window manager | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@users.sourceforge.net) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: Color.cc,v 1.1 2002/09/14 12:47:50 fluxgen Exp $ | ||
23 | |||
24 | #include "Color.hh" | ||
25 | |||
26 | namespace { | ||
27 | unsigned char maxValue(unsigned short colval) { | ||
28 | if (colval == 65535) | ||
29 | return 0xFF; | ||
30 | |||
31 | return static_cast<unsigned char>(colval/0xFF); | ||
32 | } | ||
33 | |||
34 | }; | ||
35 | |||
36 | namespace FbTk { | ||
37 | Color::Color(): | ||
38 | m_allocated(false) { | ||
39 | |||
40 | } | ||
41 | |||
42 | Color::Color(unsigned char red, unsigned char green, unsigned char blue, int screen): | ||
43 | m_red(red), m_green(green), m_blue(blue), | ||
44 | m_pixel(0), m_allocated(false) { | ||
45 | |||
46 | Display *disp = BaseDisplay::getXDisplay(); | ||
47 | XColor color; | ||
48 | // fill xcolor structure | ||
49 | color.red = red; | ||
50 | color.blue = blue; | ||
51 | color.green = green; | ||
52 | |||
53 | if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) { | ||
54 | cerr<<"FbTk::Color: Allocation error."<<endl; | ||
55 | } else { | ||
56 | setRGB(maxValue(color.red), | ||
57 | maxValue(color.green), | ||
58 | maxValue(color.blue)); | ||
59 | setPixel(color.pixel); | ||
60 | setAllocated(true); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | Color::Color(const char *color_string, int screen): | ||
65 | m_allocated(false) { | ||
66 | setFromString(color_string, screen); | ||
67 | } | ||
68 | |||
69 | bool Color::setFromString(const char *color_string, int screen) { | ||
70 | |||
71 | if (color_string == 0) { | ||
72 | free(); | ||
73 | return false; | ||
74 | } | ||
75 | |||
76 | Display *disp = BaseDisplay::instance()->getXDisplay(); | ||
77 | Colormap colm = DefaultColormap(disp, screen); | ||
78 | |||
79 | XColor color; | ||
80 | |||
81 | if (! XParseColor(disp, colm, color_string, &color)) | ||
82 | cerr<<"FbTk::Color: Parse color error: \""<<color_string<<"\""<<endl; | ||
83 | else if (! XAllocColor(disp, colm, &color)) | ||
84 | cerr<<"FbTk::Color: Allocation error: \""<<color_string<<"\""<<endl; | ||
85 | |||
86 | |||
87 | |||
88 | setPixel(color.pixel); | ||
89 | setRGB(maxValue(color.red), | ||
90 | maxValue(color.green), | ||
91 | maxValue(color.blue)); | ||
92 | setAllocated(true); | ||
93 | |||
94 | return true; | ||
95 | } | ||
96 | |||
97 | void Color::free() { | ||
98 | if (isAllocated()) { | ||
99 | unsigned long pixel = col.pixel(); | ||
100 | XFreeColors(disp, colm, &pixel, 1, 0); | ||
101 | setPixel(0); | ||
102 | setRGB(0, 0, 0); | ||
103 | setAllocated(false); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | }; | ||