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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#pragma once
24
25#include <memory>
26#include <kicommon.h>
27#include <wx/debug.h>
28#include <wx/colour.h>
29#include <wx/string.h>
30#include <hash.h>
31#include <nlohmann/json_fwd.hpp>
32
33
79
80
82{
83 unsigned char m_Blue;
84 unsigned char m_Green;
85 unsigned char m_Red;
87 std::string m_ColorName;
89};
90
93
94
95namespace KIGFX
96{
101{
102public:
103 // Constructor (creates the Color 0,0,0,0)
105 r( 0 ),
106 g( 0 ),
107 b( 0 ),
108 a( 1.0 )
109 {
110 }
111
118 COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
119 r( aRed ),
120 g( aGreen ),
121 b( aBlue ),
122 a( aAlpha )
123 {
124 wxASSERT( r >= 0.0 && r <= 1.0 );
125 wxASSERT( g >= 0.0 && g <= 1.0 );
126 wxASSERT( b >= 0.0 && b <= 1.0 );
127 wxASSERT( a >= 0.0 && a <= 1.0 );
128 }
129
134 COLOR4D( EDA_COLOR_T aColor );
135
143 COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
144
149 COLOR4D( const wxString& aColorStr );
150
154 COLOR4D( const wxColour& aColor );
155
162 bool SetFromWxString( const wxString& aColorString );
163
164 wxString ToCSSString() const;
165
166 bool SetFromHexString( const wxString& aColorString );
167 wxString ToHexString() const;
168
169 wxColour ToColour() const;
170
179 COLOR4D LegacyMix( const COLOR4D& aColor ) const;
180
189 void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutLightness ) const;
190
198 void FromHSL( double aInHue, double aInSaturation, double aInLightness );
199
206 COLOR4D& Brighten( double aFactor )
207 {
208 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
209
210 r = r * ( 1.0 - aFactor ) + aFactor;
211 g = g * ( 1.0 - aFactor ) + aFactor;
212 b = b * ( 1.0 - aFactor ) + aFactor;
213
214 return *this;
215 }
216
223 COLOR4D& Darken( double aFactor )
224 {
225 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
226
227 r = r * ( 1.0 - aFactor );
228 g = g * ( 1.0 - aFactor );
229 b = b * ( 1.0 - aFactor );
230
231 return *this;
232 }
233
240 {
241 r = ( 1.0 - r );
242 g = ( 1.0 - g );
243 b = ( 1.0 - b );
244
245 return *this;
246 }
247
251 COLOR4D& Saturate( double aFactor );
252
257 COLOR4D& Desaturate();
258
265 COLOR4D Brightened( double aFactor ) const
266 {
267 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
268
269 return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
270 b * ( 1.0 - aFactor ) + aFactor, a );
271 }
272
279 COLOR4D Darkened( double aFactor ) const
280 {
281 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
282
283 return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
284 }
285
292 COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
293 {
294 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
295
296 return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
297 aColor.g * ( 1.0 - aFactor ) + g * aFactor,
298 aColor.b * ( 1.0 - aFactor ) + b * aFactor,
299 a );
300 }
301
308 COLOR4D WithAlpha( double aAlpha ) const
309 {
310 wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
311
312 return COLOR4D( r, g, b, aAlpha );
313 }
314
321 {
322 return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
323 }
324
330 double GetBrightness() const
331 {
332 // Weighted W3C formula
333 return r * 0.299 + g * 0.587 + b * 0.117;
334 }
335
348 void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
349 bool aAlwaysDefineHue = false ) const;
350
358 void FromHSV( double aInH, double aInS, double aInV );
359
363 double Distance( const COLOR4D& other ) const;
364
365 int Compare( const COLOR4D& aRhs ) const;
366
372 double RelativeLuminance() const;
373
381 static double ContrastRatio( const COLOR4D& aLeft, const COLOR4D& aRight );
382
386 static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
387
388 // Color components: red, green, blue, alpha
389 double r;
390 double g;
391 double b;
392 double a;
393
394 // Optional text for evaluated colors
395 std::shared_ptr<wxString> m_text;
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};
COLOR4D()
Definition color4d.h:104
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:389
double g
Green component.
Definition color4d.h:390
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
static const COLOR4D CLEAR
Definition color4d.h:403
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition color4d.h:239
COLOR4D & Darken(double aFactor)
Makes the color darker by a given factor.
Definition color4d.h:223
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition color4d.h:279
COLOR4D Inverted() const
Returns an inverted color, alpha remains the same.
Definition color4d.h:320
COLOR4D & Brighten(double aFactor)
Makes the color brighter by a given factor.
Definition color4d.h:206
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition color4d.h:330
static const COLOR4D WHITE
Definition color4d.h:401
std::shared_ptr< wxString > m_text
Definition color4d.h:395
double a
Alpha component.
Definition color4d.h:392
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition color4d.h:265
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:118
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:292
double b
Blue component.
Definition color4d.h:391
KICOMMON_API const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition color4d.cpp:36
EDA_COLOR_T
Legacy color enumeration.
Definition color4d.h:38
@ HIGHLIGHT_FLAG
Definition color4d.h:76
@ LIGHTGREEN
Definition color4d.h:59
@ PUREORANGE
Definition color4d.h:74
@ BROWN
Definition color4d.h:57
@ PURECYAN
Definition color4d.h:66
@ LIGHTBLUE
Definition color4d.h:58
@ WHITE
Definition color4d.h:44
@ LIGHTERORANGE
Definition color4d.h:70
@ LIGHTORANGE
Definition color4d.h:73
@ BLUE
Definition color4d.h:52
@ LIGHTGRAY
Definition color4d.h:43
@ DARKGRAY
Definition color4d.h:42
@ MAGENTA
Definition color4d.h:56
@ DARKORANGE
Definition color4d.h:71
@ DARKMAGENTA
Definition color4d.h:50
@ LIGHTYELLOW
Definition color4d.h:45
@ DARKCYAN
Definition color4d.h:48
@ NBCOLORS
Number of colors.
Definition color4d.h:75
@ PURERED
Definition color4d.h:67
@ UNSPECIFIED_COLOR
Definition color4d.h:39
@ GREEN
Definition color4d.h:53
@ CYAN
Definition color4d.h:54
@ DARKRED
Definition color4d.h:49
@ DARKBLUE
Definition color4d.h:46
@ LIGHTCYAN
Definition color4d.h:60
@ DARKDARKGRAY
Definition color4d.h:41
@ ORANGE
Definition color4d.h:72
@ PUREGREEN
Definition color4d.h:65
@ LIGHTMAGENTA
Definition color4d.h:62
@ PUREYELLOW
Definition color4d.h:69
@ YELLOW
Definition color4d.h:63
@ PUREBLUE
Definition color4d.h:64
@ LIGHTRED
Definition color4d.h:61
@ MASKCOLOR
mask for color index into colorRefs()[]
Definition color4d.h:77
@ BLACK
Definition color4d.h:40
@ DARKGREEN
Definition color4d.h:47
@ PUREMAGENTA
Definition color4d.h:68
@ RED
Definition color4d.h:55
@ DARKBROWN
Definition color4d.h:51
static constexpr std::size_t hash_val(const Types &... args)
Definition hash.h:47
#define KICOMMON_API
Definition kicommon.h:27
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition color4d.cpp:301
bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition color4d.cpp:250
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition color4d.cpp:292
bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
Definition color4d.cpp:265
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
Definition color4d.cpp:283
bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition color4d.cpp:259
STL namespace.
unsigned char m_Blue
Definition color4d.h:83
EDA_COLOR_T m_LightColor
Definition color4d.h:88
std::string m_ColorName
Definition color4d.h:87
EDA_COLOR_T m_Numcolor
Definition color4d.h:86
unsigned char m_Green
Definition color4d.h:84
unsigned char m_Red
Definition color4d.h:85
std::size_t operator()(const KIGFX::COLOR4D &aColor) const
Definition color4d.h:428