27#include <nlohmann/json.hpp>
35#define TS( string ) wxString( _HKI( string ) ).ToStdString()
85 if( aColor <= UNSPECIFIED_COLOR || aColor >=
NBCOLORS )
93 for( ; candidate <
NBCOLORS; ++candidate )
95 if(
colorRefs()[candidate].m_Numcolor == aColor )
121 r = aColor.Red() / 255.0;
122 g = aColor.Green() / 255.0;
123 b = aColor.Blue() / 255.0;
124 a = aColor.Alpha() / 255.0;
132 if( c.Set( aColorString ) )
135 g = c.Green() / 255.0;
136 b = c.Blue() / 255.0;
137 a = c.Alpha() / 255.0;
151 const int red = c.Red();
152 const int green = c.Green();
153 const int blue = c.Blue();
154 const int alpha = c.Alpha();
156 if ( alpha == wxALPHA_OPAQUE )
158 str.Printf( wxT(
"rgb(%d, %d, %d)" ),
red, green, blue );
162 wxString alpha_str = wxString::FromCDouble( alpha / 255.0, 3 );
167 alpha_str.Replace( wxT(
"," ), wxT(
"." ) );
169 str.Printf( wxT(
"rgba(%d, %d, %d, %s)" ),
red, green, blue, alpha_str );
178 wxString str = aColorString;
182 if( str.length() < 7 || !str.StartsWith(
'#' ) )
187 if( wxSscanf( str.wx_str() + 1, wxT(
"%lx" ), &tmp ) != 1 )
190 if( str.length() >= 9 )
192 r = ( (tmp >> 24) & 0xFF ) / 255.0;
193 g = ( (tmp >> 16) & 0xFF ) / 255.0;
194 b = ( (tmp >> 8) & 0xFF ) / 255.0;
195 a = ( tmp & 0xFF ) / 255.0;
199 r = ( (tmp >> 16) & 0xFF ) / 255.0;
200 g = ( (tmp >> 8) & 0xFF ) / 255.0;
201 b = ( tmp & 0xFF ) / 255.0;
211 return wxString::Format( wxT(
"#%02X%02X%02X%02X" ),
221 using CHAN_T = wxColourBase::ChannelType;
223 const wxColour colour(
224 static_cast<CHAN_T
>(
r * 255 + 0.5 ),
static_cast<CHAN_T
>(
g * 255 + 0.5 ),
225 static_cast<CHAN_T
>(
b * 255 + 0.5 ),
static_cast<CHAN_T
>(
a * 255 + 0.5 ) );
235 candidate.
r = ( (unsigned) ( 255.0 *
r ) | (unsigned) ( 255.0 * aColor.
r ) ) / 255.0,
236 candidate.
g = ( (
unsigned) ( 255.0 *
g ) | (
unsigned) ( 255.0 * aColor.
g ) ) / 255.0,
237 candidate.
b = ( (unsigned) ( 255.0 *
b ) | (unsigned) ( 255.0 * aColor.
b ) ) / 255.0,
240 candidate.
a = ( aColor.
a +
a ) / 2;
250 return lhs.
a == rhs.
a && lhs.
r == rhs.
r && lhs.
g == rhs.
g && lhs.
b == rhs.
b;
256 return !( lhs == rhs );
264 else if( lhs.
g < rhs.
g )
266 else if( lhs.
b < rhs.
b )
268 else if( lhs.
a < rhs.
a )
283 aJson = nlohmann::json( aColor.
ToCSSString().ToStdString() );
295void COLOR4D::ToHSL(
double& aOutHue,
double& aOutSaturation,
double& aOutLightness )
const
297 auto min = std::min(
r, std::min(
g,
b ) );
298 auto max = std::max(
r, std::max(
g,
b ) );
299 auto diff = max - min;
301 aOutLightness = ( max + min ) / 2.0;
303 if( aOutLightness >= 1.0 )
304 aOutSaturation = 0.0;
306 aOutSaturation = diff / ( 1.0 -
std::abs( 2.0 * aOutLightness - 1.0 ) );
313 hue = (
g -
b ) / diff;
315 hue = (
b -
r ) / diff + 2.0;
317 hue = (
r -
g ) / diff + 4.0;
319 aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
321 while( aOutHue < 0.0 )
328 const auto P = ( 1.0 -
std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
329 const auto scaled_hue = aInHue / 60.0;
330 const auto Q = P * ( 1.0 -
std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
332 r =
g =
b = aInLightness - P / 2.0;
334 if (scaled_hue < 1.0)
339 else if (scaled_hue < 2.0)
344 else if (scaled_hue < 3.0)
349 else if (scaled_hue < 4.0)
354 else if (scaled_hue < 5.0)
368 bool aAlwaysDefineHue )
const
370 double min, max,
delta;
373 min = min <
b ? min :
b;
376 max = max >
b ? max :
b;
383 aOutSaturation = (
delta / max );
387 aOutSaturation = 0.0;
388 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
405 aOutHue = 2.0 + (
b -
r ) /
delta;
407 aOutHue = 4.0 + (
r -
g ) /
delta;
416 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
449 double p = aInV * ( 1.0 - aInS );
450 double q = aInV * ( 1.0 - ( aInS * ff ) );
451 double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
498 if(
r ==
g &&
r ==
b )
503 ToHSV( h, s, v,
true );
513 if(
r ==
g &&
r ==
b )
533 return (
r - other.
r ) * (
r - other.
r )
534 + (
g - other.
g ) * (
g - other.
g )
535 + (
b - other.
b ) * (
b - other.
b );
554 int nearest_distance = 255 * 255 * 3 + 1;
556 for(
EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS;
557 trying =
static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
564 if( distance < nearest_distance && c.m_Red >= aR &&
578 r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
579 g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
580 b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
581 a = std::max( 0.0, std::min( 1.0, aAlpha ) );
590 return (
r < aRhs.
r ) ? -1 : 1;
593 return (
g < aRhs.
g ) ? -1 : 1;
596 return (
b < aRhs.
b ) ? -1 : 1;
599 return (
a < aRhs.
a ) ? -1 : 1;
A color representation with 4 components: red, green, blue, alpha.
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Converts current color (stored in RGB) to HSL format.
static const COLOR4D CLEAR
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
bool SetFromWxString(const wxString &aColorString)
Set color values by parsing a string using wxColour::Set().
bool SetFromHexString(const wxString &aColorString)
COLOR4D LegacyMix(const COLOR4D &aColor) const
Mix this COLOR4D with an input COLOR4D using the OR-mixing of legacy canvas.
static EDA_COLOR_T FindNearestLegacyColor(int aR, int aG, int aB)
Returns a legacy color ID that is closest to the given 8-bit RGB values.
void FromHSV(double aInH, double aInS, double aInV)
Changes currently used color to the one given by hue, saturation and value parameters.
wxString ToHexString() const
wxString ToCSSString() const
int Compare(const COLOR4D &aRhs) const
static const COLOR4D WHITE
wxColour ToColour() const
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
COLOR4D & Desaturate()
Removes color (in HSL model)
double Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
void FromHSL(double aInHue, double aInSaturation, double aInLightness)
Change currently used color to the one given by hue, saturation and lightness parameters.
static const COLOR4D BLACK
COLOR4D & FromCSSRGBA(int aRed, int aGreen, int aBlue, double aAlpha=1.0)
Initialize the color from a RGBA value with 0-255 red/green/blue and 0-1 alpha.
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
EDA_COLOR_T
Legacy color enumeration.
@ NBCOLORS
Number of colors.
Some functions to handle hotkeys in KiCad.
The Cairo implementation of the graphics abstraction layer.
bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".