KiCad PCB EDA Suite
KIGFX::COLOR4D Class Reference

Class COLOR4D is the color representation with 4 components: red, green, blue, alpha. More...

#include <color4d.h>

Public Member Functions

 COLOR4D ()
 
 COLOR4D (double aRed, double aGreen, double aBlue, double aAlpha)
 Constructor. More...
 
 COLOR4D (EDA_COLOR_T aColor)
 Constructor. More...
 
COLOR4DBrighten (double aFactor)
 Function Brighten Makes the color brighter by a given factor. More...
 
COLOR4DDarken (double aFactor)
 Function Darken Makes the color darker by a given factor. More...
 
COLOR4DInvert ()
 Function Invert Makes the color inverted, alpha remains the same. More...
 
COLOR4DSaturate (double aFactor)
 Saturates the color to a given factor (in HSV model) More...
 
COLOR4D Brightened (double aFactor) const
 Function Brightened Returns a color that is brighter by a given factor, without modifying object. More...
 
COLOR4D Darkened (double aFactor) const
 Function Darkened Returns a color that is darker by a given factor, without modifying object. More...
 
COLOR4D WithAlpha (double aAlpha) const
 Function WithAlpha Returns a colour with the same colour, but the given alpha. More...
 
COLOR4D Inverted () const
 Function Inverted Returns an inverted color, alpha remains the same. More...
 
double GetBrightness () const
 Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1.0. More...
 
void ToHSV (double &aOutH, double &aOutS, double &aOutV) const
 Function ToHSV() Converts current color (stored in RGB) to HSV format. More...
 
void FromHSV (double aInH, double aInS, double aInV)
 Function FromHSV() Changes currently used color to the one given by hue, saturation and value parameters. More...
 

Public Attributes

double r
 Red component. More...
 
double g
 Green component. More...
 
double b
 Blue component. More...
 
double a
 Alpha component. More...
 

Static Public Attributes

static const COLOR4D UNSPECIFIED
 For legacy support; used as a value to indicate color hasn't been set yet. More...
 
static const COLOR4D WHITE
 
static const COLOR4D BLACK
 

Detailed Description

Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.

Definition at line 39 of file color4d.h.

Constructor & Destructor Documentation

KIGFX::COLOR4D::COLOR4D ( )
inline

Definition at line 43 of file color4d.h.

Referenced by Brightened(), Darkened(), Inverted(), and WithAlpha().

