KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <kicommon.h>
31#include <wx/debug.h>
32#include <wx/colour.h>
33#include <wx/string.h>
34#include <hash.h>
35#include <nlohmann/json_fwd.hpp>
36
37
42{
44 BLACK = 0,
80 HIGHLIGHT_FLAG = ( 1<<19 ),
81 MASKCOLOR = 31
82};
83
85{
86 unsigned char m_Blue;
87 unsigned char m_Green;
88 unsigned char m_Red;
90 std::string m_ColorName;
92};
93
96
97
98namespace KIGFX
99{
104{
105public:
106 // Constructor (creates the Color 0,0,0,0)
108 r( 0 ),
109 g( 0 ),
110 b( 0 ),
111 a( 1.0 )
112 {
113 }
114
121 COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
122 r( aRed ),
123 g( aGreen ),
124 b( aBlue ),
125 a( aAlpha )
126 {
127 wxASSERT( r >= 0.0 && r <= 1.0 );
128 wxASSERT( g >= 0.0 && g <= 1.0 );
129 wxASSERT( b >= 0.0 && b <= 1.0 );
130 wxASSERT( a >= 0.0 && a <= 1.0 );
131 }
132
137 COLOR4D( EDA_COLOR_T aColor );
138
146 COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
147
152 COLOR4D( const wxString& aColorStr );
153
157 COLOR4D( const wxColour& aColor );
158
165 bool SetFromWxString( const wxString& aColorString );
166
167 wxString ToCSSString() const;
168
169 bool SetFromHexString( const wxString& aColorString );
170 wxString ToHexString() const;
171
172 wxColour ToColour() const;
173
182 COLOR4D LegacyMix( const COLOR4D& aColor ) const;
183
192 void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutLightness ) const;
193
201 void FromHSL( double aInHue, double aInSaturation, double aInLightness );
202
209 COLOR4D& Brighten( double aFactor )
210 {
211 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
212
213 r = r * ( 1.0 - aFactor ) + aFactor;
214 g = g * ( 1.0 - aFactor ) + aFactor;
215 b = b * ( 1.0 - aFactor ) + aFactor;
216
217 return *this;
218 }
219
226 COLOR4D& Darken( double aFactor )
227 {
228 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
229
230 r = r * ( 1.0 - aFactor );
231 g = g * ( 1.0 - aFactor );
232 b = b * ( 1.0 - aFactor );
233
234 return *this;
235 }
236
243 {
244 r = ( 1.0 - r );
245 g = ( 1.0 - g );
246 b = ( 1.0 - b );
247
248 return *this;
249 }
250
254 COLOR4D& Saturate( double aFactor );
255
260 COLOR4D& Desaturate();
261
268 COLOR4D Brightened( double aFactor ) const
269 {
270 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
271
272 return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
273 b * ( 1.0 - aFactor ) + aFactor, a );
274 }
275
282 COLOR4D Darkened( double aFactor ) const
283 {
284 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
285
286 return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
287 }
288
295 COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
296 {
297 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
298
299 return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
300 aColor.g * ( 1.0 - aFactor ) + g * aFactor,
301 aColor.b * ( 1.0 - aFactor ) + b * aFactor,
302 a );
303 }
304
311 COLOR4D WithAlpha( double aAlpha ) const
312 {
313 wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
314
315 return COLOR4D( r, g, b, aAlpha );
316 }
317
324 {
325 return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
326 }
327
333 double GetBrightness() const
334 {
335 // Weighted W3C formula
336 return r * 0.299 + g * 0.587 + b * 0.117;
337 }
338
351 void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
352 bool aAlwaysDefineHue = false ) const;
353
361 void FromHSV( double aInH, double aInS, double aInV );
362
366 double Distance( const COLOR4D& other ) const;
367
368 int Compare( const COLOR4D& aRhs ) const;
369
375 double RelativeLuminance() const;
376
384 static double ContrastRatio( const COLOR4D& aLeft, const COLOR4D& aRight );
385
389 static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
390
391 // Color components: red, green, blue, alpha
392 double r;
393 double g;
394 double b;
395 double a;
396
398 static const COLOR4D UNSPECIFIED;
399
400 // Declare a few color shortcuts that are used for comparisons frequently
401 static const COLOR4D WHITE;
402 static const COLOR4D BLACK;
403 static const COLOR4D CLEAR;
404};
405
407KICOMMON_API bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
408
410KICOMMON_API bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
411
412KICOMMON_API bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
413
415KICOMMON_API std::ostream& operator<<( std::ostream& aStream, COLOR4D const& aColor );
416
417// to allow json( COLOR4D )
418KICOMMON_API void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
419
420// To allow json::get<COLOR4D>()
421KICOMMON_API void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
422
423} // namespace KIGFX
424
425template<>
426struct KICOMMON_API std::hash<KIGFX::COLOR4D>
427{
428 std::size_t operator()( const KIGFX::COLOR4D& aColor ) const
429 {
430 return hash_val( aColor.r, aColor.b, aColor.g, aColor.a );
431 }
432};
433
434#endif /* COLOR4D_H_ */
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
static const COLOR4D CLEAR
Definition: color4d.h:403
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition: color4d.h:242
COLOR4D & Darken(double aFactor)
Makes the color darker by a given factor.
Definition: color4d.h:226
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition: color4d.h:282
COLOR4D Inverted() const
Returns an inverted color, alpha remains the same.
Definition: color4d.h:323
COLOR4D & Brighten(double aFactor)
Makes the color brighter by a given factor.
Definition: color4d.h:209
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:333
static const COLOR4D WHITE
Definition: color4d.h:401
double a
Alpha component.
Definition: color4d.h:395
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:268
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:398
COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Definition: color4d.h:121
static const COLOR4D BLACK
Definition: color4d.h:402
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition: color4d.h:295
double b
Blue component.
Definition: color4d.h:394
KICOMMON_API const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition: color4d.cpp:40
EDA_COLOR_T
Legacy color enumeration.
Definition: color4d.h:42
@ HIGHLIGHT_FLAG
Definition: color4d.h:80
@ LIGHTGREEN
Definition: color4d.h:63
@ PUREORANGE
Definition: color4d.h:78
@ BROWN
Definition: color4d.h:61
@ PURECYAN
Definition: color4d.h:70
@ LIGHTBLUE
Definition: color4d.h:62
@ WHITE
Definition: color4d.h:48
@ LIGHTERORANGE
Definition: color4d.h:74
@ LIGHTORANGE
Definition: color4d.h:77
@ BLUE
Definition: color4d.h:56
@ LIGHTGRAY
Definition: color4d.h:47
@ DARKGRAY
Definition: color4d.h:46
@ MAGENTA
Definition: color4d.h:60
@ DARKORANGE
Definition: color4d.h:75
@ DARKMAGENTA
Definition: color4d.h:54
@ LIGHTYELLOW
Definition: color4d.h:49
@ DARKCYAN
Definition: color4d.h:52
@ NBCOLORS
Number of colors.
Definition: color4d.h:79
@ PURERED
Definition: color4d.h:71
@ UNSPECIFIED_COLOR
Definition: color4d.h:43
@ GREEN
Definition: color4d.h:57
@ CYAN
Definition: color4d.h:58
@ DARKRED
Definition: color4d.h:53
@ DARKBLUE
Definition: color4d.h:50
@ LIGHTCYAN
Definition: color4d.h:64
@ DARKDARKGRAY
Definition: color4d.h:45
@ ORANGE
Definition: color4d.h:76
@ PUREGREEN
Definition: color4d.h:69
@ LIGHTMAGENTA
Definition: color4d.h:66
@ PUREYELLOW
Definition: color4d.h:73
@ YELLOW
Definition: color4d.h:67
@ PUREBLUE
Definition: color4d.h:68
@ LIGHTRED
Definition: color4d.h:65
@ MASKCOLOR
mask for color index into colorRefs()[]
Definition: color4d.h:81
@ BLACK
Definition: color4d.h:44
@ DARKGREEN
Definition: color4d.h:51
@ PUREMAGENTA
Definition: color4d.h:72
@ RED
Definition: color4d.h:59
@ DARKBROWN
Definition: color4d.h:55
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1195
static constexpr std::size_t hash_val(const Types &... args)
Definition: hash.h:51
#define KICOMMON_API
Definition: kicommon.h:28
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition: color4d.cpp:288
bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition: color4d.cpp:249
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition: color4d.cpp:282
bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
Definition: color4d.cpp:261
bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition: color4d.cpp:255
STL namespace.
unsigned char m_Blue
Definition: color4d.h:86
EDA_COLOR_T m_LightColor
Definition: color4d.h:91
std::string m_ColorName
Definition: color4d.h:90
EDA_COLOR_T m_Numcolor
Definition: color4d.h:89
unsigned char m_Green
Definition: color4d.h:87
unsigned char m_Red
Definition: color4d.h:88
std::size_t operator()(const KIGFX::COLOR4D &aColor) const
Definition: color4d.h:428