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 <nlohmann/json_fwd.hpp>
32 
33 #ifdef WX_COMPATIBILITY
34 #include <wx/colour.h>
35 #include <wx/string.h>
36 #endif
37 
42 {
44  BLACK = 0,
59  RED,
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 
95 const StructColors* colorRefs();
96 
97 
98 namespace KIGFX
99 {
103 class COLOR4D
104 {
105 public:
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 
148 #ifdef WX_COMPATIBILITY
149 
152  COLOR4D( const wxColour& aColor );
153 
160  bool SetFromWxString( const wxString& aColorString );
161 
162  wxString ToWxString( long flags ) const;
163 
164  bool SetFromHexString( const wxString& aColorString );
165  wxString ToHexString() const;
166 
167  wxColour ToColour() const;
168 
177  COLOR4D LegacyMix( const COLOR4D& aColor ) const;
178 
184  unsigned int ToU32() const;
185 
189  void FromU32( unsigned int aPackedColor );
190 #endif /* WX_COMPATIBILITY */
191 
192 
201  void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
202 
210  void FromHSL( double aInHue, double aInSaturation, double aInLightness );
211 
218  COLOR4D& Brighten( double aFactor )
219  {
220  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
221 
222  r = r * ( 1.0 - aFactor ) + aFactor;
223  g = g * ( 1.0 - aFactor ) + aFactor;
224  b = b * ( 1.0 - aFactor ) + aFactor;
225 
226  return *this;
227  }
228 
235  COLOR4D& Darken( double aFactor )
236  {
237  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
238 
239  r = r * ( 1.0 - aFactor );
240  g = g * ( 1.0 - aFactor );
241  b = b * ( 1.0 - aFactor );
242 
243  return *this;
244  }
245 
252  {
253  r = ( 1.0 - r );
254  g = ( 1.0 - g );
255  b = ( 1.0 - b );
256 
257  return *this;
258  }
259 
263  COLOR4D& Saturate( double aFactor );
264 
271  COLOR4D Brightened( double aFactor ) const
272  {
273  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
274 
275  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
276  b * ( 1.0 - aFactor ) + aFactor, a );
277  }
278 
285  COLOR4D Darkened( double aFactor ) const
286  {
287  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
288 
289  return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
290  }
291 
298  COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
299  {
300  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
301 
302  return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
303  aColor.g * ( 1.0 - aFactor ) + g * aFactor,
304  aColor.b * ( 1.0 - aFactor ) + b * aFactor,
305  a );
306  }
307 
314  COLOR4D WithAlpha( double aAlpha ) const
315  {
316  wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
317 
318  return COLOR4D( r, g, b, aAlpha );
319  }
320 
327  {
328  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
329  }
330 
336  double GetBrightness() const
337  {
338  // Weighted W3C formula
339  return r * 0.299 + g * 0.587 + b * 0.117;
340  }
341 
354  void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
355  bool aAlwaysDefineHue = false ) const;
356 
364  void FromHSV( double aInH, double aInS, double aInV );
365 
369  int Distance( const COLOR4D& other ) const;
370 
374  static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
375 
376  // Color components: red, green, blue, alpha
377  double r;
378  double g;
379  double b;
380  double a;
381 
383  static const COLOR4D UNSPECIFIED;
384 
385  // Declare a few color shortcuts that are used for comparisons frequently
386  static const COLOR4D WHITE;
387  static const COLOR4D BLACK;
388  static const COLOR4D CLEAR;
389 };
390 
392 const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
393 
395 const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
396 
397 const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
398 
400 std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
401 
402 // to allow json( COLOR4D )
403 void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
404 
405 // To allow json::get<COLOR4D>()
406 void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
407 
408 } // namespace KIGFX
409 
410 #endif /* COLOR4D_H_ */
COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Definition: color4d.h:121
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition: color4d.h:251
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
Definition: color4d.cpp:357
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:336
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:551
unsigned char m_Red
Definition: color4d.h:88
COLOR4D Inverted() const
Returns an inverted color, alpha remains the same.
Definition: color4d.h:326
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
COLOR4D & Brighten(double aFactor)
Makes the color brighter by a given factor.
Definition: color4d.h:218
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
Definition: color4d.cpp:485
std::string m_ColorName
Definition: color4d.h:90
unsigned char m_Green
Definition: color4d.h:87
mask for color index into colorRefs()[]
Definition: color4d.h:81
Definition: color4d.h:61
static const COLOR4D BLACK
Definition: color4d.h:387
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:314
double g
Green component.
Definition: color4d.h:378
Definition: color4d.h:44
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition: color4d.h:285
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition: color4d.cpp:277
nlohmann::json json
Definition: gerbview.cpp:41
const bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
Definition: color4d.cpp:250
double b
Blue component.
Definition: color4d.h:379
Number of colors.
Definition: color4d.h:79
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:271
Definition: color4d.h:76
Definition: color4d.h:67
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition: color4d.cpp:39
double a
Alpha component.
Definition: color4d.h:380
static const COLOR4D CLEAR
Definition: color4d.h:388
Definition: color4d.h:57
unsigned char m_Blue
Definition: color4d.h:86
Definition: color4d.h:58
int Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
Definition: color4d.cpp:506
static const COLOR4D WHITE
Definition: color4d.h:386
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Converts current color (stored in RGB) to HSL format.
Definition: color4d.cpp:285
Definition: color4d.h:59
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
Definition: color4d.cpp:265
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:411
EDA_COLOR_T
Legacy color enumeration.
Definition: color4d.h:41
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition: color4d.cpp:271
Definition: color4d.h:48
Definition: color4d.h:56
EDA_COLOR_T m_LightColor
Definition: color4d.h:91
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition: color4d.cpp:238
COLOR4D & Darken(double aFactor)
Makes the color darker by a given factor.
Definition: color4d.h:235
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:383
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:514
EDA_COLOR_T m_Numcolor
Definition: color4d.h:89
double r
Red component.
Definition: color4d.h:377
const bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition: color4d.cpp:244
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition: color4d.h:298
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:316
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103