2002-12-31 21:21:14 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-21 02:50:02 +04:00
|
|
|
// Copyright (c) 2001-2002, Haiku, Inc.
|
2002-12-31 21:21:14 +03:00
|
|
|
//
|
|
|
|
// 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: RGBColor.cpp
|
|
|
|
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
// Description: Color encapsulation class for the app_server
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Standard Includes -----------------------------------------------------------
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// Local Includes --------------------------------------------------------------
|
|
|
|
#include "RGBColor.h"
|
2003-01-01 01:19:48 +03:00
|
|
|
#include "SystemPalette.h"
|
2003-01-18 23:12:03 +03:00
|
|
|
#include <ColorUtils.h>
|
2002-12-31 21:21:14 +03:00
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor from specified values
|
2003-01-20 02:04:58 +03:00
|
|
|
\param red red
|
|
|
|
\param green green
|
|
|
|
\param blue blue
|
|
|
|
\param alpha alpha, defaults to 255
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2003-01-18 23:12:03 +03:00
|
|
|
RGBColor::RGBColor(uint8 r, uint8 g, uint8 b, uint8 a)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
SetColor(r,g,b,a);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor from specified values
|
2003-01-20 02:04:58 +03:00
|
|
|
\param red red
|
|
|
|
\param green green
|
|
|
|
\param blue blue
|
|
|
|
\param alpha alpha, defaults to 255
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2003-06-23 06:54:52 +04:00
|
|
|
RGBColor::RGBColor(int r, int g, int b, int a)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
SetColor(r,g,b,a);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor from an rgb_color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to initialize from
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
|
|
|
RGBColor::RGBColor(const rgb_color &col)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
SetColor(col);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor from a 16-bit RGBA color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to initialize from
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
RGBColor::RGBColor(uint16 col)
|
|
|
|
{
|
|
|
|
SetColor(col);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor from an index color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to initialize from
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
RGBColor::RGBColor(uint8 col)
|
|
|
|
{
|
|
|
|
SetColor(col);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Copy Contructor
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to initialize from
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
RGBColor::RGBColor(const RGBColor &col)
|
|
|
|
{
|
|
|
|
color32=col.color32;
|
|
|
|
color16=col.color16;
|
|
|
|
color8=col.color8;
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=col.update8;
|
|
|
|
update16=col.update16;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Create an RGBColor with the values(0,0,0,0)
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-05-05 03:48:19 +04:00
|
|
|
RGBColor::RGBColor()
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
SetColor(0,0,0,0);
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=update16=false;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Returns the color as the closest 8-bit color in the palette
|
2003-01-01 01:19:48 +03:00
|
|
|
\return The palette index for the current color
|
|
|
|
*/
|
2005-05-05 03:48:19 +04:00
|
|
|
uint8 RGBColor::GetColor8() const
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
2003-10-04 23:57:54 +04:00
|
|
|
if(update8)
|
|
|
|
{
|
|
|
|
color8=FindClosestColor(system_palette, color32);
|
|
|
|
update8=false;
|
|
|
|
}
|
|
|
|
|
2002-12-31 21:21:14 +03:00
|
|
|
return color8;
|
|
|
|
}
|
|
|
|
|
2003-11-03 04:51:50 +03:00
|
|
|
/*!
|
|
|
|
\brief Returns the color as the closest 15-bit color
|
|
|
|
\return 15-bit value of the current color plus 1-bit alpha
|
|
|
|
*/
|
2005-05-05 03:48:19 +04:00
|
|
|
uint16 RGBColor::GetColor15() const
|
2003-11-03 04:51:50 +03:00
|
|
|
{
|
|
|
|
if(update15)
|
|
|
|
{
|
|
|
|
color15=FindClosestColor15(color32);
|
|
|
|
update15=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return color15;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Returns the color as the closest 16-bit color
|
2003-11-03 04:51:50 +03:00
|
|
|
\return 16-bit value of the current color
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-05-05 03:48:19 +04:00
|
|
|
uint16 RGBColor::GetColor16() const
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
2003-10-04 23:57:54 +04:00
|
|
|
if(update16)
|
|
|
|
{
|
|
|
|
color16=FindClosestColor16(color32);
|
|
|
|
update16=false;
|
|
|
|
}
|
|
|
|
|
2002-12-31 21:21:14 +03:00
|
|
|
return color16;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Returns the color as a 32-bit color
|
|
|
|
\return current color, including alpha
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-05-05 03:48:19 +04:00
|
|
|
rgb_color RGBColor::GetColor32() const
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
return color32;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified values
|
2003-01-20 02:04:58 +03:00
|
|
|
\param red red
|
|
|
|
\param green green
|
|
|
|
\param blue blue
|
|
|
|
\param alpha alpha, defaults to 255
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2003-06-23 06:54:52 +04:00
|
|
|
void RGBColor::SetColor(uint8 r, uint8 g, uint8 b, uint8 a)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
color32.red=r;
|
|
|
|
color32.green=g;
|
|
|
|
color32.blue=b;
|
|
|
|
color32.alpha=a;
|
2003-10-04 23:57:54 +04:00
|
|
|
|
|
|
|
update8=update16=true;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified values
|
2003-01-20 02:04:58 +03:00
|
|
|
\param red red
|
|
|
|
\param green green
|
|
|
|
\param blue blue
|
|
|
|
\param alpha alpha, defaults to 255
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2003-06-23 06:54:52 +04:00
|
|
|
void RGBColor::SetColor(int r, int g, int b, int a)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
color32.red=(uint8)r;
|
|
|
|
color32.green=(uint8)g;
|
|
|
|
color32.blue=(uint8)b;
|
|
|
|
color32.alpha=(uint8)a;
|
2003-10-04 23:57:54 +04:00
|
|
|
|
|
|
|
update8=update16=true;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified value
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col16 color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
void RGBColor::SetColor(uint16 col16)
|
|
|
|
{
|
|
|
|
color16=col16;
|
2003-01-18 23:12:03 +03:00
|
|
|
SetRGBColor(&color32,col16);
|
2003-10-04 23:57:54 +04:00
|
|
|
|
|
|
|
update8=true;
|
|
|
|
update16=false;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified index in the palette
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col8 color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
void RGBColor::SetColor(uint8 col8)
|
|
|
|
{
|
|
|
|
color8=col8;
|
2003-01-18 23:12:03 +03:00
|
|
|
color32=system_palette[col8];
|
2003-10-04 23:57:54 +04:00
|
|
|
|
|
|
|
update8=false;
|
|
|
|
update16=true;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param color color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
void RGBColor::SetColor(const rgb_color &color)
|
|
|
|
{
|
|
|
|
color32=color;
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=update16=true;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
void RGBColor::SetColor(const RGBColor &col)
|
|
|
|
{
|
|
|
|
color32=col.color32;
|
|
|
|
color16=col.color16;
|
|
|
|
color8=col.color8;
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=col.update8;
|
|
|
|
update16=col.update16;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2004-01-12 01:12:55 +03:00
|
|
|
const RGBColor & RGBColor::operator=(const RGBColor &col)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
color32=col.color32;
|
|
|
|
color16=col.color16;
|
|
|
|
color8=col.color8;
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=col.update8;
|
|
|
|
update16=col.update16;
|
2002-12-31 21:21:14 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Set the object to specified color
|
2003-01-20 02:04:58 +03:00
|
|
|
\param col color to copy
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2004-01-12 01:12:55 +03:00
|
|
|
const RGBColor & RGBColor::operator=(const rgb_color &col)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
color32=col;
|
2003-10-04 23:57:54 +04:00
|
|
|
update8=update16=true;
|
2002-12-31 21:21:14 +03:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-01-18 23:12:03 +03:00
|
|
|
/*!
|
|
|
|
\brief Returns a color blended between the object's value and
|
2002-12-31 21:21:14 +03:00
|
|
|
another color.
|
|
|
|
|
2003-01-20 02:04:58 +03:00
|
|
|
\param color The other color to be blended with.
|
|
|
|
\param position A weighted percentage of the second color to use. 0 <= value <= 1.0
|
2002-12-31 21:21:14 +03:00
|
|
|
\return The blended color
|
2003-01-18 23:12:03 +03:00
|
|
|
|
|
|
|
If the position passed to this function is invalid, the starting
|
|
|
|
color will be returned.
|
2002-12-31 21:21:14 +03:00
|
|
|
*/
|
2003-07-05 21:40:42 +04:00
|
|
|
RGBColor RGBColor::MakeBlendColor(const RGBColor &color, const float &position)
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
rgb_color col=color32;
|
|
|
|
rgb_color col2=color.color32;
|
|
|
|
|
|
|
|
rgb_color newcol={0,0,0,0};
|
|
|
|
float mod=0;
|
|
|
|
int16 delta;
|
|
|
|
if(position<0 || position>1)
|
2003-01-18 23:12:03 +03:00
|
|
|
return *this;
|
2002-12-31 21:21:14 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2003-10-04 23:57:54 +04:00
|
|
|
delta=int16(col2.green)-int16(col.green);
|
2002-12-31 21:21:14 +03:00
|
|
|
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 RGBColor(newcol);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Prints the 32-bit values of the color to standard out
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2003-04-05 05:51:35 +04:00
|
|
|
void RGBColor::PrintToStream(void) const
|
2002-12-31 21:21:14 +03:00
|
|
|
{
|
|
|
|
printf("RGBColor (%u,%u,%u,%u)\n", color32.red,color32.green,color32.blue,color32.alpha);
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Overloaded comaparison
|
2003-01-01 01:19:48 +03:00
|
|
|
\return true if all color elements are exactly equal
|
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
bool RGBColor::operator==(const rgb_color &col)
|
|
|
|
{
|
|
|
|
return (color32.red==col.red && color32.green==col.green
|
2003-01-18 23:12:03 +03:00
|
|
|
&& color32.blue==col.blue && color32.alpha==col.alpha)?true:false;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
2003-01-18 23:12:03 +03:00
|
|
|
\brief Overloaded comaparison
|
2003-01-01 01:19:48 +03:00
|
|
|
\return true if all color elements are exactly equal
|
|
|
|
*/
|
2002-12-31 21:21:14 +03:00
|
|
|
bool RGBColor::operator==(const RGBColor &col)
|
|
|
|
{
|
|
|
|
return (color32.red==col.color32.red && color32.green==col.color32.green
|
2003-01-18 23:12:03 +03:00
|
|
|
&& color32.blue==col.color32.blue
|
|
|
|
&& color32.alpha==col.color32.alpha)?true:false;
|
2002-12-31 21:21:14 +03:00
|
|
|
}
|