aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Color.cc
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2002-11-26 16:01:28 (GMT)
committerfluxgen <fluxgen>2002-11-26 16:01:28 (GMT)
commitac4420cdc50b7d7410781bc1bbbd1e89e18f256d (patch)
tree5c18c5497d42fafdd962269aa42f2efe80a2afdf /src/FbTk/Color.cc
parent031edc36acb3957046d0836ec0ac17cd1c323b22 (diff)
downloadfluxbox-ac4420cdc50b7d7410781bc1bbbd1e89e18f256d.zip
fluxbox-ac4420cdc50b7d7410781bc1bbbd1e89e18f256d.tar.bz2
initial import
Diffstat (limited to 'src/FbTk/Color.cc')
-rw-r--r--src/FbTk/Color.cc166
1 files changed, 166 insertions, 0 deletions
diff --git a/src/FbTk/Color.cc b/src/FbTk/Color.cc
new file mode 100644
index 0000000..2e69f1c
--- /dev/null
+++ b/src/FbTk/Color.cc
@@ -0,0 +1,166 @@
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/11/26 16:01:27 fluxgen Exp $
23
24#include "Color.hh"
25
26#include "App.hh"
27
28#include <iostream>
29using namespace std;
30
31namespace {
32unsigned char maxValue(unsigned short colval) {
33 if (colval == 65535)
34 return 0xFF;
35
36 return static_cast<unsigned char>(colval/0xFF);
37}
38
39};
40
41namespace FbTk {
42Color::Color():
43m_allocated(false),
44m_screen(0) {
45
46}
47
48Color::Color(const Color &col_copy) {
49 copy(col_copy);
50}
51
52Color::Color(unsigned char red, unsigned char green, unsigned char blue, int screen):
53m_red(red), m_green(green), m_blue(blue),
54m_pixel(0), m_allocated(false),
55m_screen(screen) {
56 allocate(red, green, blue, screen);
57}
58
59Color::Color(const char *color_string, int screen):
60m_allocated(false),
61m_screen(screen) {
62 setFromString(color_string, screen);
63}
64
65Color::~Color() {
66 free();
67}
68
69bool Color::setFromString(const char *color_string, int screen) {
70
71 if (color_string == 0) {
72 free();
73 return false;
74 }
75
76 Display *disp = App::instance()->display();
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 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*/
108
109void Color::free() {
110 if (isAllocated()) {
111 unsigned long pixel = m_pixel;
112 Display *disp = App::instance()->display();
113 XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0);
114 setPixel(0);
115 setRGB(0, 0, 0);
116 setAllocated(false);
117 }
118}
119
120void Color::copy(const Color &col_copy) {
121 if (!col_copy.isAllocated()) {
122 free();
123 setRGB(col_copy.red(), col_copy.green(), col_copy.blue());
124 setPixel(col_copy.pixel());
125 return;
126 }
127
128 free();
129
130 allocate(col_copy.red(),
131 col_copy.green(),
132 col_copy.blue(),
133 col_copy.m_screen);
134
135}
136
137void Color::allocate(unsigned char red, unsigned char green, unsigned char 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
147 if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) {
148 cerr<<"FbTk::Color: Allocation error."<<endl;
149 } else {
150 setRGB(maxValue(color.red),
151 maxValue(color.green),
152 maxValue(color.blue));
153 setPixel(color.pixel);
154 setAllocated(true);
155 }
156
157 m_screen = screen;
158}
159
160void Color::setRGB(unsigned char red, unsigned char green, unsigned char blue) {
161 m_red = red;
162 m_green = green;
163 m_blue = blue;
164}
165
166};