2 * Copyright 2001-2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * DarkWyrm <bpmagic@columbus.rr.com>
11 #include "SystemPalette.h"
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
24 \brief An approximation of 63/255, which is needed for converting from 32-bit
27 #define RATIO_8_TO_6_BIT .247058823529
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
36 \brief An approximation of 255/63, which is needed for converting from 16-bit
39 #define RATIO_6_TO_8_BIT 4.04761904762
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.
50 SetRGBColor16(rgb_color
*col
,uint16 color
)
57 // alpha's the easy part
60 r16
= (color
>> 11) & 31;
61 g16
= (color
>> 5) & 63;
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
);
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.
80 FindClosestColor(const rgb_color
*palette
, rgb_color color
)
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
);
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
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;
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
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;
158 \brief Create an RGBColor from specified values
162 \param alpha alpha, defaults to 255
164 RGBColor::RGBColor(uint8 r
, uint8 g
, uint8 b
, uint8 a
)
171 \brief Create an RGBColor from specified values
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
)
194 \brief Create an RGBColor from a 16-bit RGBA color
195 \param color color to initialize from
197 RGBColor::RGBColor(uint16 color
)
204 \brief Create an RGBColor from an index color
205 \param color color to initialize from
207 RGBColor::RGBColor(uint8 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)
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
243 RGBColor::GetColor8() const
246 fColor8
= FindClosestColor(SystemPalette(), fColor32
);
255 \brief Returns the color as the closest 15-bit color
256 \return 15-bit value of the current color plus 1-bit alpha
259 RGBColor::GetColor15() const
262 fColor15
= FindClosestColor15(fColor32
);
271 \brief Returns the color as the closest 16-bit color
272 \return 16-bit value of the current color
275 RGBColor::GetColor16() const
278 fColor16
= FindClosestColor16(fColor32
);
287 \brief Returns the color as a 32-bit color
288 \return current color, including alpha
291 RGBColor::GetColor32() const
298 \brief Set the object to specified values
302 \param alpha alpha, defaults to 255
305 RGBColor::SetColor(uint8 r
, uint8 g
, uint8 b
, uint8 a
)
312 fUpdate8
= fUpdate15
= fUpdate16
= true;
317 \brief Set the object to specified values
321 \param alpha alpha, defaults to 255
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;
336 \brief Set the object to specified value
337 \param col16 color to copy
340 RGBColor::SetColor(uint16 col16
)
343 SetRGBColor(&fColor32
, col16
);
353 \brief Set the object to specified index in the palette
354 \param col8 color to copy
357 RGBColor::SetColor(uint8 col8
)
360 fColor32
= SystemPalette()[col8
];
369 \brief Set the object to specified color
370 \param color color to copy
373 RGBColor::SetColor(const rgb_color
&color
)
376 fUpdate8
= fUpdate15
= fUpdate16
= true;
381 \brief Set the object to specified color
382 \param color color to copy
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
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
;
417 \brief Set the object to specified color
418 \param color color to copy
421 RGBColor::operator=(const rgb_color
&color
)
424 fUpdate8
= fUpdate15
= fUpdate16
= true;
431 \brief Prints the 32-bit values of the color to standard out
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
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
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
;
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
;
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
;
490 RGBColor::IsTransparentMagic() const
492 // TODO: validate this for B_CMAP8 for example
493 return *this == B_TRANSPARENT_COLOR
;