diff --git a/headers/os/interface/ColorUtils.h b/headers/os/interface/ColorUtils.h deleted file mode 100644 index 5be9db4fab..0000000000 --- a/headers/os/interface/ColorUtils.h +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, OpenBeOS -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: ColorUtils.h -// Author: DarkWyrm -// Description: Miscellaneous useful functions for working with colors -// -// -//------------------------------------------------------------------------------ -#ifndef COLORUTILS_H_ -#define COLORUTILS_H_ - -#include - -void SetRGBColor(rgb_color *col,uint8 r, uint8 g, uint8 b, uint8 a=255); -void SetRGBColor15(rgb_color *col,uint16 color); -void SetRGBColor16(rgb_color *col,uint16 color); -void SetRGBColor(rgb_color *col,uint32 color); - -uint8 FindClosestColor(const rgb_color *palette, rgb_color color); -uint16 FindClosestColor15(rgb_color color); -uint16 FindClosestColor16(rgb_color color); - -rgb_color MakeBlendColor(rgb_color col, rgb_color col2, float position); - -#endif diff --git a/headers/private/servers/app/ColorSet.h b/headers/private/servers/app/ColorSet.h index 3618d80276..8fe8a72fdd 100644 --- a/headers/private/servers/app/ColorSet.h +++ b/headers/private/servers/app/ColorSet.h @@ -1,44 +1,26 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, Haiku, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: ColorSet.h -// Author: DarkWyrm -// Description: Class for encapsulating GUI system colors -// -// -//------------------------------------------------------------------------------ -#ifndef COLORSET_H_ -#define COLORSET_H_ +/* + * Copyright 2001-2006, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * DarkWyrm + */ +#ifndef COLOR_SET_H +#define COLOR_SET_H + +#include #include #include #include -#include + /*! \class ColorSet ColorSet.h \brief Encapsulates GUI system colors */ class ColorSet : public BLocker { -public: + public: ColorSet(); ColorSet(const ColorSet &cs); ColorSet & operator=(const ColorSet &cs); @@ -56,6 +38,9 @@ public: status_t SetColor(const char *string, rgb_color value); + static status_t LoadColorSet(const char *path, ColorSet *set); + static status_t SaveColorSet(const char *path, const ColorSet &set); + rgb_color panel_background, panel_text, @@ -92,12 +77,9 @@ public: inactive_window_tab, inactive_window_tab_text; -private: + private: rgb_color *StringToMember(const char *string); void PrintMember(const rgb_color &color) const; }; -status_t LoadColorSet(const char *path, ColorSet *set); -status_t SaveColorSet(const char *path, const ColorSet &set); - -#endif +#endif // COLOR_SET_H diff --git a/src/kits/interface/ColorUtils.cc b/src/kits/interface/ColorUtils.cc deleted file mode 100644 index 1eba71dc85..0000000000 --- a/src/kits/interface/ColorUtils.cc +++ /dev/null @@ -1,287 +0,0 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, OpenBeOS -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: ColotUtils.cc -// Author: DarkWyrm (bpmagic@columbus.rr.com) -// Description: Useful global functions for working with rgb_color structures -//------------------------------------------------------------------------------ -#include "ColorUtils.h" -#include - -/*! - \brief An approximation of 31/255, which is needed for converting from 32-bit - colors to 16-bit and 15-bit. -*/ -#define RATIO_8_TO_5_BIT .121568627451 - -/*! - \brief An approximation of 63/255, which is needed for converting from 32-bit - colors to 16-bit. -*/ -#define RATIO_8_TO_6_BIT .247058823529 - -/*! - \brief An approximation of 255/31, which is needed for converting from 16-bit - and 15-bit colors to 32-bit. -*/ -#define RATIO_5_TO_8_BIT 8.22580645161 - -/*! - \brief An approximation of 255/63, which is needed for converting from 16-bit - colors to 32-bit. -*/ -#define RATIO_6_TO_8_BIT 4.04761904762 - -/*! - \brief Function for easy assignment of values to rgb_color objects - \param col Pointer to an rgb_color - \param r red value - \param g green value - \param b blue value - \param a alpha value, defaults to 255 - - This function will do nothing if given a NULL color pointer. -*/ -void SetRGBColor(rgb_color *col,uint8 r, uint8 g, uint8 b, uint8 a) -{ - if(col) - { - col->red=r; - col->green=g; - col->blue=b; - col->alpha=a; - } -} - -/*! - \brief Function for easy conversion of 15-bit colors to 32-bit - \param col Pointer to an rgb_color. - \param color RGBA16 color - - This function will do nothing if passed a NULL 32-bit color. -*/ -void SetRGBColor15(rgb_color *col,uint16 color) -{ - if(!col) - return; - - uint16 r16,g16,b16; - - // alpha's the easy part - col->alpha=(color & 0x8000)?255:0; - - r16= (color >> 10) & 31; - g16= (color >> 5) & 31; - b16= color & 31; - - col->red=uint8(r16 * RATIO_5_TO_8_BIT); - col->green=uint8(g16 * RATIO_5_TO_8_BIT); - col->blue=uint8(b16 * RATIO_5_TO_8_BIT); -} - -/*! - \brief Function for easy conversion of 16-bit colors to 32-bit - \param col Pointer to an rgb_color. - \param color RGB16 color - - This function will do nothing if passed a NULL 32-bit color. -*/ -void SetRGBColor16(rgb_color *col,uint16 color) -{ - if(!col) - return; - - uint16 r16,g16,b16; - - // alpha's the easy part - col->alpha=0; - - r16= (color >> 11) & 31; - g16= (color >> 5) & 63; - b16= color & 31; - - col->red=uint8(r16 * RATIO_5_TO_8_BIT); - col->green=uint8(g16 * RATIO_6_TO_8_BIT); - col->blue=uint8(b16 * RATIO_5_TO_8_BIT); -} - -/*! - \brief Function for easy conversion of 32-bit integer colors to an rgb_color structure - \param col Pointer to an rgb_color. - \param color 32-bit color as an integer - - This function will do nothing if passed a NULL 32-bit color. -*/ -void SetRGBColor(rgb_color *col,uint32 color) -{ - if(!col) - return; - - int8 *p8=(int8*)&color; - col->blue=p8[0]; - col->red=p8[1]; - col->green=p8[2]; - col->alpha=p8[3]; -} - -/*! - \brief Finds the index of the closest matching color in a rgb_color palette array - \param palette Array of 256 rgb_color objects - \param color Color to match - \return Index of the closest matching color - - Note that passing a NULL palette will always return 0 and passing an array of less - than 256 rgb_colors will cause a crash. -*/ -uint8 FindClosestColor(const rgb_color *palette, rgb_color color) -{ - if(!palette) - return 0; - - uint16 cindex=0,cdelta=765,delta=765; - - for(uint16 i=0;i<256;i++) - { - const rgb_color *c=&(palette[i]); - delta=abs(c->red-color.red)+abs(c->green-color.green)+ - abs(c->blue-color.blue); - - if(delta==0) - { - cindex=i; - break; - } - - if(delta127)?0x8000:0; - - color16 |= r16 << 10; - color16 |= g16 << 5; - color16 |= b16; - - return color16; -} - -/*! - \brief Constructs a RGB16 color which best matches a given 32-bit color - \param color Color to match - \return The closest matching color's value - - Format is RGB, 5:6:5 -*/ -uint16 FindClosestColor16(rgb_color color) -{ - uint16 r16,g16,b16; - uint16 color16=0; - - r16=uint16(color.red * RATIO_8_TO_5_BIT); - g16=uint16(color.green * RATIO_8_TO_6_BIT); - b16=uint16(color.blue * RATIO_8_TO_5_BIT); - - color16 |= r16 << 11; - color16 |= g16 << 5; - color16 |= b16; - - return color16; -} - -// Function which could be used to . Position is -// Any number outside -// this range will cause the function to fail and return the color (0,0,0,0) -// Alpha components are included in the calculations. 0 yields color #1 -// and 1 yields color #2. - -/*! - \brief Function mostly for calculating gradient colors - \param col Start color - \param col2 End color - \param position A floating point number such that 0.0 <= position <= 1.0. 0.0 results in the - start color and 1.0 results in the end color. - \return The blended color. If an invalid position was given, {0,0,0,0} is returned. -*/ -rgb_color MakeBlendColor(rgb_color col, rgb_color col2, float position) -{ - rgb_color newcol={0,0,0,0}; - float mod=0; - int16 delta; - if(position<0 || position>1) - return newcol; - - delta=int16(col2.red)-int16(col.red); - mod=col.red + (position * delta); - newcol.red=uint8(mod); - if(mod>255 ) - newcol.red=255; - if(mod<0 ) - newcol.red=0; - - delta=int16(col2.green)-int16(col.green); - mod=col.green + (position * delta); - newcol.green=uint8(mod); - if(mod>255 ) - newcol.green=255; - if(mod<0 ) - newcol.green=0; - - delta=int16(col2.blue)-int16(col.blue); - mod=col.blue + (position * delta); - newcol.blue=uint8(mod); - if(mod>255 ) - newcol.blue=255; - if(mod<0 ) - newcol.blue=0; - - delta=int8(col2.alpha)-int8(col.alpha); - mod=col.alpha + (position * delta); - newcol.alpha=uint8(mod); - if(mod>255 ) - newcol.alpha=255; - if(mod<0 ) - newcol.alpha=0; - - return newcol; -} diff --git a/src/kits/interface/Jamfile b/src/kits/interface/Jamfile index fbafc77c3f..dd730f0624 100644 --- a/src/kits/interface/Jamfile +++ b/src/kits/interface/Jamfile @@ -40,7 +40,6 @@ MergeObject interface_kit.o : CheckBox.cpp ColorConversion.cpp ColorControl.cpp - ColorUtils.cc Control.cpp Deskbar.cpp Dragger.cpp diff --git a/src/servers/app/AppServer.cpp b/src/servers/app/AppServer.cpp index 0496699013..271d82aefc 100644 --- a/src/servers/app/AppServer.cpp +++ b/src/servers/app/AppServer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2005, Haiku, Inc. + * Copyright (c) 2001-2006, Haiku, Inc. * Distributed under the terms of the MIT license. * * Authors: @@ -72,7 +72,7 @@ AppServer::AppServer() // Load the GUI colors here and set the global set to the values contained therein. If this // is not possible, set colors to the defaults - if (LoadColorSet(SERVER_SETTINGS_DIR COLOR_SETTINGS_NAME, &gGUIColorSet) != B_OK) + if (ColorSet::LoadColorSet(SERVER_SETTINGS_DIR COLOR_SETTINGS_NAME, &gGUIColorSet) != B_OK) gGUIColorSet.SetToDefaults(); gScreenManager = new ScreenManager(); diff --git a/src/servers/app/ColorSet.cpp b/src/servers/app/ColorSet.cpp index d730ea5839..006983695a 100644 --- a/src/servers/app/ColorSet.cpp +++ b/src/servers/app/ColorSet.cpp @@ -19,7 +19,20 @@ #include "ColorSet.h" #include "ServerConfig.h" -//! Constructor which does nothing + +static void +set_rgb_color(rgb_color& color, uint8 red, uint8 green, uint8 blue) +{ + color.red = red; + color.green = green; + color.blue = blue; + color.alpha = 255; +} + + +// #pragma mark - + + ColorSet::ColorSet() { } @@ -136,32 +149,31 @@ ColorSet::SetToDefaults(void) #ifdef DEBUG_COLORSET printf("Initializing color settings to defaults\n"); #endif - SetRGBColor(&panel_background, 216, 216, 216); - SetRGBColor(&panel_text, 0, 0, 0); - SetRGBColor(&document_background, 255, 255, 255); - SetRGBColor(&document_text, 0, 0, 0); - SetRGBColor(&control_background, 245, 245, 245); - SetRGBColor(&control_text, 0, 0, 0); - SetRGBColor(&control_border, 0, 0, 0); - SetRGBColor(&control_highlight, 102, 152, 203); - SetRGBColor(&keyboard_navigation_base, 0, 0, 229); - SetRGBColor(&keyboard_navigation_pulse, 0, 0, 0); - SetRGBColor(&shine, 255, 255, 255); - SetRGBColor(&shadow, 0, 0, 0); - SetRGBColor(&menu_background, 216, 216, 216); - SetRGBColor(&menu_selected_background, 115, 120, 184); - SetRGBColor(&menu_text, 0, 0, 0); - SetRGBColor(&menu_selected_text, 255, 255, 255); - SetRGBColor(&menu_selected_border, 0, 0, 0); - SetRGBColor(&tooltip_background, 255, 255, 0); - SetRGBColor(&tooltip_text, 0, 0, 0); - SetRGBColor(&success, 0, 255, 0); - SetRGBColor(&failure, 255, 0, 0); - SetRGBColor(&window_tab, 255, 203, 0); - - SetRGBColor(&window_tab_text, 0, 0, 0); - SetRGBColor(&inactive_window_tab, 232, 232, 232); - SetRGBColor(&inactive_window_tab_text, 80, 80, 80); + set_rgb_color(panel_background, 216, 216, 216); + set_rgb_color(panel_text, 0, 0, 0); + set_rgb_color(document_background, 255, 255, 255); + set_rgb_color(document_text, 0, 0, 0); + set_rgb_color(control_background, 245, 245, 245); + set_rgb_color(control_text, 0, 0, 0); + set_rgb_color(control_border, 0, 0, 0); + set_rgb_color(control_highlight, 102, 152, 203); + set_rgb_color(keyboard_navigation_base, 0, 0, 229); + set_rgb_color(keyboard_navigation_pulse, 0, 0, 0); + set_rgb_color(shine, 255, 255, 255); + set_rgb_color(shadow, 0, 0, 0); + set_rgb_color(menu_background, 216, 216, 216); + set_rgb_color(menu_selected_background, 115, 120, 184); + set_rgb_color(menu_text, 0, 0, 0); + set_rgb_color(menu_selected_text, 255, 255, 255); + set_rgb_color(menu_selected_border, 0, 0, 0); + set_rgb_color(tooltip_background, 255, 255, 0); + set_rgb_color(tooltip_text, 0, 0, 0); + set_rgb_color(success, 0, 255, 0); + set_rgb_color(failure, 255, 0, 0); + set_rgb_color(window_tab, 255, 203, 0); + set_rgb_color(window_tab_text, 0, 0, 0); + set_rgb_color(inactive_window_tab, 232, 232, 232); + set_rgb_color(inactive_window_tab_text, 80, 80, 80); } /*! @@ -539,50 +551,44 @@ ColorSet::PrintMember(const rgb_color &color) const printf("rgb_color(%d, %d, %d, %d)", color.red,color.green,color.blue,color.alpha); } + /*! \brief Loads the saved system colors into a ColorSet \param set the set to receive the system colors \return B_OK if successful. See BFile for other error codes */ status_t -LoadColorSet(const char *path, ColorSet *set) +ColorSet::LoadColorSet(const char *path, ColorSet *set) { BFile file(path,B_READ_ONLY); - if(file.InitCheck()!=B_OK) + if (file.InitCheck()!=B_OK) return file.InitCheck(); - + BMessage msg; - - status_t st=msg.Unflatten(&file); - if(st!=B_OK) - return st; + status_t status = msg.Unflatten(&file); + if (status != B_OK) + return status; set->ConvertFromMessage(&msg); - return B_OK; } + /*! \brief Saves the saved system colors into a flattened BMessage \param set ColorSet containing the colors to save \return B_OK if successful. See BFile for other error codes */ status_t -SaveColorSet(const char *path, const ColorSet &set) +ColorSet::SaveColorSet(const char *path, const ColorSet &set) { - // TODO: Move this check to the app_server - BEntry entry(SERVER_SETTINGS_DIR); - if(!entry.Exists()) - create_directory(SERVER_SETTINGS_DIR,0777); - BFile file(path, B_READ_WRITE | B_ERASE_FILE | B_CREATE_FILE); - if(file.InitCheck()!=B_OK) - return file.InitCheck(); + status_t status = file.InitCheck(); + if (status != B_OK) + return status; BMessage msg; - set.ConvertToMessage(&msg); - msg.Flatten(&file); - return B_OK; + return msg.Flatten(&file); } diff --git a/src/servers/app/DefaultDecorator.cpp b/src/servers/app/DefaultDecorator.cpp index 472faf9c64..04c707b641 100644 --- a/src/servers/app/DefaultDecorator.cpp +++ b/src/servers/app/DefaultDecorator.cpp @@ -12,7 +12,6 @@ #include "DefaultDecorator.h" -#include "ColorUtils.h" #include "DesktopSettings.h" #include "DrawingEngine.h" #include "DrawState.h" @@ -36,10 +35,52 @@ #endif +static inline uint8 +blend_color_value(uint8 a, uint8 b, float position) +{ + int16 delta = (int16)a - b; + int32 value = a + (int32)(position * delta); + if (value > 255) + return 255; + if (value < 0) + return 0; + + return (uint8)value; +} + + +/*! + \brief Function mostly for calculating gradient colors + \param col Start color + \param col2 End color + \param position A floating point number such that 0.0 <= position <= 1.0. 0.0 results in the + start color and 1.0 results in the end color. + \return The blended color. If an invalid position was given, {0,0,0,0} is returned. +*/ +static rgb_color +make_blend_color(rgb_color colorA, rgb_color colorB, float position) +{ + if (position < 0) + return colorA; + if (position > 1) + return colorB; + + rgb_color blendColor; + blendColor.red = blend_color_value(colorA.red, colorB.red, position); + blendColor.green = blend_color_value(colorA.green, colorB.green, position); + blendColor.blue = blend_color_value(colorA.blue, colorB.blue, position); + blendColor.alpha = blend_color_value(colorA.alpha, colorB.alpha, position); + + return blendColor; +} + + +// #pragma mark - + + // TODO: get rid of DesktopSettings here, and introduce private accessor // methods to the Decorator base class - DefaultDecorator::DefaultDecorator(DesktopSettings& settings, BRect rect, window_look look, uint32 flags) : Decorator(settings, rect, look, flags), @@ -924,38 +965,38 @@ DefaultDecorator::_DrawBlendedRect(BRect r, bool down) // Actually just draws a blended square int32 w = r.IntegerWidth(); int32 h = r.IntegerHeight(); - + RGBColor temprgbcol; - rgb_color halfcol, startcol, endcol; + rgb_color halfColor, startColor, endColor; float rstep, gstep, bstep; int steps = w < h ? w : h; if (down) { - startcol = fButtonLowColor.GetColor32(); - endcol = fButtonHighColor.GetColor32(); + startColor = fButtonLowColor.GetColor32(); + endColor = fButtonHighColor.GetColor32(); } else { - startcol = fButtonHighColor.GetColor32(); - endcol = fButtonLowColor.GetColor32(); + startColor = fButtonHighColor.GetColor32(); + endColor = fButtonLowColor.GetColor32(); } - halfcol = MakeBlendColor(startcol,endcol,0.5); + halfColor = make_blend_color(startColor, endColor, 0.5); - rstep = float(startcol.red - halfcol.red) / steps; - gstep = float(startcol.green - halfcol.green) / steps; - bstep = float(startcol.blue - halfcol.blue) / steps; + rstep = float(startColor.red - halfColor.red) / steps; + gstep = float(startColor.green - halfColor.green) / steps; + bstep = float(startColor.blue - halfColor.blue) / steps; for (int32 i = 0; i <= steps; i++) { - temprgbcol.SetColor(uint8(startcol.red - (i * rstep)), - uint8(startcol.green - (i * gstep)), - uint8(startcol.blue - (i * bstep))); + temprgbcol.SetColor(uint8(startColor.red - (i * rstep)), + uint8(startColor.green - (i * gstep)), + uint8(startColor.blue - (i * bstep))); _driver->StrokeLine(BPoint(r.left, r.top + i), BPoint(r.left + i, r.top), temprgbcol); - temprgbcol.SetColor(uint8(halfcol.red - (i * rstep)), - uint8(halfcol.green - (i * gstep)), - uint8(halfcol.blue - (i * bstep))); + temprgbcol.SetColor(uint8(halfColor.red - (i * rstep)), + uint8(halfColor.green - (i * gstep)), + uint8(halfColor.blue - (i * bstep))); _driver->StrokeLine(BPoint(r.left + steps, r.top + i), BPoint(r.left + i, r.top + steps), temprgbcol); diff --git a/src/servers/app/RGBColor.cpp b/src/servers/app/RGBColor.cpp index 11a3a93c21..b2717c0bea 100644 --- a/src/servers/app/RGBColor.cpp +++ b/src/servers/app/RGBColor.cpp @@ -10,11 +10,149 @@ #include "RGBColor.h" #include "SystemPalette.h" -#include - #include +/*! + \brief An approximation of 31/255, which is needed for converting from 32-bit + colors to 16-bit and 15-bit. +*/ +#define RATIO_8_TO_5_BIT .121568627451 + +/*! + \brief An approximation of 63/255, which is needed for converting from 32-bit + colors to 16-bit. +*/ +#define RATIO_8_TO_6_BIT .247058823529 + +/*! + \brief An approximation of 255/31, which is needed for converting from 16-bit + and 15-bit colors to 32-bit. +*/ +#define RATIO_5_TO_8_BIT 8.22580645161 + +/*! + \brief An approximation of 255/63, which is needed for converting from 16-bit + colors to 32-bit. +*/ +#define RATIO_6_TO_8_BIT 4.04761904762 + +#if 0 +/*! + \brief Function for easy conversion of 16-bit colors to 32-bit + \param col Pointer to an rgb_color. + \param color RGB16 color + + This function will do nothing if passed a NULL 32-bit color. +*/ +void +SetRGBColor16(rgb_color *col,uint16 color) +{ + if(!col) + return; + + uint16 r16,g16,b16; + + // alpha's the easy part + col->alpha=0; + + r16= (color >> 11) & 31; + g16= (color >> 5) & 63; + b16= color & 31; + + col->red=uint8(r16 * RATIO_5_TO_8_BIT); + col->green=uint8(g16 * RATIO_6_TO_8_BIT); + col->blue=uint8(b16 * RATIO_5_TO_8_BIT); +} +#endif + +/*! + \brief Finds the index of the closest matching color in a rgb_color palette array + \param palette Array of 256 rgb_color objects + \param color Color to match + \return Index of the closest matching color + + Note that passing a NULL palette will always return 0 and passing an array of less + than 256 rgb_colors will cause a crash. +*/ +static uint8 +FindClosestColor(const rgb_color *palette, rgb_color color) +{ + if (!palette) + return 0; + + uint16 cindex = 0, cdelta = 765, delta = 765; + + for (uint16 i = 0; i < 256; i++) { + const rgb_color *c = &(palette[i]); + delta = abs(c->red-color.red) + abs(c->green-color.green) + + abs(c->blue-color.blue); + + if (delta == 0) { + cindex = i; + break; + } + + if (delta < cdelta) { + cindex = i; + cdelta = delta; + } + } + + return (uint8)cindex; +} + + +/*! + \brief Constructs a RGBA15 color which best matches a given 32-bit color + \param color Color to match + \return The closest matching color's value + + Format is ARGB, 1:5:5:5 +*/ +static uint16 +FindClosestColor15(rgb_color color) +{ + uint16 r16 = uint16(color.red * RATIO_8_TO_5_BIT); + uint16 g16 = uint16(color.green * RATIO_8_TO_5_BIT); + uint16 b16 = uint16(color.blue * RATIO_8_TO_5_BIT); + + // start with alpha value + uint16 color16 = color.alpha > 127 ? 0x8000 : 0; + + color16 |= r16 << 10; + color16 |= g16 << 5; + color16 |= b16; + + return color16; +} + + +/*! + \brief Constructs a RGB16 color which best matches a given 32-bit color + \param color Color to match + \return The closest matching color's value + + Format is RGB, 5:6:5 +*/ +static uint16 +FindClosestColor16(rgb_color color) +{ + uint16 r16 = uint16(color.red * RATIO_8_TO_5_BIT); + uint16 g16 = uint16(color.green * RATIO_8_TO_6_BIT); + uint16 b16 = uint16(color.blue * RATIO_8_TO_5_BIT); + + uint16 color16 = r16 << 11; + color16 |= g16 << 5; + color16 |= b16; + + return color16; +} + + +// #pragma mark - + + /*! \brief Create an RGBColor from specified values \param red red @@ -50,7 +188,7 @@ RGBColor::RGBColor(const rgb_color &color) SetColor(color); } - +#if 0 /*! \brief Create an RGBColor from a 16-bit RGBA color \param color color to initialize from @@ -59,7 +197,7 @@ RGBColor::RGBColor(uint16 color) { SetColor(color); } - +#endif /*! \brief Create an RGBColor from an index color @@ -190,7 +328,7 @@ RGBColor::SetColor(int r, int g, int b, int a) fUpdate8 = fUpdate16 = true; } - +#if 0 /*! \brief Set the object to specified value \param col16 color to copy @@ -204,6 +342,7 @@ RGBColor::SetColor(uint16 col16) fUpdate8 = true; fUpdate16 = false; } +#endif /*!