usb_ecm: Use the current configuration instead of a fixed one.
[haiku.git] / src / servers / app / RGBColor.cpp
blob95761ea2d5e5e1e4080b0d9b259d94e4198f1050
1 /*
2 * Copyright 2001-2006, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * DarkWyrm <bpmagic@columbus.rr.com>
7 */
10 #include "RGBColor.h"
11 #include "SystemPalette.h"
13 #include <stdio.h>
14 #include <stdlib.h>
17 /*!
18 \brief An approximation of 31/255, which is needed for converting from 32-bit
19 colors to 16-bit and 15-bit.
21 #define RATIO_8_TO_5_BIT .121568627451
23 /*!
24 \brief An approximation of 63/255, which is needed for converting from 32-bit
25 colors to 16-bit.
27 #define RATIO_8_TO_6_BIT .247058823529
29 /*!
30 \brief An approximation of 255/31, which is needed for converting from 16-bit
31 and 15-bit colors to 32-bit.
33 #define RATIO_5_TO_8_BIT 8.22580645161
35 /*!
36 \brief An approximation of 255/63, which is needed for converting from 16-bit
37 colors to 32-bit.
39 #define RATIO_6_TO_8_BIT 4.04761904762
41 #if 0
42 /*!
43 \brief Function for easy conversion of 16-bit colors to 32-bit
44 \param col Pointer to an rgb_color.
45 \param color RGB16 color
47 This function will do nothing if passed a NULL 32-bit color.
49 void
50 SetRGBColor16(rgb_color *col,uint16 color)
52 if(!col)
53 return;
55 uint16 r16,g16,b16;
57 // alpha's the easy part
58 col->alpha=0;
60 r16= (color >> 11) & 31;
61 g16= (color >> 5) & 63;
62 b16= color & 31;
64 col->red=uint8(r16 * RATIO_5_TO_8_BIT);
65 col->green=uint8(g16 * RATIO_6_TO_8_BIT);
66 col->blue=uint8(b16 * RATIO_5_TO_8_BIT);
68 #endif
70 /*!
71 \brief Finds the index of the closest matching color in a rgb_color palette array
72 \param palette Array of 256 rgb_color objects
73 \param color Color to match
74 \return Index of the closest matching color
76 Note that passing a NULL palette will always return 0 and passing an array of less
77 than 256 rgb_colors will cause a crash.
79 static uint8
80 FindClosestColor(const rgb_color *palette, rgb_color color)
82 if (!palette)
83 return 0;
85 uint16 cindex = 0, cdelta = 765, delta = 765;
87 for (uint16 i = 0; i < 256; i++) {
88 const rgb_color *c = &(palette[i]);
89 delta = abs(c->red-color.red) + abs(c->green-color.green)
90 + abs(c->blue-color.blue);
92 if (delta == 0) {
93 cindex = i;
94 break;
97 if (delta < cdelta) {
98 cindex = i;
99 cdelta = delta;
103 return (uint8)cindex;
108 \brief Constructs a RGBA15 color which best matches a given 32-bit color
109 \param color Color to match
110 \return The closest matching color's value
112 Format is ARGB, 1:5:5:5
114 static uint16
115 FindClosestColor15(rgb_color color)
117 uint16 r16 = uint16(color.red * RATIO_8_TO_5_BIT);
118 uint16 g16 = uint16(color.green * RATIO_8_TO_5_BIT);
119 uint16 b16 = uint16(color.blue * RATIO_8_TO_5_BIT);
121 // start with alpha value
122 uint16 color16 = color.alpha > 127 ? 0x8000 : 0;
124 color16 |= r16 << 10;
125 color16 |= g16 << 5;
126 color16 |= b16;
128 return color16;
133 \brief Constructs a RGB16 color which best matches a given 32-bit color
134 \param color Color to match
135 \return The closest matching color's value
137 Format is RGB, 5:6:5
139 static uint16
140 FindClosestColor16(rgb_color color)
142 uint16 r16 = uint16(color.red * RATIO_8_TO_5_BIT);
143 uint16 g16 = uint16(color.green * RATIO_8_TO_6_BIT);
144 uint16 b16 = uint16(color.blue * RATIO_8_TO_5_BIT);
146 uint16 color16 = r16 << 11;
147 color16 |= g16 << 5;
148 color16 |= b16;
150 return color16;
154 // #pragma mark -
158 \brief Create an RGBColor from specified values
159 \param red red
160 \param green green
161 \param blue blue
162 \param alpha alpha, defaults to 255
164 RGBColor::RGBColor(uint8 r, uint8 g, uint8 b, uint8 a)
166 SetColor(r,g,b,a);
171 \brief Create an RGBColor from specified values
172 \param red red
173 \param green green
174 \param blue blue
175 \param alpha alpha, defaults to 255
177 RGBColor::RGBColor(int r, int g, int b, int a)
179 SetColor(r, g, b, a);
184 \brief Create an RGBColor from an rgb_color
185 \param color color to initialize from
187 RGBColor::RGBColor(const rgb_color &color)
189 SetColor(color);
192 #if 0
194 \brief Create an RGBColor from a 16-bit RGBA color
195 \param color color to initialize from
197 RGBColor::RGBColor(uint16 color)
199 SetColor(color);
201 #endif
204 \brief Create an RGBColor from an index color
205 \param color color to initialize from
207 RGBColor::RGBColor(uint8 color)
209 SetColor(color);
214 \brief Copy Contructor
215 \param color color to initialize from
217 RGBColor::RGBColor(const RGBColor &color)
219 fColor32 = color.fColor32;
220 fColor16 = color.fColor16;
221 fColor15 = color.fColor15;
222 fColor8 = color.fColor8;
223 fUpdate8 = color.fUpdate8;
224 fUpdate15 = color.fUpdate15;
225 fUpdate16 = color.fUpdate16;
230 \brief Create an RGBColor with the values(0,0,0,0)
232 RGBColor::RGBColor()
234 SetColor(0, 0, 0, 0);
239 \brief Returns the color as the closest 8-bit color in the palette
240 \return The palette index for the current color
242 uint8
243 RGBColor::GetColor8() const
245 if (fUpdate8) {
246 fColor8 = FindClosestColor(SystemPalette(), fColor32);
247 fUpdate8 = false;
250 return fColor8;
255 \brief Returns the color as the closest 15-bit color
256 \return 15-bit value of the current color plus 1-bit alpha
258 uint16
259 RGBColor::GetColor15() const
261 if (fUpdate15) {
262 fColor15 = FindClosestColor15(fColor32);
263 fUpdate15 = false;
266 return fColor15;
271 \brief Returns the color as the closest 16-bit color
272 \return 16-bit value of the current color
274 uint16
275 RGBColor::GetColor16() const
277 if (fUpdate16) {
278 fColor16 = FindClosestColor16(fColor32);
279 fUpdate16 = false;
282 return fColor16;
287 \brief Returns the color as a 32-bit color
288 \return current color, including alpha
290 rgb_color
291 RGBColor::GetColor32() const
293 return fColor32;
298 \brief Set the object to specified values
299 \param red red
300 \param green green
301 \param blue blue
302 \param alpha alpha, defaults to 255
304 void
305 RGBColor::SetColor(uint8 r, uint8 g, uint8 b, uint8 a)
307 fColor32.red = r;
308 fColor32.green = g;
309 fColor32.blue = b;
310 fColor32.alpha = a;
312 fUpdate8 = fUpdate15 = fUpdate16 = true;
317 \brief Set the object to specified values
318 \param red red
319 \param green green
320 \param blue blue
321 \param alpha alpha, defaults to 255
323 void
324 RGBColor::SetColor(int r, int g, int b, int a)
326 fColor32.red = (uint8)r;
327 fColor32.green = (uint8)g;
328 fColor32.blue = (uint8)b;
329 fColor32.alpha = (uint8)a;
331 fUpdate8 = fUpdate15 = fUpdate16 = true;
334 #if 0
336 \brief Set the object to specified value
337 \param col16 color to copy
339 void
340 RGBColor::SetColor(uint16 col16)
342 fColor16 = col16;
343 SetRGBColor(&fColor32, col16);
345 fUpdate8 = true;
346 fUpdate15 = true;
347 fUpdate16 = false;
349 #endif
353 \brief Set the object to specified index in the palette
354 \param col8 color to copy
356 void
357 RGBColor::SetColor(uint8 col8)
359 fColor8 = col8;
360 fColor32 = SystemPalette()[col8];
362 fUpdate8 = false;
363 fUpdate15 = true;
364 fUpdate16 = true;
369 \brief Set the object to specified color
370 \param color color to copy
372 void
373 RGBColor::SetColor(const rgb_color &color)
375 fColor32 = color;
376 fUpdate8 = fUpdate15 = fUpdate16 = true;
381 \brief Set the object to specified color
382 \param color color to copy
384 void
385 RGBColor::SetColor(const RGBColor &color)
387 fColor32 = color.fColor32;
388 fColor16 = color.fColor16;
389 fColor15 = color.fColor15;
390 fColor8 = color.fColor8;
391 fUpdate8 = color.fUpdate8;
392 fUpdate15 = color.fUpdate15;
393 fUpdate16 = color.fUpdate16;
398 \brief Set the object to specified color
399 \param color color to copy
401 const RGBColor&
402 RGBColor::operator=(const RGBColor &color)
404 fColor32 = color.fColor32;
405 fColor16 = color.fColor16;
406 fColor15 = color.fColor15;
407 fColor8 = color.fColor8;
408 fUpdate8 = color.fUpdate8;
409 fUpdate15 = color.fUpdate15;
410 fUpdate16 = color.fUpdate16;
412 return *this;
417 \brief Set the object to specified color
418 \param color color to copy
420 const RGBColor&
421 RGBColor::operator=(const rgb_color &color)
423 fColor32 = color;
424 fUpdate8 = fUpdate15 = fUpdate16 = true;
426 return *this;
431 \brief Prints the 32-bit values of the color to standard out
433 void
434 RGBColor::PrintToStream(void) const
436 printf("RGBColor(%u,%u,%u,%u)\n",
437 fColor32.red, fColor32.green, fColor32.blue, fColor32.alpha);
442 \brief Overloaded comaparison
443 \return true if all color elements are exactly equal
445 bool
446 RGBColor::operator==(const rgb_color &color) const
448 return fColor32.red == color.red
449 && fColor32.green == color.green
450 && fColor32.blue == color.blue
451 && fColor32.alpha == color.alpha;
456 \brief Overloaded comaparison
457 \return true if all color elements are exactly equal
459 bool
460 RGBColor::operator==(const RGBColor &color) const
462 return fColor32.red == color.fColor32.red
463 && fColor32.green == color.fColor32.green
464 && fColor32.blue == color.fColor32.blue
465 && fColor32.alpha == color.fColor32.alpha;
469 bool
470 RGBColor::operator!=(const rgb_color &color) const
472 return fColor32.red != color.red
473 || fColor32.green != color.green
474 || fColor32.blue != color.blue
475 || fColor32.alpha != color.alpha;
479 bool
480 RGBColor::operator!=(const RGBColor &color) const
482 return fColor32.red != color.fColor32.red
483 || fColor32.green != color.fColor32.green
484 || fColor32.blue != color.fColor32.blue
485 || fColor32.alpha != color.fColor32.alpha;
489 bool
490 RGBColor::IsTransparentMagic() const
492 // TODO: validate this for B_CMAP8 for example
493 return *this == B_TRANSPARENT_COLOR;