summaryrefslogtreecommitdiff
path: root/src/FbTk/Color.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Color.cc')
-rw-r--r--src/FbTk/Color.cc83
1 files changed, 64 insertions, 19 deletions
diff --git a/src/FbTk/Color.cc b/src/FbTk/Color.cc
index 2cd4278..ae35a94 100644
--- a/src/FbTk/Color.cc
+++ b/src/FbTk/Color.cc
@@ -31,6 +31,10 @@ using std::cerr;
31using std::endl; 31using std::endl;
32using std::string; 32using std::string;
33 33
34#if HAVE_XRENDER
35#include <X11/Xft/Xft.h>
36#endif
37
34namespace { 38namespace {
35 39
36inline unsigned char maxValue(unsigned short colval) { 40inline unsigned char maxValue(unsigned short colval) {
@@ -50,19 +54,31 @@ Color::Color():
50} 54}
51 55
52Color::Color(const Color &col_copy): 56Color::Color(const Color &col_copy):
53 m_red(0), m_green(0), m_blue(0), 57 m_red(0), m_green(0), m_blue(0), m_alpha(0),
54 m_pixel(0), 58 m_pixel(0),
55 m_allocated(false), m_screen(0) { 59 m_allocated(false), m_screen(0) {
56 copy(col_copy); 60 copy(col_copy);
57} 61}
58 62
59Color::Color(unsigned short red, unsigned short green, unsigned short blue, int screen): 63Color::Color(unsigned short red, unsigned short green, unsigned short blue, int screen):
60 m_red(red), m_green(green), m_blue(blue), 64 m_red(red), m_green(green), m_blue(blue), m_alpha(0),
61 m_pixel(0), m_allocated(false), 65 m_pixel(0), m_allocated(false),
62 m_screen(screen) { 66 m_screen(screen) {
63 allocate(red, green, blue, screen); 67
68 printf("Color: New RGB color: %d/%d/%d\n", red, green, blue);
69 allocate(red, green, blue, 0, screen);
64} 70}
65 71
72Color::Color(unsigned short red, unsigned short green, unsigned short blue, unsigned short alpha, int screen):
73 m_red(red), m_green(green), m_blue(blue), m_alpha(alpha),
74 m_pixel(0), m_allocated(false),
75 m_screen(screen) {
76
77 printf("Color: New RGB color: %d/%d/%d/%d\n", red, green, blue, alpha);
78 allocate(red, green, blue, alpha, screen);
79}
80
81
66Color::Color(const char *color_string, int screen): 82Color::Color(const char *color_string, int screen):
67 m_red(0), m_green(0), m_blue(0), 83 m_red(0), m_green(0), m_blue(0),
68 m_pixel(0), 84 m_pixel(0),
@@ -86,29 +102,23 @@ bool Color::setFromString(const char *color_string, int screen) {
86 StringUtil::removeTrailingWhitespace(color_string_tmp); 102 StringUtil::removeTrailingWhitespace(color_string_tmp);
87 103
88 Display *disp = App::instance()->display(); 104 Display *disp = App::instance()->display();
89 Colormap colm = DefaultColormap(disp, screen); 105 Colormap colm = App::instance()->defaultColormap(screen);
90 106
91 XColor color; 107 XColor color;
92 108
109 printf("Color: Parsing color string %s\n", color_string_tmp.c_str());
93 if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color)) 110 if (! XParseColor(disp, colm, color_string_tmp.c_str(), &color))
94 return false; 111 return false;
95 else if (! XAllocColor(disp, colm, &color))
96 return false;
97 112
98 setPixel(color.pixel); 113 allocate(color.red, color.green, color.blue, 0, screen);
99 setRGB(maxValue(color.red),
100 maxValue(color.green),
101 maxValue(color.blue));
102 setAllocated(true);
103 m_screen = screen;
104 114
105 return true; 115 return isAllocated();
106} 116}
107 117
108bool Color::validColorString(const char *color_string, int screen) { 118bool Color::validColorString(const char *color_string, int screen) {
109 XColor color; 119 XColor color;
110 Display *disp = App::instance()->display(); 120 Display *disp = App::instance()->display();
111 Colormap colm = DefaultColormap(disp, screen); 121 Colormap colm = App::instance()->defaultColormap(screen);
112 // trim white space 122 // trim white space
113 string color_string_tmp = color_string; 123 string color_string_tmp = color_string;
114 StringUtil::removeFirstWhitespace(color_string_tmp); 124 StringUtil::removeFirstWhitespace(color_string_tmp);
@@ -129,9 +139,14 @@ Color &Color::operator = (const Color &col_copy) {
129 139
130void Color::free() { 140void Color::free() {
131 if (isAllocated()) { 141 if (isAllocated()) {
132 unsigned long pixel = m_pixel;
133 Display *disp = App::instance()->display(); 142 Display *disp = App::instance()->display();
134 XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0); 143#if HAVE_XRENDER
144 XftColorFree(disp, App::instance()->defaultVisual(m_screen),
145 App::instance()->defaultColormap(m_screen), &m_xftcolor);
146#else /* No XRENDER */
147 unsigned long pixel = m_pixel;
148 XFreeColors(disp, App::instance()->defaultColormap(m_screen), &pixel, 1, 0);
149#endif
135 setPixel(0); 150 setPixel(0);
136 setRGB(0, 0, 0); 151 setRGB(0, 0, 0);
137 setAllocated(false); 152 setAllocated(false);
@@ -149,39 +164,69 @@ void Color::copy(const Color &col_copy) {
149 allocate(col_copy.red()*0x101, 164 allocate(col_copy.red()*0x101,
150 col_copy.green()*0x101, 165 col_copy.green()*0x101,
151 col_copy.blue()*0x101, 166 col_copy.blue()*0x101,
167 col_copy.alpha()*0x101,
152 col_copy.m_screen); 168 col_copy.m_screen);
153 169
154} 170}
155 171
156void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, int screen) { 172void Color::allocate(unsigned short red, unsigned short green, unsigned short blue, unsigned short alpha, int screen) {
157 173
158 Display *disp = App::instance()->display(); 174 Display *disp = App::instance()->display();
175 Colormap cmap = App::instance()->defaultColormap(screen);
176 int xresult;
177
178#if HAVE_XRENDER
179 Visual *vis = App::instance()->defaultVisual(screen);
180 XRenderColor acolor;
181 acolor.red = red;
182 acolor.green = green;
183 acolor.blue = blue;
184 acolor.alpha = alpha;
185
186 xresult = XftColorAllocValue(disp, vis, cmap, &acolor, &m_xftcolor);
187 printf("Color: XftColorAllocValue(%04x/%04x/%04x/%04x) returned %d\n",
188 red, green, blue, alpha, xresult);
189#else /* No XRENDER */
159 XColor color; 190 XColor color;
160 // fill xcolor structure 191 // fill xcolor structure
161 color.red = red; 192 color.red = red;
162 color.green = green; 193 color.green = green;
163 color.blue = blue; 194 color.blue = blue;
164 195
196 xresult = XAllocColor(disp, cmap, &color);
197 printf("Color: XAllocColor(%04x/%04x/%04x) returned %d\n",
198 red, green, blue, xresult);
199#endif
165 200
166 if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) { 201 if (!xresult) {
167 _FB_USES_NLS; 202 _FB_USES_NLS;
168 cerr<<"FbTk::Color: "<<_FBTK_CONSOLETEXT(Error, ColorAllocation, "Allocation error.", "XAllocColor failed...")<<endl; 203 cerr<<"FbTk::Color: "<<_FBTK_CONSOLETEXT(Error, ColorAllocation, "Allocation error.", "XAllocColor failed...")<<endl;
169 } else { 204 } else {
170 free(); 205 free();
206#if HAVE_XRENDER
207 setRGB(maxValue(m_xftcolor.color.red),
208 maxValue(m_xftcolor.color.green),
209 maxValue(m_xftcolor.color.blue),
210 maxValue(m_xftcolor.color.alpha));
211 setPixel(m_xftcolor.pixel);
212#else /* No XRENDER */
171 setRGB(maxValue(color.red), 213 setRGB(maxValue(color.red),
172 maxValue(color.green), 214 maxValue(color.green),
173 maxValue(color.blue)); 215 maxValue(color.blue));
174 setPixel(color.pixel); 216 setPixel(color.pixel);
217#endif
175 setAllocated(true); 218 setAllocated(true);
176 } 219 }
177 220
178 m_screen = screen; 221 m_screen = screen;
179} 222}
180 223
181void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue) { 224void Color::setRGB(unsigned short red, unsigned short green, unsigned short blue,
225 unsigned short alpha) {
182 m_red = red; 226 m_red = red;
183 m_green = green; 227 m_green = green;
184 m_blue = blue; 228 m_blue = blue;
229 m_alpha = alpha;
185} 230}
186 231
187}; 232};