KiCad PCB EDA Suite
color4d.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5 * Copyright (C) 2017-2020 Kicad Developers, see AUTHORS.txt for contributors.
6 *
7 * Color class
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef COLOR4D_H_
28#define COLOR4D_H_
29
30#include <wx/debug.h>
31#include <wx/colour.h>
32#include <wx/string.h>
33#include <nlohmann/json_fwd.hpp>
34
35
40{
42 BLACK = 0,
78 HIGHLIGHT_FLAG = ( 1<<19 ),
79 MASKCOLOR = 31
80};
81
83{
84 unsigned char m_Blue;
85 unsigned char m_Green;
86 unsigned char m_Red;
88 std::string m_ColorName;
90};
91
93const StructColors* colorRefs();
94
95
96namespace KIGFX
97{
102{
103public:
104 // Constructor (creates the Color 0,0,0,0)
106 r( 0 ),
107 g( 0 ),
108 b( 0 ),
109 a( 1.0 )
110 {
111 }
112
119 COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
120 r( aRed ),
121 g( aGreen ),
122 b( aBlue ),
123 a( aAlpha )
124 {
125 wxASSERT( r >= 0.0 && r <= 1.0 );
126 wxASSERT( g >= 0.0 && g <= 1.0 );
127 wxASSERT( b >= 0.0 && b <= 1.0 );
128 wxASSERT( a >= 0.0 && a <= 1.0 );
129 }
130
135 COLOR4D( EDA_COLOR_T aColor );
136
144 COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
145
150 COLOR4D( const wxString& aColorStr );
151
155 COLOR4D( const wxColour& aColor );
156
163 bool SetFromWxString( const wxString& aColorString );
164
165 wxString ToCSSString() const;
166
167 bool SetFromHexString( const wxString& aColorString );
168 wxString ToHexString() const;
169
170 wxColour ToColour() const;
171
180 COLOR4D LegacyMix( const COLOR4D& aColor ) const;
181
190 void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
191
199 void FromHSL( double aInHue, double aInSaturation, double aInLightness );
200
207 COLOR4D& Brighten( double aFactor )
208 {
209 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
210
211 r = r * ( 1.0 - aFactor ) + aFactor;
212 g = g * ( 1.0 - aFactor ) + aFactor;
213 b = b * ( 1.0 - aFactor ) + aFactor;
214
215 return *this;
216 }
217
224 COLOR4D& Darken( double aFactor )
225 {
226 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
227
228 r = r * ( 1.0 - aFactor );
229 g = g * ( 1.0 - aFactor );
230 b = b * ( 1.0 - aFactor );
231
232 return *this;
233 }
234
241 {
242 r = ( 1.0 - r );
243 g = ( 1.0 - g );
244 b = ( 1.0 - b );
245
246 return *this;
247 }
248
252 COLOR4D& Saturate( double aFactor );
253
259
266 COLOR4D Brightened( double aFactor ) const
267 {
268 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
269
270 return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
271 b * ( 1.0 - aFactor ) + aFactor, a );
272 }
273
280 COLOR4D Darkened( double aFactor ) const
281 {
282 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
283
284 return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
285 }
286
293 COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
294 {
295 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
296
297 return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
298 aColor.g * ( 1.0 - aFactor ) + g * aFactor,
299 aColor.b * ( 1.0 - aFactor ) + b * aFactor,
300 a );
301 }
302
309 COLOR4D WithAlpha( double aAlpha ) const
310 {
311 wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
312
313 return COLOR4D( r, g, b, aAlpha );
314 }
315
322 {
323 return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
324 }
325
331 double GetBrightness() const
332 {
333 // Weighted W3C formula
334 return r * 0.299 + g * 0.587 + b * 0.117;
335 }
336
349 void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
350 bool aAlwaysDefineHue = false ) const;
351
359 void FromHSV( double aInH, double aInS, double aInV );
360
364 double Distance( const COLOR4D& other ) const;
365
369 static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
370
371 // Color components: red, green, blue, alpha
372 double r;
373 double g;
374 double b;
375 double a;
376
378 static const COLOR4D UNSPECIFIED;
379
380 // Declare a few color shortcuts that are used for comparisons frequently
381 static const COLOR4D WHITE;
382 static const COLOR4D BLACK;
383 static const COLOR4D CLEAR;
384};
385
387const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
388
390const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
391
392const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
393
395std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
396
397// to allow json( COLOR4D )
398void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
399
400// To allow json::get<COLOR4D>()
401void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
402
403} // namespace KIGFX
404
405#endif /* COLOR4D_H_ */
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:102
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Converts current color (stored in RGB) to HSL format.
Definition: color4d.cpp:295
double r
Red component.
Definition: color4d.h:372
double g
Green component.
Definition: color4d.h:373
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:309
static const COLOR4D CLEAR
Definition: color4d.h:383
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition: color4d.h:240
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
Definition: color4d.cpp:367
bool SetFromWxString(const wxString &aColorString)
Set color values by parsing a string using wxColour::Set().
Definition: color4d.cpp:128
bool SetFromHexString(const wxString &aColorString)
Definition: color4d.cpp:176
COLOR4D LegacyMix(const COLOR4D &aColor) const
Mix this COLOR4D with an input COLOR4D using the OR-mixing of legacy canvas.
Definition: color4d.cpp:230
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.
Definition: color4d.cpp:539
void FromHSV(double aInH, double aInS, double aInV)
Changes currently used color to the one given by hue, saturation and value parameters.
Definition: color4d.cpp:421
COLOR4D & Darken(double aFactor)
Makes the color darker by a given factor.
Definition: color4d.h:224
wxString ToHexString() const
Definition: color4d.cpp:209
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition: color4d.h:280
COLOR4D Inverted() const
Returns an inverted color, alpha remains the same.
Definition: color4d.h:321
wxString ToCSSString() const
Definition: color4d.cpp:146
COLOR4D & Brighten(double aFactor)
Makes the color brighter by a given factor.
Definition: color4d.h:207
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:331
static const COLOR4D WHITE
Definition: color4d.h:381
double a
Alpha component.
Definition: color4d.h:375
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:266
wxColour ToColour() const
Definition: color4d.cpp:219
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:378
COLOR4D & Desaturate()
Removes color (in HSL model)
Definition: color4d.cpp:510
COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Definition: color4d.h:119
double Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
Definition: color4d.cpp:531
void FromHSL(double aInHue, double aInSaturation, double aInLightness)
Change currently used color to the one given by hue, saturation and lightness parameters.
Definition: color4d.cpp:326
static const COLOR4D BLACK
Definition: color4d.h:382
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition: color4d.h:293
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.
Definition: color4d.cpp:576
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
Definition: color4d.cpp:495
double b
Blue component.
Definition: color4d.h:374
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition: color4d.cpp:39
EDA_COLOR_T
Legacy color enumeration.
Definition: color4d.h:40
@ HIGHLIGHT_FLAG
Definition: color4d.h:78
@ LIGHTGREEN
Definition: color4d.h:61
@ PUREORANGE
Definition: color4d.h:76
@ BROWN
Definition: color4d.h:59
@ PURECYAN
Definition: color4d.h:68
@ LIGHTBLUE
Definition: color4d.h:60
@ WHITE
Definition: color4d.h:46
@ LIGHTERORANGE
Definition: color4d.h:72
@ LIGHTORANGE
Definition: color4d.h:75
@ BLUE
Definition: color4d.h:54
@ LIGHTGRAY
Definition: color4d.h:45
@ DARKGRAY
Definition: color4d.h:44
@ MAGENTA
Definition: color4d.h:58
@ DARKORANGE
Definition: color4d.h:73
@ DARKMAGENTA
Definition: color4d.h:52
@ LIGHTYELLOW
Definition: color4d.h:47
@ DARKCYAN
Definition: color4d.h:50
@ NBCOLORS
Number of colors.
Definition: color4d.h:77
@ PURERED
Definition: color4d.h:69
@ UNSPECIFIED_COLOR
Definition: color4d.h:41
@ GREEN
Definition: color4d.h:55
@ CYAN
Definition: color4d.h:56
@ DARKRED
Definition: color4d.h:51
@ DARKBLUE
Definition: color4d.h:48
@ LIGHTCYAN
Definition: color4d.h:62
@ DARKDARKGRAY
Definition: color4d.h:43
@ ORANGE
Definition: color4d.h:74
@ PUREGREEN
Definition: color4d.h:67
@ LIGHTMAGENTA
Definition: color4d.h:64
@ PUREYELLOW
Definition: color4d.h:71
@ YELLOW
Definition: color4d.h:65
@ PUREBLUE
Definition: color4d.h:66
@ LIGHTRED
Definition: color4d.h:63
@ MASKCOLOR
mask for color index into colorRefs()[]
Definition: color4d.h:79
@ BLACK
Definition: color4d.h:42
@ DARKGREEN
Definition: color4d.h:49
@ PUREMAGENTA
Definition: color4d.h:70
@ RED
Definition: color4d.h:57
@ DARKBROWN
Definition: color4d.h:53
nlohmann::json json
Definition: gerbview.cpp:44
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:246
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition: color4d.cpp:287
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
Definition: color4d.cpp:275
const bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
Definition: color4d.cpp:260
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition: color4d.cpp:248
const bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition: color4d.cpp:254
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition: color4d.cpp:281
unsigned char m_Blue
Definition: color4d.h:84
EDA_COLOR_T m_LightColor
Definition: color4d.h:89
std::string m_ColorName
Definition: color4d.h:88
EDA_COLOR_T m_Numcolor
Definition: color4d.h:87
unsigned char m_Green
Definition: color4d.h:85
unsigned char m_Red
Definition: color4d.h:86