aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Color.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Color.cc')
-rw-r--r--src/FbTk/Color.cc165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/FbTk/Color.cc b/src/FbTk/Color.cc
new file mode 100644
index 0000000..4a42d1c
--- /dev/null
+++ b/src/FbTk/Color.cc
@@ -0,0 +1,165 @@
1// Color.cc for Fluxbox window manager
2// Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen(at)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.8 2004/01/09 21:36:21 fluxgen Exp $
23
24#include "Color.hh"
25
26#include "App.hh"
27#include "StringUtil.hh"
28
29#include <iostream>
30using namespace std;
31
32namespace FbTk {
33
34Color::Color():
35 m_red(0), m_green(0), m_blue(0),
36 m_pixel(0),
37 m_allocated(false),
38 m_screen(0) {
39
40}
41
42Color::Color(const Color &col_copy):
43 m_red(0), m_green(0), m_blue(0),
44 m_pixel(0),
45 m_allocated(false), m_screen(0) {
46 copy(col_copy);
47}
48
49Color::Color(unsigned short red, unsigned short green, unsigned short blue, int screen):
50 m_red(red), m_green(green), m_blue(blue),
51 m_pixel(0), m_allocated(false),
52 m_screen(screen) {
53 allocate(red, green, blue, screen);
54}
55
56Color::Color(const char *color_string, int screen):
57 m_red(0), m_green(0), m_blue(0),
58 m_pixel(0),
59 m_allocated(false),
60 m_screen(screen) {
61 setFromString(color_string, screen);
62}
63
64Color::~Color() {
65 free();
66}
67
68bool Color::setFromString(const char *color_string, int screen) {
69
70 if (color_string == 0) {
71 free();
72 return false;
73 }
74 string color_string_tmp = color_string;
75 StringUtil::removeFirstWhitespace(color_string_tmp);
76 StringUtil::removeTrailingWhitespace(color_string_tmp);
77
78 Display *disp = App::instance()->display();
79 Colormap colm = DefaultColormap(disp, screen);
80
81 XColor color;
82
83 if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color))
84 return false;
85 else if (! XAllocColor(disp, colm, &color))
86 return false;
87
88 setPixel(color.pixel);
89 setRGB(color.red,
90 color.green,
91 color.blue);
92 setAllocated(true);
93 m_screen = screen;
94
95 return true;
96}
97
98
99Color &Color::Color::operator = (const Color &col_copy) {
100 // check for aliasing
101 if (this == &col_copy)
102 return *this;
103
104 copy(col_copy);
105 return *this;
106}
107
108void Color::free() {
109 if (isAllocated()) {
110 unsigned long pixel = m_pixel;
111 Display *disp = App::instance()->display();
112 XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0);
113 setPixel(0);
114 setRGB(0, 0, 0);
115 setAllocated(false);
116 }
117}
118
119void Color::copy(const Color &col_copy) {
120 if (!col_copy.isAllocated()) {
121 free();
122 setRGB(col_copy.red(), col_copy.green(), col_copy.blue());
123 setPixel(col_copy.pixel());
124 return;
125 }
126
127 free();
128
129 allocate(col_copy.red(),
130 col_copy.green(),
131 col_copy.blue(),
132 col_copy.m_screen);
133
134
135}
136
137void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, int screen) {
138
139 Display *disp = App::instance()->display();
140 XColor color;
141 // fill xcolor structure
142 color.red = red;
143 color.green = green;
144 color.blue = blue;
145
146 if (XAllocColor(disp, DefaultColormap(disp, screen), &color) == 0) {
147 cerr<<"FbTk::Color: Allocation error."<<endl;
148 } else {
149 setRGB(color.red,
150 color.green,
151 color.blue);
152 setPixel(color.pixel);
153 setAllocated(true);
154 }
155
156 m_screen = screen;
157}
158
159void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue) {
160 m_red = red;
161 m_green = green;
162 m_blue = blue;
163}
164
165};