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 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 <colors.h>
31 #include <cassert>
32 
33 namespace KIGFX
34 {
39 class COLOR4D
40 {
41 public:
42  // Constructor (creates the Color 0,0,0,0)
43  COLOR4D() :
44  r( 0 ), g( 0 ), b( 0 ), a( 1.0 )
45  {
46  }
47 
56  COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
57  r( aRed ), g( aGreen ), b( aBlue ), a( aAlpha )
58  {
59  assert( r >= 0.0 && r <= 1.0 );
60  assert( g >= 0.0 && g <= 1.0 );
61  assert( b >= 0.0 && b <= 1.0 );
62  assert( a >= 0.0 && a <= 1.0 );
63  }
64 
71  COLOR4D( EDA_COLOR_T aColor );
72 
73 #ifdef WX_COMPATIBILITY
74 
79  COLOR4D( const wxColour& aColor );
80 
88  bool SetFromWxString( const wxString& aColorString );
89 
90  wxString ToWxString( long flags ) const;
91 
92  wxColour ToColour() const;
93 
104  COLOR4D LegacyMix( COLOR4D aColor ) const;
105 
111  COLOR4D& SetToLegacyHighlightColor();
112 
118  COLOR4D& SetToNearestLegacyColor();
119 
120  COLOR4D AsLegacyColor() const
121  {
122  return COLOR4D( COLOR4D::GetNearestLegacyColor( *this ) );
123  }
124 
129  unsigned int ToU32() const;
130 
134  void FromU32( unsigned int aPackedColor );
135 
139  static EDA_COLOR_T GetNearestLegacyColor( const COLOR4D &aColor );
140 #endif /* WX_COMPATIBLITY */
141 
142 
152  void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
153 
162  void FromHSL( double aInHue, double aInSaturation, double aInLightness );
163 
170  COLOR4D& Brighten( double aFactor )
171  {
172  assert( aFactor >= 0.0 && aFactor <= 1.0 );
173 
174  r = r * ( 1.0 - aFactor ) + aFactor;
175  g = g * ( 1.0 - aFactor ) + aFactor;
176  b = b * ( 1.0 - aFactor ) + aFactor;
177 
178  return *this;
179  }
180 
187  COLOR4D& Darken( double aFactor )
188  {
189  assert( aFactor >= 0.0 && aFactor <= 1.0 );
190 
191  r = r * ( 1.0 - aFactor );
192  g = g * ( 1.0 - aFactor );
193  b = b * ( 1.0 - aFactor );
194 
195  return *this;
196  }
197 
204  {
205  r = ( 1.0 - r );
206  g = ( 1.0 - g );
207  b = ( 1.0 - b );
208 
209  return *this;
210  }
211 
215  COLOR4D& Saturate( double aFactor );
216 
223  COLOR4D Brightened( double aFactor ) const
224  {
225  assert( aFactor >= 0.0 && aFactor <= 1.0 );
226 
227  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor,
228  g * ( 1.0 - aFactor ) + aFactor,
229  b * ( 1.0 - aFactor ) + aFactor,
230  a );
231  }
232 
239  COLOR4D Darkened( double aFactor ) const
240  {
241  assert( aFactor >= 0.0 && aFactor <= 1.0 );
242 
243  return COLOR4D( r * ( 1.0 - aFactor ),
244  g * ( 1.0 - aFactor ),
245  b * ( 1.0 - aFactor ),
246  a );
247  }
248 
255  COLOR4D WithAlpha( double aAlpha ) const
256  {
257  assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
258 
259  return COLOR4D( r, g, b, aAlpha );
260  }
261 
268  {
269  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
270  }
271 
277  double GetBrightness() const
278  {
279  // Weighted W3C formula
280  return r * 0.299 + g * 0.587 + b * 0.117;
281  }
282 
296  void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue, bool aAlwaysDefineHue = false ) const;
297 
306  void FromHSV( double aInH, double aInS, double aInV );
307 
308  // Color components: red, green, blue, alpha
309  double r;
310  double g;
311  double b;
312  double a;
313 
315  static const COLOR4D UNSPECIFIED;
316 
317  // Declare a few color shortcuts that are used for comparisons frequently
318  static const COLOR4D WHITE;
319  static const COLOR4D BLACK;
320 };
321 
323 const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
324 
326 const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
327 
329 std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
330 
331 } // namespace KIGFX
332 
333 #endif /* COLOR4D_H_ */
COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Constructor.
Definition: color4d.h:56
COLOR4D & Invert()
Function Invert Makes the color inverted, alpha remains the same.
Definition: color4d.h:203
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
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:358
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:277
COLOR4D Inverted() const
Function Inverted Returns an inverted color, alpha remains the same.
Definition: color4d.h:267
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
COLOR4D & Brighten(double aFactor)
Function Brighten Makes the color brighter by a given factor.
Definition: color4d.h:170
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
Definition: color4d.cpp:485
static const COLOR4D BLACK
Definition: color4d.h:319
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:255
double g
Green component.
Definition: color4d.h:310
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:315
COLOR4D Darkened(double aFactor) const
Function Darkened Returns a color that is darker by a given factor, without modifying object.
Definition: color4d.h:239
double b
Blue component.
Definition: color4d.h:311
COLOR4D Brightened(double aFactor) const
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:223
double a
Alpha component.
Definition: color4d.h:312
static const COLOR4D WHITE
Definition: color4d.h:318
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Function ToHSL() Converts current color (stored in RGB) to HSL format.
Definition: color4d.cpp:286
std::ostream & operator<<(std::ostream &aStream, COLOR4D const &aColor)
Syntactic sugar for outputting colors to strings.
Definition: color4d.cpp:278
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:411
YYCODETYPE lhs
const bool operator==(const COLOR4D &lhs, const COLOR4D &rhs)
Equality operator, are two colors equal.
Definition: color4d.cpp:267
COLOR4D & Darken(double aFactor)
Function Darken Makes the color darker by a given factor.
Definition: color4d.h:187
double r
Red component.
Definition: color4d.h:309
const bool operator!=(const COLOR4D &lhs, const COLOR4D &rhs)
Not equality operator, are two colors not equal.
Definition: color4d.cpp:273
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:317
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39