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-2019 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 <cassert>
31 #include <nlohmann/json_fwd.hpp>
32 
33 #ifdef WX_COMPATIBILITY
34 #include <wx/wx.h>
35 #endif
36 
42 {
44  BLACK = 0,
59  RED,
75  HIGHLIGHT_FLAG = ( 1<<19 ),
76  MASKCOLOR = 31
77 };
78 
80 {
81  unsigned char m_Blue;
82  unsigned char m_Green;
83  unsigned char m_Red;
85  std::string m_ColorName;
87 };
88 
90 const StructColors* colorRefs();
91 
92 
93 namespace KIGFX
94 {
99 class COLOR4D
100 {
101 public:
102  // Constructor (creates the Color 0,0,0,0)
104  r( 0 ), g( 0 ), b( 0 ), a( 1.0 )
105  {
106  }
107 
116  constexpr COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
117  r( aRed ), g( aGreen ), b( aBlue ), a( aAlpha )
118  {
119  assert( r >= 0.0 && r <= 1.0 );
120  assert( g >= 0.0 && g <= 1.0 );
121  assert( b >= 0.0 && b <= 1.0 );
122  assert( a >= 0.0 && a <= 1.0 );
123  }
124 
131  COLOR4D( EDA_COLOR_T aColor );
132 
133 #ifdef WX_COMPATIBILITY
134 
139  COLOR4D( const wxColour& aColor );
140 
148  bool SetFromWxString( const wxString& aColorString );
149 
150  wxString ToWxString( long flags ) const;
151 
152  wxColour ToColour() const;
153 
164  COLOR4D LegacyMix( COLOR4D aColor ) const;
165 
170  unsigned int ToU32() const;
171 
175  void FromU32( unsigned int aPackedColor );
176 #endif /* WX_COMPATIBLITY */
177 
178 
188  void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
189 
198  void FromHSL( double aInHue, double aInSaturation, double aInLightness );
199 
206  COLOR4D& Brighten( double aFactor )
207  {
208  assert( 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  assert( 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 
259  COLOR4D Brightened( double aFactor ) const
260  {
261  assert( aFactor >= 0.0 && aFactor <= 1.0 );
262 
263  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor,
264  g * ( 1.0 - aFactor ) + aFactor,
265  b * ( 1.0 - aFactor ) + aFactor,
266  a );
267  }
268 
275  COLOR4D Darkened( double aFactor ) const
276  {
277  assert( aFactor >= 0.0 && aFactor <= 1.0 );
278 
279  return COLOR4D( r * ( 1.0 - aFactor ),
280  g * ( 1.0 - aFactor ),
281  b * ( 1.0 - aFactor ),
282  a );
283  }
284 
291  COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
292  {
293  assert( aFactor >= 0.0 && aFactor <= 1.0 );
294 
295  return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
296  aColor.g * ( 1.0 - aFactor ) + g * aFactor,
297  aColor.b * ( 1.0 - aFactor ) + b * aFactor,
298  a );
299  }
300 
307  COLOR4D WithAlpha( double aAlpha ) const
308  {
309  assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
310 
311  return COLOR4D( r, g, b, aAlpha );
312  }
313 
320  {
321  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
322  }
323 
329  double GetBrightness() const
330  {
331  // Weighted W3C formula
332  return r * 0.299 + g * 0.587 + b * 0.117;
333  }
334 
348  void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue, bool aAlwaysDefineHue = false ) const;
349 
358  void FromHSV( double aInH, double aInS, double aInV );
359 
363  static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
364 
365  // Color components: red, green, blue, alpha
366  double r;
367  double g;
368  double b;
369  double a;
370 
372  static const COLOR4D UNSPECIFIED;
373 
374  // Declare a few color shortcuts that are used for comparisons frequently
375  static const COLOR4D WHITE;
376  static const COLOR4D BLACK;
377  static const COLOR4D CLEAR;
378 };
379 
381 const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
382 
384 const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
385 
386 const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
387 
389 std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
390 
391 // to allow json( COLOR4D )
392 void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
393 
394 // To allow json::get<COLOR4D>()
395 void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
396 
397 } // namespace KIGFX
398 
399 #endif /* COLOR4D_H_ */
COLOR4D & Invert()
Function Invert Makes the color inverted, alpha remains the same.
Definition: color4d.h:239
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Function ToHSV() Converts current color (stored in RGB) to HSV format.
Definition: color4d.cpp:293
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:329
unsigned char m_Red
Definition: color4d.h:83
COLOR4D Inverted() const
Function Inverted Returns an inverted color, alpha remains the same.
Definition: color4d.h:319
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
COLOR4D & Brighten(double aFactor)
Function Brighten Makes the color brighter by a given factor.
Definition: color4d.h:206
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
Definition: color4d.cpp:420
std::string m_ColorName
Definition: color4d.h:85
unsigned char m_Green
Definition: color4d.h:82
mask for color index into colorRefs()[]
Definition: color4d.h:76
Definition: color4d.h:61
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:307
double g
Green component.
Definition: color4d.h:367
Definition: color4d.h:44
COLOR4D Darkened(double aFactor) const
Function Darkened Returns a color that is darker by a given factor, without modifying object.
Definition: color4d.h:275
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition: color4d.cpp:213
nlohmann::json json
Definition: gerbview.cpp:40
const bool operator<(const COLOR4D &lhs, const COLOR4D &rhs)
Definition: color4d.cpp:188
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:368
Number of colors.
Definition: color4d.h:74
COLOR4D Brightened(double aFactor) const
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:259
static const COLOR4D BLACK
Definition: color4d.h:376
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:37
double a
Alpha component.
Definition: color4d.h:369
Definition: color4d.h:57
static const COLOR4D CLEAR
Definition: color4d.h:377
unsigned char m_Blue
Definition: color4d.h:81
Definition: color4d.h:58
static const COLOR4D WHITE
Definition: color4d.h:375
constexpr COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Constructor.
Definition: color4d.h:116
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Function ToHSL() Converts current color (stored in RGB) to HSL format.
Definition: color4d.cpp:221
Definition: color4d.h:59
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
Definition: color4d.cpp:202
void FromHSV(double aInH, double aInS, double aInV)
Function FromHSV() Changes currently used color to the one given by hue, saturation and value paramet...
Definition: color4d.cpp:346
EDA_COLOR_T
Legacy color enumeration.
Definition: color4d.h:41
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition: color4d.cpp:207
Definition: color4d.h:48
Definition: color4d.h:56
EDA_COLOR_T m_LightColor
Definition: color4d.h:86
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition: color4d.cpp:177
COLOR4D & Darken(double aFactor)
Function Darken Makes the color darker by a given factor.
Definition: color4d.h:223
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:440
EDA_COLOR_T m_Numcolor
Definition: color4d.h:84
double r
Red component.
Definition: color4d.h:366
const bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition: color4d.cpp:183
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Function Mix Returns a color that is mixed with the input by a factor.
Definition: color4d.h:291
void FromHSL(double aInHue, double aInSaturation, double aInLightness)
Function FromHSL() Changes currently used color to the one given by hue, saturation and lightness par...
Definition: color4d.cpp:252
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99