news download themes documentation links










Color.cc

00001 // Color.cc for Fluxbox window manager
00002 // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
00003 //
00004 // Permission is hereby granted, free of charge, to any person obtaining a
00005 // copy of this software and associated documentation files (the "Software"),
00006 // to deal in the Software without restriction, including without limitation
00007 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 // and/or sell copies of the Software, and to permit persons to whom the
00009 // Software is furnished to do so, subject to the following conditions:
00010 //
00011 // The above copyright notice and this permission notice shall be included in
00012 // all copies or substantial portions of the Software.
00013 //
00014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020 // DEALINGS IN THE SOFTWARE.
00021 
00022 // $Id: Color.cc,v 1.8 2004/01/09 21:36:21 fluxgen Exp $
00023 
00024 #include "Color.hh"
00025 
00026 #include "App.hh"
00027 #include "StringUtil.hh"
00028 
00029 #include <iostream>
00030 using namespace std;
00031 
00032 namespace {
00033 unsigned char maxValue(unsigned short colval) {
00034     if (colval == 65535) 
00035         return 0xFF;
00036 
00037     return static_cast<unsigned char>(colval/0xFF);
00038 }
00039 
00040 };
00041 
00042 namespace FbTk {
00043 
00044 Color::Color():
00045     m_red(0), m_green(0), m_blue(0),
00046     m_pixel(0),
00047     m_allocated(false),
00048     m_screen(0) {
00049 
00050 }
00051 
00052 Color::Color(const Color &col_copy):
00053     m_red(0), m_green(0), m_blue(0),
00054     m_pixel(0),
00055     m_allocated(false), m_screen(0) {
00056     copy(col_copy);
00057 }
00058 
00059 Color::Color(unsigned short red, unsigned short green, unsigned short blue, int screen):
00060     m_red(red), m_green(green), m_blue(blue), 
00061     m_pixel(0), m_allocated(false),
00062     m_screen(screen) { 
00063     allocate(red, green, blue, screen);
00064 }
00065 
00066 Color::Color(const char *color_string, int screen):
00067     m_red(0), m_green(0), m_blue(0),
00068     m_pixel(0),
00069     m_allocated(false),
00070     m_screen(screen) {
00071     setFromString(color_string, screen);
00072 }
00073 
00074 Color::~Color() {
00075     free();
00076 }
00077 
00078 bool Color::setFromString(const char *color_string, int screen) {
00079 
00080     if (color_string == 0) {
00081         free();
00082         return false;
00083     }
00084     string color_string_tmp = color_string;
00085     StringUtil::removeFirstWhitespace(color_string_tmp);
00086     StringUtil::removeTrailingWhitespace(color_string_tmp);
00087 
00088     Display *disp = App::instance()->display();
00089     Colormap colm = DefaultColormap(disp, screen);
00090 
00091     XColor color;
00092 
00093     if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color))
00094         return false;
00095     else if (! XAllocColor(disp, colm, &color))
00096         return false;
00097 
00098     setPixel(color.pixel);
00099     setRGB(maxValue(color.red), 
00100            maxValue(color.green),
00101            maxValue(color.blue));
00102     setAllocated(true);
00103     m_screen = screen;
00104 
00105     return true;
00106 }
00107 
00108 /*
00109   Color &Color::Color::operator  = (const Color &col_copy) {
00110   // check for aliasing
00111   if (this == &col_copy)
00112   return *this;
00113 
00114   copy(col_copy);
00115   return *this;
00116   }
00117 */
00118 
00119 void Color::free() {
00120     if (isAllocated()) {
00121         unsigned long pixel = m_pixel;
00122         Display *disp = App::instance()->display();
00123         XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0);
00124         setPixel(0);
00125         setRGB(0, 0, 0);
00126         setAllocated(false);
00127     }   
00128 }
00129 
00130 void Color::copy(const Color &col_copy) {
00131     if (!col_copy.isAllocated()) {
00132         free();
00133         setRGB(col_copy.red(), col_copy.green(), col_copy.blue());
00134         setPixel(col_copy.pixel());
00135         return;
00136     }
00137 
00138     free();
00139         
00140     allocate(col_copy.red(), 
00141              col_copy.green(),
00142              col_copy.blue(),
00143              col_copy.m_screen);
00144     
00145 }
00146 
00147 void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, int screen) {
00148 
00149     Display *disp = App::instance()->display();
00150     XColor color;
00151     // fill xcolor structure
00152     color.red = red;
00153     color.green = green;    
00154     color.blue = blue;
00155     
00156     
00157     if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) {
00158         cerr<<"FbTk::Color: Allocation error."<<endl;
00159     } else {
00160         setRGB(maxValue(color.red),
00161                maxValue(color.green),
00162                maxValue(color.blue));
00163         setPixel(color.pixel);
00164         setAllocated(true);
00165     }
00166     
00167     m_screen = screen;
00168 }
00169 
00170 void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue) {
00171     m_red = red;
00172     m_green = green;
00173     m_blue = blue;
00174 }
00175 
00176 };

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA