00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00110
00111
00112
00113
00114
00115
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
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 };