43  :
44  r( 0 ), g( 0 ), b( 0 ), a( 0 )
45  {
46  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
double r
Red component.
Definition: color4d.h:281
KIGFX::COLOR4D::COLOR4D ( double  aRed,
double  aGreen,
double  aBlue,
double  aAlpha 
)
inline

Constructor.

Parameters
aRedis the red component [0.0 .. 1.0].
aGreenis the green component [0.0 .. 1.0].
aBlueis the blue component [0.0 .. 1.0].
aAlphais the alpha value [0.0 .. 1.0].

Definition at line 56 of file color4d.h.

References a, b, g, and r.

56  :
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  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
double r
Red component.
Definition: color4d.h:281
COLOR4D::COLOR4D ( EDA_COLOR_T  aColor)

Constructor.

Parameters
aColoris one of KiCad's palette colors.
See also
EDA_COLOR_T

Definition at line 33 of file color4d.cpp.

References a, b, g, g_ColorRefs, StructColors::m_Blue, StructColors::m_Green, StructColors::m_Red, NBCOLORS, r, and UNSPECIFIED.

34 {
35  if( aColor <= UNSPECIFIED_COLOR || aColor >= NBCOLORS )
36  {
37  *this = COLOR4D::UNSPECIFIED;
38  return;
39  }
40 
41  r = g_ColorRefs[aColor].m_Red / 255.0;
42  g = g_ColorRefs[aColor].m_Green / 255.0;
43  b = g_ColorRefs[aColor].m_Blue / 255.0;
44  a = 1.0;
45 }
unsigned char m_Red
Definition: colors.h:146
unsigned char m_Green
Definition: colors.h:145
double g
Green component.
Definition: color4d.h:282
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:287
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
unsigned char m_Blue
Definition: colors.h:144
const StructColors g_ColorRefs[NBCOLORS]
The predefined colors used in KiCad.
Definition: colors.cpp:37
double r
Red component.
Definition: color4d.h:281
Number of colors.
Definition: colors.h:75

Member Function Documentation

COLOR4D& KIGFX::COLOR4D::Brighten ( double  aFactor)
inline

Function Brighten Makes the color brighter by a given factor.

Parameters
aFactorSpecifies how bright the color should become (valid values: 0.0 .. 1.0).
Returns
COLOR4D& Brightened color.

Definition at line 147 of file color4d.h.

References b, g, and r.

148  {
149  assert( aFactor >= 0.0 && aFactor <= 1.0 );
150 
151  r = r * ( 1.0 - aFactor ) + aFactor;
152  g = g * ( 1.0 - aFactor ) + aFactor;
153  b = b * ( 1.0 - aFactor ) + aFactor;
154 
155  return *this;
156  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281
COLOR4D KIGFX::COLOR4D::Brightened ( double  aFactor) const
inline

Function Brightened Returns a color that is brighter by a given factor, without modifying object.

Parameters
aFactorSpecifies how bright the color should become (valid values: 0.0 .. 1.0).
Returns
COLOR4D Highlighted color.

Definition at line 200 of file color4d.h.

References a, b, COLOR4D(), g, and r.

Referenced by KIGFX::RENDER_SETTINGS::update().

201  {
202  assert( aFactor >= 0.0 && aFactor <= 1.0 );
203 
204  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor,
205  g * ( 1.0 - aFactor ) + aFactor,
206  b * ( 1.0 - aFactor ) + aFactor,
207  a );
208  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
double r
Red component.
Definition: color4d.h:281
COLOR4D& KIGFX::COLOR4D::Darken ( double  aFactor)
inline

Function Darken Makes the color darker by a given factor.

Parameters
aFactorSpecifies how dark the color should become (valid values: 0.0 .. 1.0).
Returns
COLOR4D& Darkened color.

Definition at line 164 of file color4d.h.

References b, g, and r.

165  {
166  assert( aFactor >= 0.0 && aFactor <= 1.0 );
167 
168  r = r * ( 1.0 - aFactor );
169  g = g * ( 1.0 - aFactor );
170  b = b * ( 1.0 - aFactor );
171 
172  return *this;
173  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281
COLOR4D KIGFX::COLOR4D::Darkened ( double  aFactor) const
inline

Function Darkened Returns a color that is darker by a given factor, without modifying object.

Parameters
aFactorSpecifies how dark the color should become (valid values: 0.0 .. 1.0).
Returns
COLOR4D Darkened color.

Definition at line 216 of file color4d.h.

References a, b, COLOR4D(), g, and r.

Referenced by KIGFX::RENDER_SETTINGS::update().

217  {
218  assert( aFactor >= 0.0 && aFactor <= 1.0 );
219 
220  return COLOR4D( r * ( 1.0 - aFactor ),
221  g * ( 1.0 - aFactor ),
222  b * ( 1.0 - aFactor ),
223  a );
224  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
double r
Red component.
Definition: color4d.h:281
void COLOR4D::FromHSV ( double  aInH,
double  aInS,
double  aInV 
)

Function FromHSV() Changes currently used color to the one given by hue, saturation and value parameters.

Parameters
aInHis hue component.
aInSis saturation component.
aInVis value component.

Definition at line 306 of file color4d.cpp.

References b, g, and r.

Referenced by Saturate().

307 {
308  double hh, p, q, t, ff;
309  long i;
310 
311  if( aInS <= 0.0 ) // < is bogus, just shuts up warnings
312  {
313  r = aInV;
314  g = aInV;
315  b = aInV;
316  return;
317  }
318 
319  hh = aInH;
320 
321  if( hh >= 360.0 )
322  hh = 0.0;
323 
324  hh /= 60.0;
325 
326  i = (long) hh;
327  ff = hh - i;
328 
329  p = aInV * ( 1.0 - aInS );
330  q = aInV * ( 1.0 - ( aInS * ff ) );
331  t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
332 
333  switch( i )
334  {
335  case 0:
336  r = aInV;
337  g = t;
338  b = p;
339  break;
340 
341  case 1:
342  r = q;
343  g = aInV;
344  b = p;
345  break;
346 
347  case 2:
348  r = p;
349  g = aInV;
350  b = t;
351  break;
352 
353  case 3:
354  r = p;
355  g = q;
356  b = aInV;
357  break;
358 
359  case 4:
360  r = t;
361  g = p;
362  b = aInV;
363  break;
364 
365  case 5:
366  default:
367  r = aInV;
368  g = p;
369  b = q;
370  break;
371  }
372 }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281
double KIGFX::COLOR4D::GetBrightness ( ) const
inline

Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1.0.

Returns
The brightness value.

Definition at line 254 of file color4d.h.

References b, g, and r.

Referenced by DrawGraphicHaloText(), and EDA_DRAW_PANEL::ReDraw().

255  {
256  // Weighted W3C formula
257  return r * 0.299 + g * 0.587 + b * 0.117;
258  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281
COLOR4D& KIGFX::COLOR4D::Invert ( )
inline

Function Invert Makes the color inverted, alpha remains the same.

Returns
COLOR4D& Inverted color.

Definition at line 180 of file color4d.h.

References b, g, and r.

181  {
182  r = ( 1.0 - r );
183  g = ( 1.0 - g );
184  b = ( 1.0 - b );
185 
186  return *this;
187  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281
COLOR4D KIGFX::COLOR4D::Inverted ( ) const
inline

Function Inverted Returns an inverted color, alpha remains the same.

Returns
COLOR4D& Inverted color.

Definition at line 244 of file color4d.h.

References a, b, COLOR4D(), g, and r.

Referenced by KIGFX::PCB_RENDER_SETTINGS::ImportLegacyColors().

245  {
246  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
247  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double a
Alpha component.
Definition: color4d.h:284
double r
Red component.
Definition: color4d.h:281
COLOR4D & COLOR4D::Saturate ( double  aFactor)

Saturates the color to a given factor (in HSV model)

Definition at line 375 of file color4d.cpp.

References FromHSV(), and ToHSV().

376 {
377  double h, s, v;
378 
379  ToHSV( h, s, v );
380  FromHSV( h, aFactor, 1.0 );
381 
382  return *this;
383 }
void ToHSV(double &aOutH, double &aOutS, double &aOutV) const
Function ToHSV() Converts current color (stored in RGB) to HSV format.
Definition: color4d.cpp:267
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:306
void COLOR4D::ToHSV ( double &  aOutH,
double &  aOutS,
double &  aOutV 
) const

Function ToHSV() Converts current color (stored in RGB) to HSV format.

Parameters
aOutHis conversion result for hue component.
aOutSis conversion result for saturation component.
aOutVis conversion result for value component.

Definition at line 267 of file color4d.cpp.

References b, delta, g, max, min, and r.

Referenced by Saturate().

268 {
269  double min, max, delta;
270 
271  min = r < g ? r : g;
272  min = min < b ? min : b;
273 
274  max = r > g ? r : g;
275  max = max > b ? max : b;
276 
277  aOutV = max; // v
278  delta = max - min;
279 
280  if( max > 0.0 )
281  {
282  aOutS = ( delta / max ); // s
283  }
284  else
285  {
286  // r = g = b = 0 // s = 0, v is undefined
287  aOutS = 0.0;
288  aOutH = NAN; // its now undefined
289  return;
290  }
291 
292  if( r >= max ) // > is bogus, just keeps compiler happy
293  aOutH = ( g - b ) / delta; // between yellow & magenta
294  else if( g >= max )
295  aOutH = 2.0 + ( b - r ) / delta; // between cyan & yellow
296  else
297  aOutH = 4.0 + ( r - g ) / delta; // between magenta & cyan
298 
299  aOutH *= 60.0; // degrees
300 
301  if( aOutH < 0.0 )
302  aOutH += 360.0;
303 }
double g
Green component.
Definition: color4d.h:282
static const int delta[8][2]
Definition: solve.cpp:112
double b
Blue component.
Definition: color4d.h:283
#define max(a, b)
Definition: auxiliary.h:86
double r
Red component.
Definition: color4d.h:281
#define min(a, b)
Definition: auxiliary.h:85
COLOR4D KIGFX::COLOR4D::WithAlpha ( double  aAlpha) const
inline

Function WithAlpha Returns a colour with the same colour, but the given alpha.

Parameters
aAlphaspecifies the alpha of the new color
Returns
COLOR4D color with that alpha

Definition at line 232 of file color4d.h.

References b, COLOR4D(), g, and r.

Referenced by drawArcWithHilight(), drawLineWithHilight(), and ZONE_CREATE_HELPER::OnFirstPoint().

233  {
234  assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
235 
236  return COLOR4D( r, g, b, aAlpha );
237  }
double g
Green component.
Definition: color4d.h:282
double b
Blue component.
Definition: color4d.h:283
double r
Red component.
Definition: color4d.h:281

Member Data Documentation

const COLOR4D COLOR4D::BLACK
static

Definition at line 291 of file color4d.h.

return COLOR4D::UNSPECIFIED
static

For legacy support; used as a value to indicate color hasn't been set yet.

Definition at line 287 of file color4d.h.

Referenced by COLOR4D().

const COLOR4D COLOR4D::WHITE
static

Definition at line 290 of file color4d.h.


The documentation for this class was generated from the following files: