2013-03-28 16:42:15 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2017-07-16 15:10:24 +00:00
|
|
|
* Copyright 2012 Torsten Hueter, torstenhtr <at> gmx.de
|
2021-07-26 17:28:37 +00:00
|
|
|
* Copyright 2017-2021 Kicad Developers, see AUTHORS.txt for contributors.
|
2013-03-28 16:42:15 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
#include <map>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2013-03-28 16:42:15 +00:00
|
|
|
#include <gal/color4d.h>
|
2020-06-19 02:32:14 +00:00
|
|
|
#include <i18n_utility.h>
|
2021-08-31 17:55:26 +00:00
|
|
|
#include <wx/crt.h>
|
|
|
|
#include <math/util.h>
|
2013-03-28 16:42:15 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-03-28 16:42:15 +00:00
|
|
|
|
2020-06-19 02:32:14 +00:00
|
|
|
#define TS( string ) wxString( _HKI( string ) ).ToStdString()
|
|
|
|
|
2020-06-19 16:05:18 +00:00
|
|
|
// We can't have this as a plain static variable, because it is referenced during the initialization
|
|
|
|
// of other static variables, so we must initialize it explicitly on first use.
|
|
|
|
const StructColors* colorRefs()
|
2020-06-19 02:32:14 +00:00
|
|
|
{
|
2020-06-19 16:05:18 +00:00
|
|
|
static StructColors s_ColorRefs[NBCOLORS] =
|
|
|
|
{
|
|
|
|
{ 0, 0, 0, BLACK, TS( "Black" ), DARKDARKGRAY },
|
|
|
|
{ 72, 72, 72, DARKDARKGRAY, TS( "Gray 1" ), DARKGRAY },
|
|
|
|
{ 132, 132, 132, DARKGRAY, TS( "Gray 2" ), LIGHTGRAY },
|
|
|
|
{ 194, 194, 194, LIGHTGRAY, TS( "Gray 3" ), WHITE },
|
|
|
|
{ 255, 255, 255, WHITE, TS( "White" ), WHITE },
|
|
|
|
{ 194, 255, 255, LIGHTYELLOW, TS( "L.Yellow" ), WHITE },
|
2021-05-28 18:49:34 +00:00
|
|
|
{ 191, 229, 255, LIGHTERORANGE, TS( "L.Orange" ), WHITE },
|
2020-06-19 16:05:18 +00:00
|
|
|
{ 72, 0, 0, DARKBLUE, TS( "Blue 1" ), BLUE },
|
|
|
|
{ 0, 72, 0, DARKGREEN, TS( "Green 1" ), GREEN },
|
|
|
|
{ 72, 72, 0, DARKCYAN, TS( "Cyan 1" ), CYAN },
|
|
|
|
{ 0, 0, 72, DARKRED, TS( "Red 1" ), RED },
|
|
|
|
{ 72, 0, 72, DARKMAGENTA, TS( "Magenta 1" ), MAGENTA },
|
|
|
|
{ 0, 72, 72, DARKBROWN, TS( "Brown 1" ), BROWN },
|
2021-05-28 18:49:34 +00:00
|
|
|
{ 0, 77, 128, DARKORANGE, TS( "Orange 1" ), ORANGE },
|
2020-06-19 16:05:18 +00:00
|
|
|
{ 132, 0, 0, BLUE, TS( "Blue 2" ), LIGHTBLUE },
|
|
|
|
{ 0, 132, 0, GREEN, TS( "Green 2" ), LIGHTGREEN },
|
|
|
|
{ 132, 132, 0, CYAN, TS( "Cyan 2" ), LIGHTCYAN },
|
|
|
|
{ 0, 0, 132, RED, TS( "Red 2" ), LIGHTRED },
|
|
|
|
{ 132, 0, 132, MAGENTA, TS( "Magenta 2" ), LIGHTMAGENTA },
|
|
|
|
{ 0, 132, 132, BROWN, TS( "Brown 2" ), YELLOW },
|
2021-05-28 18:49:34 +00:00
|
|
|
{ 0, 102, 204, ORANGE, TS( "Orange 2" ), LIGHTORANGE },
|
2020-06-19 16:05:18 +00:00
|
|
|
{ 194, 0, 0, LIGHTBLUE, TS( "Blue 3" ), PUREBLUE, },
|
|
|
|
{ 0, 194, 0, LIGHTGREEN, TS( "Green 3" ), PUREGREEN },
|
|
|
|
{ 194, 194, 0, LIGHTCYAN, TS( "Cyan 3" ), PURECYAN },
|
|
|
|
{ 0, 0, 194, LIGHTRED, TS( "Red 3" ), PURERED },
|
|
|
|
{ 194, 0, 194, LIGHTMAGENTA, TS( "Magenta 3" ), PUREMAGENTA },
|
|
|
|
{ 0, 194, 194, YELLOW, TS( "Yellow 3" ), PUREYELLOW },
|
2021-05-28 18:49:34 +00:00
|
|
|
{ 0, 133, 221, LIGHTORANGE, TS( "Orange 3" ), PUREORANGE },
|
2020-06-19 16:05:18 +00:00
|
|
|
{ 255, 0, 0, PUREBLUE, TS( "Blue 4" ), WHITE },
|
|
|
|
{ 0, 255, 0, PUREGREEN, TS( "Green 4" ), WHITE },
|
|
|
|
{ 255, 255, 0, PURECYAN, TS( "Cyan 4" ), WHITE },
|
|
|
|
{ 0, 0, 255, PURERED, TS( "Red 4" ), WHITE },
|
|
|
|
{ 255, 0, 255, PUREMAGENTA, TS( "Magenta 4" ), WHITE },
|
|
|
|
{ 0, 255, 255, PUREYELLOW, TS( "Yellow 4" ), WHITE },
|
2021-05-28 18:49:34 +00:00
|
|
|
{ 0, 153, 255, PUREORANGE, TS( "Orange 4" ), WHITE },
|
2020-06-19 16:05:18 +00:00
|
|
|
};
|
|
|
|
return s_ColorRefs;
|
|
|
|
}
|
2020-06-19 02:32:14 +00:00
|
|
|
|
|
|
|
|
2013-06-24 08:12:36 +00:00
|
|
|
COLOR4D::COLOR4D( EDA_COLOR_T aColor )
|
|
|
|
{
|
2017-02-23 17:17:47 +00:00
|
|
|
if( aColor <= UNSPECIFIED_COLOR || aColor >= NBCOLORS )
|
|
|
|
{
|
|
|
|
*this = COLOR4D::UNSPECIFIED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:49:34 +00:00
|
|
|
int candidate = 0;
|
|
|
|
|
|
|
|
for( ; candidate < NBCOLORS; ++candidate )
|
|
|
|
{
|
|
|
|
if( colorRefs()[candidate].m_Numcolor == aColor )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( candidate >= NBCOLORS )
|
|
|
|
{
|
|
|
|
*this = COLOR4D::UNSPECIFIED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = colorRefs()[candidate].m_Red / 255.0;
|
|
|
|
g = colorRefs()[candidate].m_Green / 255.0;
|
|
|
|
b = colorRefs()[candidate].m_Blue / 255.0;
|
2013-06-24 08:12:36 +00:00
|
|
|
a = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 08:32:08 +00:00
|
|
|
#ifdef WX_COMPATIBILITY
|
2021-12-14 00:03:19 +00:00
|
|
|
COLOR4D::COLOR4D( const wxString& aColorStr )
|
|
|
|
{
|
|
|
|
if( !SetFromHexString( aColorStr ) )
|
|
|
|
SetFromWxString( aColorStr );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
COLOR4D::COLOR4D( const wxColour& aColor )
|
|
|
|
{
|
|
|
|
r = aColor.Red() / 255.0;
|
|
|
|
g = aColor.Green() / 255.0;
|
|
|
|
b = aColor.Blue() / 255.0;
|
|
|
|
a = aColor.Alpha() / 255.0;
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
bool COLOR4D::SetFromWxString( const wxString& aColorString )
|
|
|
|
{
|
|
|
|
wxColour c;
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
if( c.Set( aColorString ) )
|
2017-02-20 16:57:41 +00:00
|
|
|
{
|
2021-07-26 17:28:37 +00:00
|
|
|
r = c.Red() / 255.0;
|
|
|
|
g = c.Green() / 255.0;
|
|
|
|
b = c.Blue() / 255.0;
|
|
|
|
a = c.Alpha() / 255.0;
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
wxString COLOR4D::ToWxString( long flags ) const
|
|
|
|
{
|
|
|
|
wxColour c = ToColour();
|
|
|
|
return c.GetAsString( flags );
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
|
|
|
|
2021-08-31 17:55:26 +00:00
|
|
|
bool COLOR4D::SetFromHexString( const wxString& aColorString )
|
|
|
|
{
|
2021-08-31 20:23:46 +00:00
|
|
|
wxString str = aColorString;
|
|
|
|
str.Trim( true );
|
|
|
|
str.Trim( false );
|
|
|
|
|
|
|
|
if( str.length() < 7 || str.GetChar( 0 ) != '#' )
|
2021-08-31 17:55:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned long tmp;
|
|
|
|
|
2021-08-31 20:23:46 +00:00
|
|
|
if( wxSscanf( str.wx_str() + 1, wxT( "%lx" ), &tmp ) != 1 )
|
2021-08-31 17:55:26 +00:00
|
|
|
return false;
|
|
|
|
|
2021-08-31 20:23:46 +00:00
|
|
|
if( str.length() >= 9 )
|
|
|
|
{
|
|
|
|
r = ( (tmp >> 24) & 0xFF ) / 255.0;
|
|
|
|
g = ( (tmp >> 16) & 0xFF ) / 255.0;
|
|
|
|
b = ( (tmp >> 8) & 0xFF ) / 255.0;
|
|
|
|
a = ( tmp & 0xFF ) / 255.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r = ( (tmp >> 16) & 0xFF ) / 255.0;
|
|
|
|
g = ( (tmp >> 8) & 0xFF ) / 255.0;
|
|
|
|
b = ( tmp & 0xFF ) / 255.0;
|
|
|
|
a = 1.0;
|
|
|
|
}
|
2021-08-31 17:55:26 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString COLOR4D::ToHexString() const
|
|
|
|
{
|
|
|
|
return wxString::Format( wxT("#%02X%02X%02X%02X" ),
|
|
|
|
KiROUND( r * 255.0 ),
|
|
|
|
KiROUND( g * 255.0 ),
|
|
|
|
KiROUND( b * 255.0 ),
|
|
|
|
KiROUND( a * 255.0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
wxColour COLOR4D::ToColour() const
|
|
|
|
{
|
|
|
|
using CHAN_T = wxColourBase::ChannelType;
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
const wxColour colour(
|
|
|
|
static_cast<CHAN_T>( r * 255 + 0.5 ), static_cast<CHAN_T>( g * 255 + 0.5 ),
|
|
|
|
static_cast<CHAN_T>( b * 255 + 0.5 ), static_cast<CHAN_T>( a * 255 + 0.5 ) );
|
|
|
|
return colour;
|
|
|
|
}
|
2018-11-08 13:28:42 +00:00
|
|
|
|
2022-01-07 16:55:11 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-08 13:28:42 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
COLOR4D COLOR4D::LegacyMix( const COLOR4D& aColor ) const
|
|
|
|
{
|
|
|
|
COLOR4D candidate;
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
// Blend the two colors (i.e. OR the RGB values)
|
|
|
|
candidate.r = ( (unsigned) ( 255.0 * r ) | (unsigned) ( 255.0 * aColor.r ) ) / 255.0,
|
|
|
|
candidate.g = ( (unsigned) ( 255.0 * g ) | (unsigned) ( 255.0 * aColor.g ) ) / 255.0,
|
|
|
|
candidate.b = ( (unsigned) ( 255.0 * b ) | (unsigned) ( 255.0 * aColor.b ) ) / 255.0,
|
2017-07-04 10:48:47 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
// the alpha channel can be reinitialized but what is the best value?
|
|
|
|
candidate.a = ( aColor.a + a ) / 2;
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
return candidate;
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
unsigned int COLOR4D::ToU32() const
|
|
|
|
{
|
|
|
|
return ToColour().GetRGB();
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
void COLOR4D::FromU32( unsigned int aPackedColor )
|
|
|
|
{
|
|
|
|
wxColour c;
|
|
|
|
c.SetRGB( aPackedColor );
|
|
|
|
r = c.Red() / 255.0;
|
|
|
|
g = c.Green() / 255.0;
|
|
|
|
b = c.Blue() / 255.0;
|
|
|
|
a = c.Alpha() / 255.0;
|
|
|
|
}
|
2017-02-20 16:57:41 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
namespace KIGFX {
|
2013-06-24 08:32:08 +00:00
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs )
|
2013-03-28 16:42:15 +00:00
|
|
|
{
|
2017-02-20 16:57:41 +00:00
|
|
|
return lhs.a == rhs.a && lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b;
|
2013-03-28 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs )
|
2013-03-28 16:42:15 +00:00
|
|
|
{
|
2017-02-20 16:57:41 +00:00
|
|
|
return !( lhs == rhs );
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2020-04-09 23:34:38 +00:00
|
|
|
const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs )
|
|
|
|
{
|
|
|
|
if( lhs.r < rhs.r )
|
|
|
|
return true;
|
|
|
|
else if( lhs.g < rhs.g )
|
|
|
|
return true;
|
|
|
|
else if( lhs.b < rhs.b )
|
|
|
|
return true;
|
|
|
|
else if( lhs.a < rhs.a )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2017-03-31 02:09:19 +00:00
|
|
|
std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor )
|
|
|
|
{
|
|
|
|
return aStream << aColor.ToWxString( wxC2S_CSS_SYNTAX );
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
void to_json( nlohmann::json& aJson, const COLOR4D& aColor )
|
|
|
|
{
|
2022-07-20 16:51:59 +00:00
|
|
|
wxColour c = aColor.ToColour();
|
|
|
|
wxString str;
|
|
|
|
|
|
|
|
const int red = c.Red();
|
|
|
|
const int green = c.Green();
|
|
|
|
const int blue = c.Blue();
|
|
|
|
const int alpha = c.Alpha();
|
|
|
|
|
|
|
|
if ( alpha == wxALPHA_OPAQUE )
|
|
|
|
{
|
|
|
|
str.Printf( wxT( "rgb(%d, %d, %d)" ), red, green, blue );
|
|
|
|
}
|
|
|
|
else // use rgba() form
|
|
|
|
{
|
|
|
|
wxString a = wxString::FromCDouble( alpha / 255.0, 3);
|
|
|
|
|
|
|
|
// The wxC2S_CSS_SYNTAX is particularly sensitive to ','s (as it uses them for value
|
|
|
|
// delimiters), and wxWidgets is known to be buggy in this respect when dealing with
|
|
|
|
// Serbian and Russian locales (at least), so we enforce an extra level of safety.
|
|
|
|
a.Replace( wxT( "," ), wxT( "." ) );
|
|
|
|
|
|
|
|
str.Printf( wxT( "rgba(%d, %d, %d, %s)" ), red, green, blue, a );
|
|
|
|
}
|
|
|
|
|
|
|
|
aJson = nlohmann::json( str.ToStdString() );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void from_json( const nlohmann::json& aJson, COLOR4D& aColor )
|
|
|
|
{
|
|
|
|
aColor.SetFromWxString( aJson.get<std::string>() );
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:42:15 +00:00
|
|
|
}
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2019-02-12 03:55:03 +00:00
|
|
|
void COLOR4D::ToHSL( double& aOutHue, double& aOutSaturation, double& aOutLightness ) const
|
|
|
|
{
|
|
|
|
auto min = std::min( r, std::min( g, b ) );
|
|
|
|
auto max = std::max( r, std::max( g, b ) );
|
|
|
|
auto diff = max - min;
|
|
|
|
|
|
|
|
aOutLightness = ( max + min ) / 2.0;
|
|
|
|
|
|
|
|
if( aOutLightness >= 1.0 )
|
|
|
|
aOutSaturation = 0.0;
|
|
|
|
else
|
|
|
|
aOutSaturation = diff / ( 1.0 - std::abs( 2.0 * aOutLightness - 1.0 ) );
|
|
|
|
|
|
|
|
double hue;
|
|
|
|
|
|
|
|
if( diff <= 0.0 )
|
|
|
|
hue = 0.0;
|
|
|
|
else if( max == r )
|
|
|
|
hue = ( g - b ) / diff;
|
|
|
|
else if( max == g )
|
|
|
|
hue = ( b - r ) / diff + 2.0;
|
|
|
|
else
|
|
|
|
hue = ( r - g ) / diff + 4.0;
|
|
|
|
|
|
|
|
aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
|
|
|
|
|
|
|
|
while( aOutHue < 0.0 )
|
|
|
|
aOutHue += 360.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COLOR4D::FromHSL( double aInHue, double aInSaturation, double aInLightness )
|
|
|
|
{
|
|
|
|
const auto P = ( 1.0 - std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
|
|
|
|
const auto scaled_hue = aInHue / 60.0;
|
|
|
|
const auto Q = P * ( 1.0 - std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
|
|
|
|
|
|
|
|
r = g = b = aInLightness - P / 2.0;
|
|
|
|
|
|
|
|
if (scaled_hue < 1.0)
|
|
|
|
{
|
|
|
|
r += P;
|
|
|
|
g += Q;
|
|
|
|
}
|
|
|
|
else if (scaled_hue < 2.0)
|
|
|
|
{
|
|
|
|
r += Q;
|
|
|
|
g += P;
|
|
|
|
}
|
|
|
|
else if (scaled_hue < 3.0)
|
|
|
|
{
|
|
|
|
g += P;
|
|
|
|
b += Q;
|
|
|
|
}
|
|
|
|
else if (scaled_hue < 4.0)
|
|
|
|
{
|
|
|
|
g += Q;
|
|
|
|
b += P;
|
|
|
|
}
|
|
|
|
else if (scaled_hue < 5.0)
|
|
|
|
{
|
|
|
|
r += Q;
|
|
|
|
b += P;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r += P;
|
|
|
|
b += Q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
void COLOR4D::ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
|
|
|
|
bool aAlwaysDefineHue ) const
|
2013-09-12 09:35:42 +00:00
|
|
|
{
|
2013-09-12 15:42:28 +00:00
|
|
|
double min, max, delta;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
|
|
|
min = r < g ? r : g;
|
2013-09-12 15:42:28 +00:00
|
|
|
min = min < b ? min : b;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
|
|
|
max = r > g ? r : g;
|
2013-09-12 15:42:28 +00:00
|
|
|
max = max > b ? max : b;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
aOutValue = max; // value
|
2013-09-12 09:35:42 +00:00
|
|
|
delta = max - min;
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
if( max > 0.0 )
|
|
|
|
{
|
2017-07-16 15:10:24 +00:00
|
|
|
aOutSaturation = ( delta / max );
|
2013-09-12 15:42:28 +00:00
|
|
|
}
|
2017-07-16 15:10:24 +00:00
|
|
|
else // for black color (r = g = b = 0 ) saturation is set to 0.
|
2013-09-12 15:42:28 +00:00
|
|
|
{
|
2017-07-16 15:10:24 +00:00
|
|
|
aOutSaturation = 0.0;
|
2017-07-17 07:59:34 +00:00
|
|
|
aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
|
2013-09-12 09:35:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
/* Hue in degrees (0...360) is coded according to this table
|
|
|
|
* 0 or 360 : red
|
|
|
|
* 60 : yellow
|
|
|
|
* 120 : green
|
|
|
|
* 180 : cyan
|
|
|
|
* 240 : blue
|
|
|
|
* 300 : magenta
|
|
|
|
*/
|
|
|
|
if( delta != 0.0 )
|
|
|
|
{
|
|
|
|
if( r >= max )
|
|
|
|
aOutHue = ( g - b ) / delta; // between yellow & magenta
|
|
|
|
else if( g >= max )
|
|
|
|
aOutHue = 2.0 + ( b - r ) / delta; // between cyan & yellow
|
|
|
|
else
|
|
|
|
aOutHue = 4.0 + ( r - g ) / delta; // between magenta & cyan
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
aOutHue *= 60.0; // degrees
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
if( aOutHue < 0.0 )
|
|
|
|
aOutHue += 360.0;
|
|
|
|
}
|
2017-07-17 07:59:34 +00:00
|
|
|
else // delta = 0 means r = g = b. hue is set to 0.0
|
|
|
|
{
|
|
|
|
aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
|
|
|
|
}
|
2013-09-12 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
void COLOR4D::FromHSV( double aInH, double aInS, double aInV )
|
2013-09-12 09:35:42 +00:00
|
|
|
{
|
2017-07-16 15:10:24 +00:00
|
|
|
if( aInS <= 0.0 )
|
2013-09-12 15:42:28 +00:00
|
|
|
{
|
|
|
|
r = aInV;
|
|
|
|
g = aInV;
|
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
double hh = aInH;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
while( hh >= 360.0 )
|
|
|
|
hh -= 360.0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
/* Hue in degrees (0...360) is coded according to this table
|
|
|
|
* 0 or 360 : red
|
|
|
|
* 60 : yellow
|
|
|
|
* 120 : green
|
|
|
|
* 180 : cyan
|
|
|
|
* 240 : blue
|
|
|
|
* 300 : magenta
|
|
|
|
*/
|
2013-09-12 09:35:42 +00:00
|
|
|
hh /= 60.0;
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
int i = (int) hh;
|
|
|
|
double ff = hh - i;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2017-07-16 15:10:24 +00:00
|
|
|
double p = aInV * ( 1.0 - aInS );
|
|
|
|
double q = aInV * ( 1.0 - ( aInS * ff ) );
|
|
|
|
double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
switch( i )
|
2013-09-12 15:42:28 +00:00
|
|
|
{
|
2013-09-12 09:35:42 +00:00
|
|
|
case 0:
|
2013-09-12 15:42:28 +00:00
|
|
|
r = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
g = t;
|
|
|
|
b = p;
|
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 1:
|
|
|
|
r = q;
|
2013-09-12 15:42:28 +00:00
|
|
|
g = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
b = p;
|
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 2:
|
|
|
|
r = p;
|
2013-09-12 15:42:28 +00:00
|
|
|
g = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
b = t;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
r = p;
|
|
|
|
g = q;
|
2013-09-12 15:42:28 +00:00
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 4:
|
|
|
|
r = t;
|
|
|
|
g = p;
|
2013-09-12 15:42:28 +00:00
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 5:
|
|
|
|
default:
|
2013-09-12 15:42:28 +00:00
|
|
|
r = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
g = p;
|
|
|
|
b = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
COLOR4D& COLOR4D::Saturate( double aFactor )
|
|
|
|
{
|
2018-10-11 09:26:59 +00:00
|
|
|
// One can saturate a color only when r, v, b are not equal
|
|
|
|
if( r == g && r == b )
|
|
|
|
return *this;
|
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
double h, s, v;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2018-10-09 22:41:22 +00:00
|
|
|
ToHSV( h, s, v, true );
|
2013-09-12 15:42:28 +00:00
|
|
|
FromHSV( h, aFactor, 1.0 );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2017-02-20 17:48:27 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2021-02-26 14:33:50 +00:00
|
|
|
const COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0 );
|
|
|
|
const COLOR4D COLOR4D::WHITE( 1, 1, 1, 1 );
|
|
|
|
const COLOR4D COLOR4D::BLACK( 0, 0, 0, 1 );
|
|
|
|
const COLOR4D COLOR4D::CLEAR( 1, 0, 1, 0 );
|
2020-06-19 02:32:14 +00:00
|
|
|
|
|
|
|
|
2022-02-07 02:05:44 +00:00
|
|
|
double COLOR4D::Distance( const COLOR4D& other ) const
|
2021-09-16 10:14:43 +00:00
|
|
|
{
|
|
|
|
return ( r - other.r ) * ( r - other.r )
|
|
|
|
+ ( g - other.g ) * ( g - other.g )
|
|
|
|
+ ( b - other.b ) * ( b - other.b );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-19 02:32:14 +00:00
|
|
|
EDA_COLOR_T COLOR4D::FindNearestLegacyColor( int aR, int aG, int aB )
|
|
|
|
{
|
|
|
|
EDA_COLOR_T candidate = EDA_COLOR_T::BLACK;
|
|
|
|
|
|
|
|
/* Find the 'nearest' color in the palette. This is fun. There is
|
|
|
|
a gazilion of metrics for the color space and no one of the
|
|
|
|
useful one is in the RGB color space. Who cares, this is a CAD,
|
|
|
|
not a photosomething...
|
|
|
|
|
|
|
|
I hereby declare that the distance is the sum of the square of the
|
|
|
|
component difference. Think about the RGB color cube. Now get the
|
|
|
|
euclidean distance, but without the square root... for ordering
|
|
|
|
purposes it's the same, obviously. Also each component can't be
|
|
|
|
less of the target one, since I found this currently work better...
|
|
|
|
*/
|
|
|
|
int nearest_distance = 255 * 255 * 3 + 1; // Can't beat this
|
|
|
|
|
|
|
|
for( EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS;
|
|
|
|
trying = static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
|
|
|
|
{
|
2020-06-19 16:05:18 +00:00
|
|
|
const StructColors &c = colorRefs()[trying];
|
2021-07-26 17:28:37 +00:00
|
|
|
int distance = ( aR - c.m_Red ) * ( aR - c.m_Red ) +
|
|
|
|
( aG - c.m_Green ) * ( aG - c.m_Green ) +
|
|
|
|
( aB - c.m_Blue ) * ( aB - c.m_Blue );
|
2020-06-19 02:32:14 +00:00
|
|
|
|
|
|
|
if( distance < nearest_distance && c.m_Red >= aR &&
|
|
|
|
c.m_Green >= aG && c.m_Blue >= aB )
|
|
|
|
{
|
|
|
|
nearest_distance = distance;
|
|
|
|
candidate = trying;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return candidate;
|
|
|
|
}
|
2020-10-15 01:57:36 +00:00
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
|
2020-10-15 01:57:36 +00:00
|
|
|
COLOR4D& COLOR4D::FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha )
|
|
|
|
{
|
|
|
|
r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
|
|
|
|
g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
|
|
|
|
b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
|
|
|
|
a = std::max( 0.0, std::min( 1.0, aAlpha ) );
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|