27 #include <nlohmann/json.hpp> 31 using namespace KIGFX;
33 #define TS( string ) wxString( _HKI( string ) ).ToStdString() 78 if( aColor <= UNSPECIFIED_COLOR || aColor >=
NBCOLORS )
91 #ifdef WX_COMPATIBILITY 94 r = aColor.Red() / 255.0;
95 g = aColor.Green() / 255.0;
96 b = aColor.Blue() / 255.0;
97 a = aColor.Alpha() / 255.0;
101 bool COLOR4D::SetFromWxString(
const wxString& aColorString )
105 if( c.Set( aColorString ) )
108 g = c.Green() / 255.0;
109 b = c.Blue() / 255.0;
110 a = c.Alpha() / 255.0;
119 wxString COLOR4D::ToWxString(
long flags )
const 121 wxColour c = ToColour();
122 return c.GetAsString( flags );
126 wxColour COLOR4D::ToColour()
const 128 using CHAN_T = wxColourBase::ChannelType;
130 const wxColour colour(
131 static_cast<CHAN_T>(
r * 255 + 0.5 ),
132 static_cast<CHAN_T>(
g * 255 + 0.5 ),
133 static_cast<CHAN_T>(
b * 255 + 0.5 ),
134 static_cast<CHAN_T>(
a * 255 + 0.5 )
145 candidate.
r = ( (unsigned)( 255.0 *
r ) | (unsigned)( 255.0 * aColor.
r ) ) / 255.0,
146 candidate.
g = ( (
unsigned)( 255.0 *
g ) | (
unsigned)( 255.0 * aColor.
g ) ) / 255.0,
147 candidate.
b = ( (unsigned)( 255.0 *
b ) | (unsigned)( 255.0 * aColor.
b ) ) / 255.0,
151 candidate.
a = ( aColor.
a +
a ) / 2;
157 unsigned int COLOR4D::ToU32()
const 159 return ToColour().GetRGB();
163 void COLOR4D::FromU32(
unsigned int aPackedColor )
166 c.SetRGB( aPackedColor );
168 g = c.Green() / 255.0;
169 b = c.Blue() / 255.0;
170 a = c.Alpha() / 255.0;
179 return lhs.
a == rhs.
a && lhs.
r == rhs.
r && lhs.
g == rhs.
g && lhs.
b == rhs.
b;
185 return !( lhs == rhs );
192 else if( lhs.
g < rhs.
g )
194 else if( lhs.
b < rhs.
b )
196 else if( lhs.
a < rhs.
a )
204 return aStream << aColor.ToWxString( wxC2S_CSS_SYNTAX );
209 aJson =
nlohmann::json( aColor.ToWxString( wxC2S_CSS_SYNTAX ).ToStdString() );
215 aColor.SetFromWxString( aJson.get<std::string>() );
221 void COLOR4D::ToHSL(
double& aOutHue,
double& aOutSaturation,
double& aOutLightness )
const 223 auto min = std::min(
r, std::min(
g,
b ) );
224 auto max = std::max(
r, std::max(
g,
b ) );
225 auto diff = max - min;
227 aOutLightness = ( max + min ) / 2.0;
229 if( aOutLightness >= 1.0 )
230 aOutSaturation = 0.0;
232 aOutSaturation = diff / ( 1.0 - std::abs( 2.0 * aOutLightness - 1.0 ) );
239 hue = (
g -
b ) / diff;
241 hue = (
b -
r ) / diff + 2.0;
243 hue = (
r -
g ) / diff + 4.0;
245 aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
247 while( aOutHue < 0.0 )
254 const auto P = ( 1.0 - std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
255 const auto scaled_hue = aInHue / 60.0;
256 const auto Q = P * ( 1.0 - std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
258 r =
g =
b = aInLightness - P / 2.0;
260 if (scaled_hue < 1.0)
265 else if (scaled_hue < 2.0)
270 else if (scaled_hue < 3.0)
275 else if (scaled_hue < 4.0)
280 else if (scaled_hue < 5.0)
293 void COLOR4D::ToHSV(
double& aOutHue,
double& aOutSaturation,
double& aOutValue,
bool aAlwaysDefineHue )
const 295 double min, max, delta;
298 min = min <
b ? min :
b;
301 max = max >
b ? max :
b;
308 aOutSaturation = ( delta / max );
312 aOutSaturation = 0.0;
313 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
328 aOutHue = (
g -
b ) / delta;
330 aOutHue = 2.0 + (
b -
r ) / delta;
332 aOutHue = 4.0 + (
r -
g ) / delta;
341 aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
374 double p = aInV * ( 1.0 - aInS );
375 double q = aInV * ( 1.0 - ( aInS * ff ) );
376 double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
423 if(
r ==
g &&
r ==
b )
428 ToHSV( h, s, v,
true );
455 int nearest_distance = 255 * 255 * 3 + 1;
458 trying = static_cast<EDA_COLOR_T>(
int( trying ) + 1 ) )
465 if( distance < nearest_distance && c.m_Red >= aR &&
478 r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
479 g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
480 b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
481 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
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.
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.
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.