Horizon
Loading...
Searching...
No Matches
color4d.h
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_compat.h"
31
35enum EDA_COLOR_T
36{
37 UNSPECIFIED_COLOR = -1,
38 BLACK = 0,
39 DARKDARKGRAY,
40 DARKGRAY,
41 LIGHTGRAY,
42 WHITE,
43 LIGHTYELLOW,
44 DARKBLUE,
45 DARKGREEN,
46 DARKCYAN,
47 DARKRED,
48 DARKMAGENTA,
49 DARKBROWN,
50 BLUE,
51 GREEN,
52 CYAN,
53 RED,
54 MAGENTA,
55 BROWN,
56 LIGHTBLUE,
57 LIGHTGREEN,
58 LIGHTCYAN,
59 LIGHTRED,
60 LIGHTMAGENTA,
61 YELLOW,
62 PUREBLUE,
63 PUREGREEN,
64 PURECYAN,
65 PURERED,
66 PUREMAGENTA,
67 PUREYELLOW,
68 LIGHTERORANGE,
69 DARKORANGE,
70 ORANGE,
71 LIGHTORANGE,
72 PUREORANGE,
73 NBCOLORS,
74 HIGHLIGHT_FLAG = ( 1<<19 ),
75 MASKCOLOR = 31
76};
77
79{
80 unsigned char m_Blue;
81 unsigned char m_Green;
82 unsigned char m_Red;
83 EDA_COLOR_T m_Numcolor;
84 std::string m_ColorName;
85 EDA_COLOR_T m_LightColor;
86};
87
89const StructColors* colorRefs();
90
91
92namespace KIGFX
93{
98{
99public:
100 // Constructor (creates the Color 0,0,0,0)
101 COLOR4D() :
102 r( 0 ),
103 g( 0 ),
104 b( 0 ),
105 a( 1.0 )
106 {
107 }
108
115 COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
116 r( aRed ),
117 g( aGreen ),
118 b( aBlue ),
119 a( aAlpha )
120 {
121 wxASSERT( r >= 0.0 && r <= 1.0 );
122 wxASSERT( g >= 0.0 && g <= 1.0 );
123 wxASSERT( b >= 0.0 && b <= 1.0 );
124 wxASSERT( a >= 0.0 && a <= 1.0 );
125 }
126
131 COLOR4D( EDA_COLOR_T aColor );
132
140 COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
141
142#ifdef WX_COMPATIBILITY
146 COLOR4D( const wxColour& aColor );
147
154 bool SetFromWxString( const wxString& aColorString );
155
156 wxString ToWxString( long flags ) const;
157
158 bool SetFromHexString( const wxString& aColorString );
159 wxString ToHexString() const;
160
161 wxColour ToColour() const;
162
171 COLOR4D LegacyMix( const COLOR4D& aColor ) const;
172
178 unsigned int ToU32() const;
179
183 void FromU32( unsigned int aPackedColor );
184#endif /* WX_COMPATIBILITY */
185
186
195 void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
196
204 void FromHSL( double aInHue, double aInSaturation, double aInLightness );
205
212 COLOR4D& Brighten( double aFactor )
213 {
214 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
215
216 r = r * ( 1.0 - aFactor ) + aFactor;
217 g = g * ( 1.0 - aFactor ) + aFactor;
218 b = b * ( 1.0 - aFactor ) + aFactor;
219
220 return *this;
221 }
222
229 COLOR4D& Darken( double aFactor )
230 {
231 wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
232
233 r = r * ( 1.0 - aFactor );
234 g = g * ( 1.0 - aFactor );
235 b = b * ( 1.0 - aFactor );
236
237 return *this;
238 }
239
246 {
247 r = ( 1.0 - r );
248 g = ( 1.0 - g );
249 b = ( 1.0 - b );
250
251 return *this;
252 }
253
257 COLOR4D& Saturate( double aFactor );
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 int Distance( const COLOR4D& other ) const;
364
368 static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
369
370 // Color components: red, green, blue, alpha
371 double r;
372 double g;
373 double b;
374 double a;
375
377 static const COLOR4D UNSPECIFIED;
378
379 // Declare a few color shortcuts that are used for comparisons frequently
380 static const COLOR4D WHITE;
381 static const COLOR4D BLACK;
382 static const COLOR4D CLEAR;
383};
384
386const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
387
389const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
390
391const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
392
394std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
395
396} // namespace KIGFX
397
398#endif /* COLOR4D_H_ */
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:98
void ToHSL(double &aOutHue, double &aOutSaturation, double &aOutValue) const
Converts current color (stored in RGB) to HSL format.
Definition color4d.cpp:264
double r
Red component.
Definition color4d.h:371
double g
Green component.
Definition color4d.h:372
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition color4d.h:245
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
Definition color4d.cpp:336
int Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
Definition color4d.cpp:485
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:493
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:390
COLOR4D & Darken(double aFactor)
Makes the color darker by a given factor.
Definition color4d.h:229
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:212
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition color4d.h:330
double a
Alpha component.
Definition color4d.h:374
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:377
COLOR4D(double aRed, double aGreen, double aBlue, double aAlpha)
Definition color4d.h:115
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:295
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition color4d.h:292
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:530
COLOR4D & Saturate(double aFactor)
Saturates the color to a given factor (in HSV model)
Definition color4d.cpp:464
double b
Blue component.
Definition color4d.h:373
Definition wx_compat.h:13
Definition color4d.h:79