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/colour.h>
35 #include <wx/string.h>
36 #endif
37 
43 {
45  BLACK = 0,
60  RED,
76  HIGHLIGHT_FLAG = ( 1<<19 ),
77  MASKCOLOR = 31
78 };
79 
81 {
82  unsigned char m_Blue;
83  unsigned char m_Green;
84  unsigned char m_Red;
86  std::string m_ColorName;
88 };
89 
91 const StructColors* colorRefs();
92 
93 
94 namespace KIGFX
95 {
100 class COLOR4D
101 {
102 public:
103  // Constructor (creates the Color 0,0,0,0)
105  r( 0 ), g( 0 ), b( 0 ), a( 1.0 )
106  {
107  }
108 
117  constexpr COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
118  r( aRed ), g( aGreen ), b( aBlue ), a( aAlpha )
119  {
120  assert( r >= 0.0 && r <= 1.0 );
121  assert( g >= 0.0 && g <= 1.0 );
122  assert( b >= 0.0 && b <= 1.0 );
123  assert( a >= 0.0 && a <= 1.0 );
124  }
125 
132  COLOR4D( EDA_COLOR_T aColor );
133 
139  COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
140 
141 #ifdef WX_COMPATIBILITY
142 
147  COLOR4D( const wxColour& aColor );
148 
156  bool SetFromWxString( const wxString& aColorString );
157 
158  wxString ToWxString( long flags ) const;
159 
160  wxColour ToColour() const;
161 
172  COLOR4D LegacyMix( COLOR4D aColor ) const;
173 
178  unsigned int ToU32() const;
179 
183  void FromU32( unsigned int aPackedColor );
184 #endif /* WX_COMPATIBLITY */
185 
186 
196  void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
197 
206  void FromHSL( double aInHue, double aInSaturation, double aInLightness );
207 
214  COLOR4D& Brighten( double aFactor )
215  {
216  assert( aFactor >= 0.0 && aFactor <= 1.0 );
217 
218  r = r * ( 1.0 - aFactor ) + aFactor;
219  g = g * ( 1.0 - aFactor ) + aFactor;
220  b = b * ( 1.0 - aFactor ) + aFactor;
221 
222  return *this;
223  }
224 
231  COLOR4D& Darken( double aFactor )
232  {
233  assert( aFactor >= 0.0 && aFactor <= 1.0 );
234 
235  r = r * ( 1.0 - aFactor );
236  g = g * ( 1.0 - aFactor );
237  b = b * ( 1.0 - aFactor );
238 
239  return *this;
240  }
241 
248  {
249  r = ( 1.0 - r );
250  g = ( 1.0 - g );
251  b = ( 1.0 - b );
252 
253  return *this;
254  }
255 
259  COLOR4D& Saturate( double aFactor );
260 
267  COLOR4D Brightened( double aFactor ) const
268  {
269  assert( aFactor >= 0.0 && aFactor <= 1.0 );
270 
271  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor,
272  g * ( 1.0 - aFactor ) + aFactor,
273  b * ( 1.0 - aFactor ) + aFactor,
274  a );
275  }
276 
283  COLOR4D Darkened( double aFactor ) const
284  {
285  assert( aFactor >= 0.0 && aFactor <= 1.0 );
286 
287  return COLOR4D( r * ( 1.0 - aFactor ),
288  g * ( 1.0 - aFactor ),
289  b * ( 1.0 - aFactor ),
290  a );
291  }
292 
299  COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
300  {
301  assert( aFactor >= 0.0 && aFactor <= 1.0 );
302 
303  return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
304  aColor.g * ( 1.0 - aFactor ) + g * aFactor,
305  aColor.b * ( 1.0 - aFactor ) + b * aFactor,
306  a );
307  }
308 
315  COLOR4D WithAlpha( double aAlpha ) const
316  {
317  assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
318 
319  return COLOR4D( r, g, b, aAlpha );
320  }
321 
328  {
329  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
330  }
331 
337  double GetBrightness() const
338  {
339  // Weighted W3C formula
340  return r * 0.299 + g * 0.587 + b * 0.117;
341  }
342 
356  void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue, bool aAlwaysDefineHue = false ) const;
357 
366  void FromHSV( double aInH, double aInS, double aInV );
367 
371  static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
372 
373  // Color components: red, green, blue, alpha
374  double r;
375  double g;
376  double b;
377  double a;
378 
380  static const COLOR4D UNSPECIFIED;
381 
382  // Declare a few color shortcuts that are used for comparisons frequently
383  static const COLOR4D WHITE;
384  static const COLOR4D BLACK;
385  static const COLOR4D CLEAR;
386 };
387 
389 const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
390 
392 const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
393 
394 const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
395 
397 std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
398 
399 // to allow json( COLOR4D )
400 void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
401 
402 // To allow json::get<COLOR4D>()
403 void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
404 
405 } // namespace KIGFX
406 
407 #endif /* COLOR4D_H_ */
COLOR4D & Invert()
Function Invert Makes the color inverted, alpha remains the same.
Definition: color4d.h:247
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:337
COLOR4D & FromCSSRGBA(int aRed, int aGreen, int aBlue, double aAlpha=1.0)
Initializes the color from a RGBA value with 0-255 red/green/blue and 0-1 alpha.
Definition: color4d.cpp:476
unsigned char m_Red
Definition: color4d.h:84
COLOR4D Inverted() const
Function Inverted Returns an inverted color, alpha remains the same.
Definition: color4d.h:327
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:214
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:86
unsigned char m_Green
Definition: color4d.h:83
mask for color index into colorRefs()[]
Definition: color4d.h:77
Definition: color4d.h:62
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:315
double g
Green component.
Definition: color4d.h:375
Definition: color4d.h:45
COLOR4D Darkened(double aFactor) const
Function Darkened Returns a color that is darker by a given factor, without modifying object.
Definition: color4d.h:283
void from_json(const nlohmann::json &aJson, COLOR4D &aColor)
Definition: color4d.cpp:213
nlohmann::json json
Definition: gerbview.cpp:39
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:380
double b
Blue component.
Definition: color4d.h:376
Number of colors.
Definition: color4d.h:75
COLOR4D Brightened(double aFactor) const
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:267
static const COLOR4D BLACK
Definition: color4d.h:384
Definition: color4d.h:68
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:377
Definition: color4d.h:58
static const COLOR4D CLEAR
Definition: color4d.h:385
unsigned char m_Blue
Definition: color4d.h:82
Definition: color4d.h:59
static const COLOR4D WHITE
Definition: color4d.h:383
constexpr COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Constructor.
Definition: color4d.h:117
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:60
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:42
void to_json(nlohmann::json &aJson, const COLOR4D &aColor)
Definition: color4d.cpp:207
Definition: color4d.h:49
Definition: color4d.h:57
EDA_COLOR_T m_LightColor
Definition: color4d.h:87
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:231
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:85
double r
Red component.
Definition: color4d.h:374
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:299
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:100