]>
Dogcows Code - chaz/openbox/blob - src/Color.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Color.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
29 #include "BaseDisplay.hh"
36 BColor::ColorCache
BColor::colorcache
;
37 bool BColor::cleancache
= false;
39 BColor::BColor(const BaseDisplay
* const _display
, unsigned int _screen
)
40 : allocated(false), r(-1), g(-1), b(-1), p(0), dpy(_display
), scrn(_screen
)
43 BColor::BColor(int _r
, int _g
, int _b
,
44 const BaseDisplay
* const _display
, unsigned int _screen
)
45 : allocated(false), r(_r
), g(_g
), b(_b
), p(0), dpy(_display
), scrn(_screen
)
49 BColor::BColor(const std::string
&_name
,
50 const BaseDisplay
* const _display
, unsigned int _screen
)
51 : allocated(false), r(-1), g(-1), b(-1), p(0), dpy(_display
), scrn(_screen
),
57 BColor::~BColor(void) {
62 void BColor::setDisplay(const BaseDisplay
* const _display
,
63 unsigned int _screen
) {
64 if (_display
== display() && _screen
== screen()) {
74 if (! colorname
.empty()) {
80 unsigned long BColor::pixel(void) const {
83 BColor
*that
= (BColor
*) this;
91 void BColor::parseColorName(void) {
94 if (colorname
.empty()) {
95 fprintf(stderr
, "BColor: empty colorname, cannot parse (using black)\n");
100 scrn
= DefaultScreen(display()->getXDisplay());
101 Colormap colormap
= display()->getScreenInfo(scrn
)->getColormap();
103 // get rgb values from colorname
110 if (! XParseColor(display()->getXDisplay(), colormap
,
111 colorname
.c_str(), &xcol
)) {
112 fprintf(stderr
, "BColor::allocate: color parse error: \"%s\"\n",
118 setRGB(xcol
.red
>> 8, xcol
.green
>> 8, xcol
.blue
>> 8);
122 void BColor::allocate(void) {
125 if (scrn
== ~(0u)) scrn
= DefaultScreen(display()->getXDisplay());
126 Colormap colormap
= display()->getScreenInfo(scrn
)->getColormap();
129 if (colorname
.empty()) {
130 fprintf(stderr
, "BColor: cannot allocate invalid color (using black)\n");
137 // see if we have allocated this color before
138 RGB
rgb(display(), scrn
, r
, g
, b
);
139 ColorCache::iterator it
= colorcache
.find(rgb
);
140 if (it
!= colorcache
.end()) {
144 (*it
).second
.count
++;
148 // allocate color from rgb values
150 xcol
.red
= r
| r
<< 8;
151 xcol
.green
= g
| g
<< 8;
152 xcol
.blue
= b
| b
<< 8;
155 if (! XAllocColor(display()->getXDisplay(), colormap
, &xcol
)) {
156 fprintf(stderr
, "BColor::allocate: color alloc error: rgb:%x/%x/%x\n",
164 colorcache
.insert(ColorCacheItem(rgb
, PixelRef(p
)));
171 void BColor::deallocate(void) {
177 ColorCache::iterator it
= colorcache
.find(RGB(display(), scrn
, r
, g
, b
));
178 if (it
!= colorcache
.end()) {
179 if ((*it
).second
.count
>= 1)
180 (*it
).second
.count
--;
190 BColor
&BColor::operator=(const BColor
&c
) {
193 setRGB(c
.r
, c
.g
, c
.b
);
194 colorname
= c
.colorname
;
201 void BColor::cleanupColorCache(void) {
206 void BColor::doCacheCleanup(void) {
207 // ### TODO - support multiple displays!
208 ColorCache::iterator it
= colorcache
.begin();
209 if (it
== colorcache
.end()) {
214 const BaseDisplay
* const display
= (*it
).first
.display
;
215 unsigned long *pixels
= new unsigned long[ colorcache
.size() ];
216 unsigned int i
, count
;
218 for (i
= 0; i
< display
->getNumberOfScreens(); i
++) {
220 it
= colorcache
.begin();
222 while (it
!= colorcache
.end()) {
223 if ((*it
).second
.count
!= 0 || (*it
).first
.screen
!= i
) {
228 pixels
[ count
++ ] = (*it
).second
.p
;
229 ColorCache::iterator it2
= it
;
231 colorcache
.erase(it2
);
235 XFreeColors(display
->getXDisplay(),
236 display
->getScreenInfo(i
)->getColormap(),
This page took 0.047577 seconds and 5 git commands to generate.