1
// Color.cc for Fluxbox window manager
2
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
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:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
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.
22
// $Id: Color.cc 3864 2005-01-24 18:02:34Z mathias $
27
#include "StringUtil.hh"
34
unsigned char maxValue(unsigned short colval) {
38
return static_cast<unsigned char>(colval/0xFF);
46
m_red(0), m_green(0), m_blue(0),
53
Color::Color(const Color &col_copy):
54
m_red(0), m_green(0), m_blue(0),
56
m_allocated(false), m_screen(0) {
60
Color::Color(unsigned short red, unsigned short green, unsigned short blue, int screen):
61
m_red(red), m_green(green), m_blue(blue),
62
m_pixel(0), m_allocated(false),
64
allocate(red, green, blue, screen);
67
Color::Color(const char *color_string, int screen):
68
m_red(0), m_green(0), m_blue(0),
72
setFromString(color_string, screen);
79
bool Color::setFromString(const char *color_string, int screen) {
81
if (color_string == 0) {
85
string color_string_tmp = color_string;
86
StringUtil::removeFirstWhitespace(color_string_tmp);
87
StringUtil::removeTrailingWhitespace(color_string_tmp);
89
Display *disp = App::instance()->display();
90
Colormap colm = DefaultColormap(disp, screen);
94
if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color))
96
else if (! XAllocColor(disp, colm, &color))
99
setPixel(color.pixel);
100
setRGB(maxValue(color.red),
101
maxValue(color.green),
102
maxValue(color.blue));
110
Color &Color::operator = (const Color &col_copy) {
111
// check for aliasing
112
if (this == &col_copy)
122
unsigned long pixel = m_pixel;
123
Display *disp = App::instance()->display();
124
XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0);
131
void Color::copy(const Color &col_copy) {
132
if (!col_copy.isAllocated()) {
134
setRGB(col_copy.red(), col_copy.green(), col_copy.blue());
135
setPixel(col_copy.pixel());
141
allocate(col_copy.red()*0xFF,
142
col_copy.green()*0xFF,
143
col_copy.blue()*0xFF,
148
void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, int screen) {
150
Display *disp = App::instance()->display();
152
// fill xcolor structure
158
if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) {
160
cerr<<"FbTk::Color: "<<_FBTKTEXT(Error, ColorAllocation, "Allocation error.", "XAllocColor failed...")<<endl;
162
setRGB(maxValue(color.red),
163
maxValue(color.green),
164
maxValue(color.blue));
165
setPixel(color.pixel);
172
void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue) {