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 )
112#ifdef WX_COMPATIBILITY
115 if( !SetFromHexString( aColorStr ) )
116 SetFromWxString( aColorStr );
122 r = aColor.Red() / 255.0;
123 g = aColor.Green() / 255.0;
124 b = aColor.Blue() / 255.0;
125 a = aColor.Alpha() / 255.0;
129bool COLOR4D::SetFromWxString(
const wxString& aColorString )
133 if( c.Set( aColorString ) )
136 g = c.Green() / 255.0;
137 b = c.Blue() / 255.0;
138 a = c.Alpha() / 255.0;
147wxString COLOR4D::ToCSSString()
const
149 wxColour c = ToColour();
152 const int red = c.Red();
153 const int green = c.Green();
154 const int blue = c.Blue();
155 const int alpha = c.Alpha();
157 if ( alpha == wxALPHA_OPAQUE )
159 str.Printf( wxT(
"rgb(%d, %d, %d)" ), red, green, blue );
163 wxString alpha_str = wxString::FromCDouble( alpha / 255.0, 3 );
168 alpha_str.Replace( wxT(
"," ), wxT(
"." ) );
170 str.Printf( wxT(
"rgba(%d, %d, %d, %s)" ), red, green, blue, alpha_str );
177bool COLOR4D::SetFromHexString(
const wxString& aColorString )
179 wxString str = aColorString;
183 if( str.length() < 7 || str.GetChar( 0 ) !=
'#' )
188 if( wxSscanf( str.wx_str() + 1, wxT(
"%lx" ), &tmp ) != 1 )
191 if( str.length() >= 9 )
193 r = ( (tmp >> 24) & 0xFF ) / 255.0;
194 g = ( (tmp >> 16) & 0xFF ) / 255.0;
195 b = ( (tmp >> 8) & 0xFF ) / 255.0;
196 a = ( tmp & 0xFF ) / 255.0;
200 r = ( (tmp >> 16) & 0xFF ) / 255.0;
201 g = ( (tmp >> 8) & 0xFF ) / 255.0;
202 b = ( tmp & 0xFF ) / 255.0;
210wxString COLOR4D::ToHexString()
const
220wxColour COLOR4D::ToColour()
const
222 using CHAN_T = wxColourBase::ChannelType;
224 const wxColour colour(
225 static_cast<CHAN_T
>(
r * 255 + 0.5 ),
static_cast<CHAN_T
>(
g * 255 + 0.5 ),
226 static_cast<CHAN_T
>(
b * 255 + 0.5 ),
static_cast<CHAN_T
>(
a * 255 + 0.5 ) );
238 candidate.
r = ( (unsigned) ( 255.0 *
r ) | (unsigned) ( 255.0 * aColor.
r ) ) / 255.0,
239 candidate.
g = ( (
unsigned) ( 255.0 *
g ) | (
unsigned) ( 255.0 * aColor.
g ) ) / 255.0,
240 candidate.
b = ( (unsigned) ( 255.0 *
b ) | (unsigned) ( 255.0 * aColor.
b ) ) / 255.0,
243 candidate.
a = ( aColor.
a +
a ) / 2;
249unsigned int COLOR4D::ToU32()
const
251 return ToColour().GetRGB();
255void COLOR4D::FromU32(
unsigned int aPackedColor )
258 c.SetRGB( aPackedColor );
260 g = c.Green() / 255.0;
261 b = c.Blue() / 255.0;
262 a = c.Alpha() / 255.0;
270 return lhs.
a == rhs.
a && lhs.
r == rhs.
r && lhs.
g == rhs.
g && lhs.
b == rhs.
b;
276 return !( lhs == rhs );
284 else if( lhs.
g < rhs.
g )
286 else if( lhs.
b < rhs.
b )
288 else if( lhs.
a < rhs.
a )
297 return aStream << aColor.ToCSSString();
309 aColor.SetFromWxString( aJson.get<std::string>() );
315void COLOR4D::ToHSL(
double& aOutHue,
double& aOutSaturation,
double& aOutLightness )
const
317 auto min = std::min(
r, std::min(
g,
b ) );
318 auto max = std::max(
r, std::max(
g,
b ) );
319 auto diff = max - min;
321 aOutLightness = ( max + min ) / 2.0;
323 if( aOutLightness >= 1.0 )
324 aOutSaturation = 0.0;
326 aOutSaturation = diff / ( 1.0 -
std::abs( 2.0 * aOutLightness - 1.0 ) );
333 hue = (
g -
b ) / diff;
335 hue = (
b -
r ) / diff + 2.0;
337 hue = (
r -
g ) / diff + 4.0;
339 aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
341 while( aOutHue < 0.0 )
348 const auto P = ( 1.0 -
std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
349 const auto scaled_hue = aInHue / 60.0;
350 const auto Q =
P * ( 1.0 -
std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
352 r =
g =
b = aInLightness -
P / 2.0;
354 if (scaled_hue < 1.0)
359 else if (scaled_hue < 2.0)
364 else if (scaled_hue < 3.0)
369 else if (scaled_hue < 4.0)
374 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 ) ) );
518 if(
r ==
g &&
r ==
b )
523 ToHSV( h, s, v,
true );
533 if(
r ==
g &&
r ==
b )
553 return (
r - other.
r ) * (
r - other.
r )
554 + (
g - other.
g ) * (
g - other.
g )
555 + (
b - other.
b ) * (
b - other.
b );
574 int nearest_distance = 255 * 255 * 3 + 1;
577 trying =
static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
584 if( distance < nearest_distance && c.m_Red >= aR &&
598 r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
599 g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
600 b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
601 a = std::max( 0.0, std::min( 1.0, aAlpha ) );
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.
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.
static const COLOR4D WHITE
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)
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
const bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
const 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)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
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".