27 #include <nlohmann/json.hpp> 33 using namespace KIGFX;
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;
129 bool 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;
147 wxString COLOR4D::ToWxString(
long flags )
const 149 wxColour c = ToColour();
150 return c.GetAsString( flags );
154 bool COLOR4D::SetFromHexString(
const wxString& aColorString )
156 wxString str = aColorString;
160 if( str.length() < 7 || str.GetChar( 0 ) !=
'#' )
165 if( wxSscanf( str.wx_str() + 1, wxT(
"%lx" ), &tmp ) != 1 )
168 if( str.length() >= 9 )
170 r = ( (tmp >> 24) & 0xFF ) / 255.0;
171 g = ( (tmp >> 16) & 0xFF ) / 255.0;
172 b = ( (tmp >> 8) & 0xFF ) / 255.0;
173 a = ( tmp & 0xFF ) / 255.0;
177 r = ( (tmp >> 16) & 0xFF ) / 255.0;
178 g = ( (tmp >> 8) & 0xFF ) / 255.0;
179 b = ( tmp & 0xFF ) / 255.0;
187 wxString COLOR4D::ToHexString()
const 197 wxColour COLOR4D::ToColour()
const 199 using CHAN_T = wxColourBase::ChannelType;
201 const wxColour colour(
202 static_cast<CHAN_T>(
r * 255 + 0.5 ), static_cast<CHAN_T>(
g * 255 + 0.5 ),
203 static_cast<CHAN_T>(
b * 255 + 0.5 ), static_cast<CHAN_T>(
a * 255 + 0.5 ) );
215 candidate.
r = ( (unsigned) ( 255.0 *
r ) | (unsigned) ( 255.0 * aColor.
r ) ) / 255.0,
216 candidate.
g = ( (
unsigned) ( 255.0 *
g ) | (
unsigned) ( 255.0 * aColor.
g ) ) / 255.0,
217 candidate.
b = ( (unsigned) ( 255.0 *
b ) | (unsigned) ( 255.0 * aColor.
b ) ) / 255.0,
220 candidate.
a = ( aColor.
a +
a ) / 2;
226 unsigned int COLOR4D::ToU32()
const 228 return ToColour().GetRGB();
232 void COLOR4D::FromU32(
unsigned int aPackedColor )
235 c.SetRGB( aPackedColor );
237 g = c.Green() / 255.0;
238 b = c.Blue() / 255.0;
239 a = c.Alpha() / 255.0;
247 return lhs.
a == rhs.
a && lhs.
r == rhs.
r && lhs.
g == rhs.
g && lhs.
b == rhs.
b;
253 return !( lhs == rhs );
261 else if( lhs.
g < rhs.
g )
263 else if( lhs.
b < rhs.
b )
265 else if( lhs.
a < rhs.
a )
274 return aStream << aColor.ToWxString( wxC2S_CSS_SYNTAX );
280 aJson =
nlohmann::json( aColor.ToWxString( wxC2S_CSS_SYNTAX ).ToStdString() );
286 aColor.SetFromWxString( aJson.get<std::string>() );
292 void COLOR4D::ToHSL(
double& aOutHue,
double& aOutSaturation,
double& aOutLightness )
const 294 auto min = std::min(
r, std::min(
g,
b ) );
295 auto max = std::max(
r, std::max(
g,
b ) );
296 auto diff = max - min;
298 aOutLightness = ( max + min ) / 2.0;
300 if( aOutLightness >= 1.0 )
301 aOutSaturation = 0.0;
303 aOutSaturation = diff / ( 1.0 - std::abs( 2.0 * aOutLightness - 1.0 ) );
310 hue = (
g -
b ) / diff;
312 hue = (
b -
r ) / diff + 2.0;
314 hue = (
r -
g ) / diff + 4.0;
316 aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
318 while( aOutHue < 0.0 )
325 const auto P = ( 1.0 - std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
326 const auto scaled_hue = aInHue / 60.0;
327 const auto Q = P * ( 1.0 - std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
329 r =
g =
b = aInLightness - P / 2.0;
331 if (scaled_hue < 1.0)
336 else if (scaled_hue < 2.0)
341 else if (scaled_hue < 3.0)
346 else if (scaled_hue < 4.0)
351 else if (scaled_hue < 5.0)
365 bool aAlwaysDefineHue )
const 367 double min, max,
delta;
370 min = min <
b ? min :
b;
373 max = max >
b ? max :
b;
380 aOutSaturation = (
delta / max );
384 aOutSaturation = 0.0;
385 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
402 aOutHue = 2.0 + (
b -
r ) /
delta;
404 aOutHue = 4.0 + (
r -
g ) /
delta;
413 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
446 double p = aInV * ( 1.0 - aInS );
447 double q = aInV * ( 1.0 - ( aInS * ff ) );
448 double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
495 if(
r ==
g &&
r ==
b )
500 ToHSV( h, s, v,
true );
515 return (
r - other.
r ) * (
r - other.
r )
516 + (
g - other.
g ) * (
g - other.
g )
517 + (
b - other.
b ) * (
b - other.
b );
536 int nearest_distance = 255 * 255 * 3 + 1;
539 trying = static_cast<EDA_COLOR_T>(
int( trying ) + 1 ) )
546 if( distance < nearest_distance && c.m_Red >= aR &&
560 r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
561 g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
562 b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
563 a = std::max( 0.0, std::min( 1.0, aAlpha ) );
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
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.
The Cairo implementation of the graphics abstraction layer.
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
static const COLOR4D BLACK
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
const bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
static const COLOR4D CLEAR
double Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
static const COLOR4D WHITE
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Converts current color (stored in RGB) to HSL format.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
void FromHSV(double aInH, double aInS, double aInV)
Changes currently used color to the one given by hue, saturation and value parameters.
EDA_COLOR_T
Legacy color enumeration.
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Some functions to handle hotkeys in KiCad.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
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.
const bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
void FromHSL(double aInHue, double aInSaturation, double aInLightness)
Change currently used color to the one given by hue, saturation and lightness parameters.
A color representation with 4 components: red, green, blue, alpha.