~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/Color.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Color.cc for Fluxbox window manager
2
 
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
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 3864 2005-01-24 18:02:34Z mathias $
23
 
 
24
 
#include "Color.hh"
25
 
 
26
 
#include "App.hh"
27
 
#include "StringUtil.hh"
28
 
#include "I18n.hh"
29
 
 
30
 
#include <iostream>
31
 
using namespace std;
32
 
 
33
 
namespace {
34
 
unsigned char maxValue(unsigned short colval) {
35
 
    if (colval == 65535) 
36
 
        return 0xFF;
37
 
 
38
 
    return static_cast<unsigned char>(colval/0xFF);
39
 
}
40
 
 
41
 
};
42
 
 
43
 
namespace FbTk {
44
 
 
45
 
Color::Color():
46
 
    m_red(0), m_green(0), m_blue(0),
47
 
    m_pixel(0),
48
 
    m_allocated(false),
49
 
    m_screen(0) {
50
 
 
51
 
}
52
 
 
53
 
Color::Color(const Color &col_copy):
54
 
    m_red(0), m_green(0), m_blue(0),
55
 
    m_pixel(0),
56
 
    m_allocated(false), m_screen(0) {
57
 
    copy(col_copy);
58
 
}
59
 
 
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),
63
 
    m_screen(screen) { 
64
 
    allocate(red, green, blue, screen);
65
 
}
66
 
 
67
 
Color::Color(const char *color_string, int screen):
68
 
    m_red(0), m_green(0), m_blue(0),
69
 
    m_pixel(0),
70
 
    m_allocated(false),
71
 
    m_screen(screen) {
72
 
    setFromString(color_string, screen);
73
 
}
74
 
 
75
 
Color::~Color() {
76
 
    free();
77
 
}
78
 
 
79
 
bool Color::setFromString(const char *color_string, int screen) {
80
 
 
81
 
    if (color_string == 0) {
82
 
        free();
83
 
        return false;
84
 
    }
85
 
    string color_string_tmp = color_string;
86
 
    StringUtil::removeFirstWhitespace(color_string_tmp);
87
 
    StringUtil::removeTrailingWhitespace(color_string_tmp);
88
 
 
89
 
    Display *disp = App::instance()->display();
90
 
    Colormap colm = DefaultColormap(disp, screen);
91
 
 
92
 
    XColor color;
93
 
 
94
 
    if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color))
95
 
        return false;
96
 
    else if (! XAllocColor(disp, colm, &color))
97
 
        return false;
98
 
 
99
 
    setPixel(color.pixel);
100
 
    setRGB(maxValue(color.red), 
101
 
           maxValue(color.green),
102
 
           maxValue(color.blue));
103
 
    setAllocated(true);
104
 
    m_screen = screen;
105
 
 
106
 
    return true;
107
 
}
108
 
 
109
 
 
110
 
Color &Color::operator  = (const Color &col_copy) {
111
 
    // check for aliasing
112
 
    if (this == &col_copy)
113
 
        return *this;
114
 
    
115
 
    copy(col_copy);
116
 
    return *this;
117
 
}
118
 
 
119
 
 
120
 
void Color::free() {
121
 
    if (isAllocated()) {
122
 
        unsigned long pixel = m_pixel;
123
 
        Display *disp = App::instance()->display();
124
 
        XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0);
125
 
        setPixel(0);
126
 
        setRGB(0, 0, 0);
127
 
        setAllocated(false);
128
 
    }   
129
 
}
130
 
 
131
 
void Color::copy(const Color &col_copy) {
132
 
    if (!col_copy.isAllocated()) {
133
 
        free();
134
 
        setRGB(col_copy.red(), col_copy.green(), col_copy.blue());
135
 
        setPixel(col_copy.pixel());
136
 
        return;
137
 
    }
138
 
 
139
 
    free();
140
 
                
141
 
    allocate(col_copy.red()*0xFF, 
142
 
             col_copy.green()*0xFF,
143
 
             col_copy.blue()*0xFF,
144
 
             col_copy.m_screen);
145
 
        
146
 
}
147
 
 
148
 
void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, int screen) {
149
 
 
150
 
    Display *disp = App::instance()->display();
151
 
    XColor color;
152
 
    // fill xcolor structure
153
 
    color.red = red;
154
 
    color.green = green;        
155
 
    color.blue = blue;
156
 
        
157
 
        
158
 
    if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) {
159
 
        _FB_USES_NLS;
160
 
        cerr<<"FbTk::Color: "<<_FBTKTEXT(Error, ColorAllocation, "Allocation error.", "XAllocColor failed...")<<endl;
161
 
    } else {
162
 
        setRGB(maxValue(color.red),
163
 
               maxValue(color.green),
164
 
               maxValue(color.blue));
165
 
        setPixel(color.pixel);
166
 
        setAllocated(true);
167
 
    }
168
 
        
169
 
    m_screen = screen;
170
 
}
171
 
 
172
 
void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue) {
173
 
    m_red = red;
174
 
    m_green = green;
175
 
    m_blue = blue;
176
 
}
177
 
 
178
 
};