36#define TS( string ) wxString( _HKI( string ) ).ToStdString()
86 if( aColor <= UNSPECIFIED_COLOR || aColor >=
NBCOLORS )
94 for( ; candidate <
NBCOLORS; ++candidate )
96 if(
colorRefs()[candidate].m_Numcolor == aColor )
123 r = aColor.Red() / 255.0;
124 g = aColor.Green() / 255.0;
125 b = aColor.Blue() / 255.0;
126 a = aColor.Alpha() / 255.0;
135 if( c.Set( aColorString ) )
138 g = c.Green() / 255.0;
139 b = c.Blue() / 255.0;
140 a = c.Alpha() / 255.0;
155 const int red = c.Red();
156 const int green = c.Green();
157 const int blue = c.Blue();
158 const int alpha = c.Alpha();
160 if ( alpha == wxALPHA_OPAQUE )
162 str.Printf( wxT(
"rgb(%d, %d, %d)" ),
red,
green,
blue );
166 wxString alpha_str = wxString::FromCDouble( alpha / 255.0, 3 );
171 alpha_str.Replace( wxT(
"," ), wxT(
"." ) );
173 str.Printf( wxT(
"rgba(%d, %d, %d, %s)" ),
red,
green,
blue, alpha_str );
182 wxString str = aColorString;
186 if( str.length() < 7 || !str.StartsWith(
'#' ) )
191 if( wxSscanf( str.wx_str() + 1, wxT(
"%lx" ), &tmp ) != 1 )
194 if( str.length() >= 9 )
196 r = ( (tmp >> 24) & 0xFF ) / 255.0;
197 g = ( (tmp >> 16) & 0xFF ) / 255.0;
198 b = ( (tmp >> 8) & 0xFF ) / 255.0;
199 a = ( tmp & 0xFF ) / 255.0;
203 r = ( (tmp >> 16) & 0xFF ) / 255.0;
204 g = ( (tmp >> 8) & 0xFF ) / 255.0;
205 b = ( tmp & 0xFF ) / 255.0;
217 return wxString::Format( wxT(
"#%02X%02X%02X%02X" ),
227 using CHAN_T = wxColourBase::ChannelType;
229 const wxColour colour(
230 static_cast<CHAN_T
>(
r * 255 + 0.5 ),
static_cast<CHAN_T
>(
g * 255 + 0.5 ),
231 static_cast<CHAN_T
>(
b * 255 + 0.5 ),
static_cast<CHAN_T
>(
a * 255 + 0.5 ) );
241 candidate.
r = ( (unsigned) ( 255.0 *
r ) | (unsigned) ( 255.0 * aColor.
r ) ) / 255.0,
242 candidate.
g = ( (
unsigned) ( 255.0 *
g ) | (
unsigned) ( 255.0 * aColor.
g ) ) / 255.0,
243 candidate.
b = ( (unsigned) ( 255.0 *
b ) | (unsigned) ( 255.0 * aColor.
b ) ) / 255.0,
246 candidate.
a = ( aColor.
a +
a ) / 2;
257 return lhs.
m_text.value_or( wxEmptyString ) == rhs.
m_text.value_or( wxEmptyString );
259 return lhs.
a == rhs.
a && lhs.
r == rhs.
r && lhs.
g == rhs.
g && lhs.
b == rhs.
b;
265 return !( lhs == rhs );
272 return lhs.
m_text.value_or( wxEmptyString ) < rhs.
m_text.value_or( wxEmptyString );
276 else if( lhs.
g < rhs.
g )
278 else if( lhs.
b < rhs.
b )
280 else if( lhs.
a < rhs.
a )
289 if( aColor.
m_text.has_value() )
290 return aStream << aColor.
m_text.value();
298 if( aColor.
m_text.has_value() )
299 aJson = nlohmann::json( aColor.
m_text.value().ToStdString() );
301 aJson = nlohmann::json( aColor.
ToCSSString().ToStdString() );
313void COLOR4D::ToHSL(
double& aOutHue,
double& aOutSaturation,
double& aOutLightness )
const
315 auto min = std::min(
r, std::min(
g,
b ) );
316 auto max = std::max(
r, std::max(
g,
b ) );
317 auto diff = max - min;
319 aOutLightness = ( max + min ) / 2.0;
321 if( aOutLightness >= 1.0 )
322 aOutSaturation = 0.0;
324 aOutSaturation = diff / ( 1.0 -
std::abs( 2.0 * aOutLightness - 1.0 ) );
331 hue = (
g -
b ) / diff;
333 hue = (
b -
r ) / diff + 2.0;
335 hue = (
r -
g ) / diff + 4.0;
337 aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
339 while( aOutHue < 0.0 )
346 const auto P = ( 1.0 -
std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
347 const auto scaled_hue = aInHue / 60.0;
348 const auto Q = P * ( 1.0 -
std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
350 r =
g =
b = aInLightness - P / 2.0;
352 if (scaled_hue < 1.0)
357 else if (scaled_hue < 2.0)
362 else if (scaled_hue < 3.0)
367 else if (scaled_hue < 4.0)
372 else if (scaled_hue < 5.0)
388 bool aAlwaysDefineHue )
const
390 double min, max,
delta;
393 min = min <
b ? min :
b;
396 max = max >
b ? max :
b;
403 aOutSaturation = (
delta / max );
407 aOutSaturation = 0.0;
408 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
425 aOutHue = 2.0 + (
b -
r ) /
delta;
427 aOutHue = 4.0 + (
r -
g ) /
delta;
436 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
469 double p = aInV * ( 1.0 - aInS );
470 double q = aInV * ( 1.0 - ( aInS * ff ) );
471 double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
520 if(
r ==
g &&
r ==
b )
525 ToHSV( h, s, v,
true );
535 if(
r ==
g &&
r ==
b )
555 return (
r - other.
r ) * (
r - other.
r )
556 + (
g - other.
g ) * (
g - other.
g )
557 + (
b - other.
b ) * (
b - other.
b );
576 int nearest_distance = 255 * 255 * 3 + 1;
579 trying =
static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
600 r = std::clamp( aRed, 0, 255 ) / 255.0;
601 g = std::clamp( aGreen, 0, 255 ) / 255.0;
602 b = std::clamp( aBlue, 0, 255 ) / 255.0;
603 a = std::clamp( aAlpha, 0.0, 1.0 );
613 return (
m_text.value_or( wxEmptyString ) < aRhs.
m_text.value_or( wxEmptyString ) ) ? -1 : 1;
616 return (
r < aRhs.
r ) ? -1 : 1;
619 return (
g < aRhs.
g ) ? -1 : 1;
622 return (
b < aRhs.
b ) ? -1 : 1;
625 return (
a < aRhs.
a ) ? -1 : 1;
634 double cr = (
r <= 0.04045 ) ? (
r / 12.92 ) : std::pow( (
r + 0.055 ) / 1.055, 2.4 );
635 double cg = (
g <= 0.04045 ) ? (
g / 12.92 ) : std::pow( (
g + 0.055 ) / 1.055, 2.4 );
636 double cb = (
b <= 0.04045 ) ? (
b / 12.92 ) : std::pow( (
b + 0.055 ) / 1.055, 2.4 );
638 return 0.2126 * cr + 0.7152 * cg + 0.0722 * cb;
649 return ( aRL + 0.05 ) / ( bRL + 0.05 );
651 return ( bRL + 0.05 ) / ( aRL + 0.05 );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
A color representation with 4 components: red, green, blue, alpha.
static const COLOR4D CLEAR
std::optional< wxString > m_text
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
static double ContrastRatio(const COLOR4D &aLeft, const COLOR4D &aRight)
Compute the contrast ration between two colors using the formula from WCAG21.
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutLightness) const
Converts current color (stored in RGB) to HSL 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
double RelativeLuminance() const
Compute the relative luminance of a color using the formula from WCAG21.
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.
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
void to_json(nlohmann::json &aJson, const 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.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)