KiCad PCB EDA Suite
KIGFX::COLOR4D Class Reference

A 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)
 
 COLOR4D (EDA_COLOR_T aColor)
 
COLOR4DFromCSSRGBA (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. More...
 
void ToHSL (double &aOutHue, double &aOutSaturation, double &aOutValue) const
 Converts current color (stored in RGB) to HSL format. More...
 
void FromHSL (double aInHue, double aInSaturation, double aInLightness)
 Change currently used color to the one given by hue, saturation and lightness parameters. More...
 
COLOR4DBrighten (double aFactor)
 Makes the color brighter by a given factor. More...
 
COLOR4DDarken (double aFactor)
 Makes the color darker by a given factor. More...
 
COLOR4DInvert ()
 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
 Return a color that is brighter by a given factor, without modifying object. More...
 
COLOR4D Darkened (double aFactor) const
 Return a color that is darker by a given factor, without modifying object. More...
 
COLOR4D Mix (const COLOR4D &aColor, double aFactor) const
 Return a color that is mixed with the input by a factor. More...
 
COLOR4D WithAlpha (double aAlpha) const
 Return a color with the same color, but the given alpha. More...
 
COLOR4D Inverted () const
 Returns an inverted color, alpha remains the same. More...
 
double GetBrightness () const
 Returns the brightness value of the color ranged from 0.0 to 1.0. More...
 
void ToHSV (double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
 Convert current color (stored in RGB) to HSV format. More...
 
void FromHSV (double aInH, double aInS, double aInV)
 Changes currently used color to the one given by hue, saturation and value parameters. More...
 
int Distance (const COLOR4D &other) const
 Returns the distance (in RGB space) between two colors. More...
 

Static Public Member Functions

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. 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
 
static const COLOR4D CLEAR
 

Detailed Description

A color representation with 4 components: red, green, blue, alpha.

Definition at line 103 of file color4d.h.

Constructor & Destructor Documentation

◆ COLOR4D() [1/3]

KIGFX::COLOR4D::COLOR4D ( )
inline

Definition at line 107 of file color4d.h.

107  :
108  r( 0 ),
109  g( 0 ),
110  b( 0 ),
111  a( 1.0 )
112  {
113  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

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

◆ COLOR4D() [2/3]

KIGFX::COLOR4D::COLOR4D ( double  aRed,
double  aGreen,
double  aBlue,
double  aAlpha 
)
inline
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 121 of file color4d.h.

121  :
122  r( aRed ),
123  g( aGreen ),
124  b( aBlue ),
125  a( aAlpha )
126  {
127  wxASSERT( r >= 0.0 && r <= 1.0 );
128  wxASSERT( g >= 0.0 && g <= 1.0 );
129  wxASSERT( b >= 0.0 && b <= 1.0 );
130  wxASSERT( a >= 0.0 && a <= 1.0 );
131  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

References a, b, g, and r.

◆ COLOR4D() [3/3]

COLOR4D::COLOR4D ( EDA_COLOR_T  aColor)
Parameters
aColoris one of KiCad's palette colors.
See also
EDA_COLOR_T

Definition at line 83 of file color4d.cpp.

84 {
85  if( aColor <= UNSPECIFIED_COLOR || aColor >= NBCOLORS )
86  {
87  *this = COLOR4D::UNSPECIFIED;
88  return;
89  }
90 
91  int candidate = 0;
92 
93  for( ; candidate < NBCOLORS; ++candidate )
94  {
95  if( colorRefs()[candidate].m_Numcolor == aColor )
96  break;
97  }
98 
99  if( candidate >= NBCOLORS )
100  {
101  *this = COLOR4D::UNSPECIFIED;
102  return;
103  }
104 
105  r = colorRefs()[candidate].m_Red / 255.0;
106  g = colorRefs()[candidate].m_Green / 255.0;
107  b = colorRefs()[candidate].m_Blue / 255.0;
108  a = 1.0;
109 }
unsigned char m_Red
Definition: color4d.h:88
unsigned char m_Green
Definition: color4d.h:87
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
Number of colors.
Definition: color4d.h:79
double a
Alpha component.
Definition: color4d.h:387
unsigned char m_Blue
Definition: color4d.h:86
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition: color4d.cpp:39
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:390
double r
Red component.
Definition: color4d.h:384

References a, b, colorRefs(), g, StructColors::m_Blue, StructColors::m_Green, StructColors::m_Red, NBCOLORS, r, and UNSPECIFIED.

Member Function Documentation

◆ Brighten()

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

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 225 of file color4d.h.

226  {
227  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
228 
229  r = r * ( 1.0 - aFactor ) + aFactor;
230  g = g * ( 1.0 - aFactor ) + aFactor;
231  b = b * ( 1.0 - aFactor ) + aFactor;
232 
233  return *this;
234  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

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

◆ Brightened()

COLOR4D KIGFX::COLOR4D::Brightened ( double  aFactor) const
inline

Return 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 278 of file color4d.h.

279  {
280  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
281 
282  return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
283  b * ( 1.0 - aFactor ) + aFactor, a );
284  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

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

Referenced by KIGFX::SCH_PAINTER::drawDanglingSymbol(), KIGFX::SCH_PAINTER::drawPinDanglingSymbol(), KIGFX::DS_RENDER_SETTINGS::DS_RENDER_SETTINGS(), KIGFX::GERBVIEW_RENDER_SETTINGS::LoadColors(), COLOR_SWATCH::MakeBitmap(), KIGFX::RENDER_SETTINGS::update(), and EDIT_POINTS::ViewDraw().

◆ Darken()

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

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 242 of file color4d.h.

243  {
244  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
245 
246  r = r * ( 1.0 - aFactor );
247  g = g * ( 1.0 - aFactor );
248  b = b * ( 1.0 - aFactor );
249 
250  return *this;
251  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

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

◆ Darkened()

COLOR4D KIGFX::COLOR4D::Darkened ( double  aFactor) const
inline

Return 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 292 of file color4d.h.

293  {
294  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
295 
296  return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
297  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

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

Referenced by KIGFX::GERBVIEW_RENDER_SETTINGS::LoadColors(), COLOR_SWATCH::MakeBitmap(), KIGFX::RENDER_SETTINGS::update(), and EDIT_POINTS::ViewDraw().

◆ Distance()

int COLOR4D::Distance ( const COLOR4D other) const

Returns the distance (in RGB space) between two colors.

Definition at line 513 of file color4d.cpp.

514 {
515  return ( r - other.r ) * ( r - other.r )
516  + ( g - other.g ) * ( g - other.g )
517  + ( b - other.b ) * ( b - other.b );
518 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by EDIT_POINTS::ViewDraw().

◆ FindNearestLegacyColor()

EDA_COLOR_T COLOR4D::FindNearestLegacyColor ( int  aR,
int  aG,
int  aB 
)
static

Returns a legacy color ID that is closest to the given 8-bit RGB values.

Definition at line 521 of file color4d.cpp.

522 {
523  EDA_COLOR_T candidate = EDA_COLOR_T::BLACK;
524 
525  /* Find the 'nearest' color in the palette. This is fun. There is
526  a gazilion of metrics for the color space and no one of the
527  useful one is in the RGB color space. Who cares, this is a CAD,
528  not a photosomething...
529 
530  I hereby declare that the distance is the sum of the square of the
531  component difference. Think about the RGB color cube. Now get the
532  euclidean distance, but without the square root... for ordering
533  purposes it's the same, obviously. Also each component can't be
534  less of the target one, since I found this currently work better...
535  */
536  int nearest_distance = 255 * 255 * 3 + 1; // Can't beat this
537 
538  for( EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS;
539  trying = static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
540  {
541  const StructColors &c = colorRefs()[trying];
542  int distance = ( aR - c.m_Red ) * ( aR - c.m_Red ) +
543  ( aG - c.m_Green ) * ( aG - c.m_Green ) +
544  ( aB - c.m_Blue ) * ( aB - c.m_Blue );
545 
546  if( distance < nearest_distance && c.m_Red >= aR &&
547  c.m_Green >= aG && c.m_Blue >= aB )
548  {
549  nearest_distance = distance;
550  candidate = trying;
551  }
552  }
553 
554  return candidate;
555 }
unsigned char m_Red
Definition: color4d.h:88
unsigned char m_Green
Definition: color4d.h:87
Definition: color4d.h:44
Number of colors.
Definition: color4d.h:79
unsigned char m_Blue
Definition: color4d.h:86
const StructColors * colorRefs()
Global list of legacy color names, still used all over the place for constructing COLOR4D's.
Definition: color4d.cpp:39
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
EDA_COLOR_T
Legacy color enumeration.
Definition: color4d.h:41

References BLACK, colorRefs(), distance(), StructColors::m_Blue, StructColors::m_Green, StructColors::m_Red, and NBCOLORS.

◆ FromCSSRGBA()

COLOR4D & 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.

Suitable for taking the values directly from the "CSS syntax" from ToWxString.

Returns
this color.

Definition at line 558 of file color4d.cpp.

559 {
560  r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
561  g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
562  b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
563  a = std::max( 0.0, std::min( 1.0, aAlpha ) );
564 
565  return *this;
566 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

References a, b, g, and r.

Referenced by GetColorFromInt().

◆ FromHSL()

void COLOR4D::FromHSL ( double  aInHue,
double  aInSaturation,
double  aInLightness 
)

Change currently used color to the one given by hue, saturation and lightness parameters.

Parameters
aInHueis hue component, in degrees (0.0 - 360.0).
aInSaturationis saturation component (0.0 - 1.0).
aInLightnessis lightness component (0.0 - 1.0).

Definition at line 323 of file color4d.cpp.

324 {
325  const auto P = ( 1.0 - std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
326  const auto scaled_hue = aInHue / 60.0;
327  const auto Q = P * ( 1.0 - std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
328 
329  r = g = b = aInLightness - P / 2.0;
330 
331  if (scaled_hue < 1.0)
332  {
333  r += P;
334  g += Q;
335  }
336  else if (scaled_hue < 2.0)
337  {
338  r += Q;
339  g += P;
340  }
341  else if (scaled_hue < 3.0)
342  {
343  g += P;
344  b += Q;
345  }
346  else if (scaled_hue < 4.0)
347  {
348  g += Q;
349  b += P;
350  }
351  else if (scaled_hue < 5.0)
352  {
353  r += Q;
354  b += P;
355  }
356  else
357  {
358  r += P;
359  b += Q;
360  }
361 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by BOOST_AUTO_TEST_CASE().

◆ FromHSV()

void COLOR4D::FromHSV ( double  aInH,
double  aInS,
double  aInV 
)

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

Parameters
aInHis hue component, in degrees.
aInSis saturation component.
aInVis value component.

Definition at line 418 of file color4d.cpp.

419 {
420  if( aInS <= 0.0 )
421  {
422  r = aInV;
423  g = aInV;
424  b = aInV;
425  return;
426  }
427 
428  double hh = aInH;
429 
430  while( hh >= 360.0 )
431  hh -= 360.0;
432 
433  /* Hue in degrees (0...360) is coded according to this table
434  * 0 or 360 : red
435  * 60 : yellow
436  * 120 : green
437  * 180 : cyan
438  * 240 : blue
439  * 300 : magenta
440  */
441  hh /= 60.0;
442 
443  int i = (int) hh;
444  double ff = hh - i;
445 
446  double p = aInV * ( 1.0 - aInS );
447  double q = aInV * ( 1.0 - ( aInS * ff ) );
448  double t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
449 
450  switch( i )
451  {
452  case 0:
453  r = aInV;
454  g = t;
455  b = p;
456  break;
457 
458  case 1:
459  r = q;
460  g = aInV;
461  b = p;
462  break;
463 
464  case 2:
465  r = p;
466  g = aInV;
467  b = t;
468  break;
469 
470  case 3:
471  r = p;
472  g = q;
473  b = aInV;
474  break;
475 
476  case 4:
477  r = t;
478  g = p;
479  b = aInV;
480  break;
481 
482  case 5:
483  default:
484  r = aInV;
485  g = p;
486  b = q;
487  break;
488  }
489 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by BOOST_AUTO_TEST_CASE(), DIALOG_COLOR_PICKER::OnChangeBrightness(), DIALOG_COLOR_PICKER::OnChangeEditHue(), DIALOG_COLOR_PICKER::OnChangeEditSat(), Saturate(), and DIALOG_COLOR_PICKER::setHSvaluesFromCursor().

◆ GetBrightness()

double KIGFX::COLOR4D::GetBrightness ( ) const
inline

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

Returns
The brightness value.

Definition at line 343 of file color4d.h.

344  {
345  // Weighted W3C formula
346  return r * 0.299 + g * 0.587 + b * 0.117;
347  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by RC_TREE_MODEL::GetAttr(), KIGFX::PREVIEW::GetShadowColor(), GRHaloText(), KIGFX::DS_RENDER_SETTINGS::IsBackgroundDark(), KIGFX::SCH_RENDER_SETTINGS::IsBackgroundDark(), KIGFX::PCB_RENDER_SETTINGS::IsBackgroundDark(), COLOR_SWATCH::MakeBitmap(), and EDIT_POINTS::ViewDraw().

◆ Invert()

COLOR4D& KIGFX::COLOR4D::Invert ( )
inline

Makes the color inverted, alpha remains the same.

Returns
COLOR4D& Inverted color.

Definition at line 258 of file color4d.h.

259  {
260  r = ( 1.0 - r );
261  g = ( 1.0 - g );
262  b = ( 1.0 - b );
263 
264  return *this;
265  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by EDIT_POINTS::ViewDraw().

◆ Inverted()

COLOR4D KIGFX::COLOR4D::Inverted ( ) const
inline

Returns an inverted color, alpha remains the same.

Returns
COLOR4D& Inverted color.

Definition at line 333 of file color4d.h.

334  {
335  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
336  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

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

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

◆ Mix()

COLOR4D KIGFX::COLOR4D::Mix ( const COLOR4D aColor,
double  aFactor 
) const
inline

Return a color that is mixed with the input by a factor.

Parameters
aFactorSpecifies how much of the original color to keep (valid values: 0.0 .. 1.0).
Returns
COLOR4D Mixed color.

Definition at line 305 of file color4d.h.

306  {
307  wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
308 
309  return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
310  aColor.g * ( 1.0 - aFactor ) + g * aFactor,
311  aColor.b * ( 1.0 - aFactor ) + b * aFactor,
312  a );
313  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double a
Alpha component.
Definition: color4d.h:387
double r
Red component.
Definition: color4d.h:384

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

Referenced by BOARD_ADAPTER::InitSettings(), and KIGFX::RENDER_SETTINGS::update().

◆ Saturate()

COLOR4D & COLOR4D::Saturate ( double  aFactor)

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

Definition at line 492 of file color4d.cpp.

493 {
494  // One can saturate a color only when r, v, b are not equal
495  if( r == g && r == b )
496  return *this;
497 
498  double h, s, v;
499 
500  ToHSV( h, s, v, true );
501  FromHSV( h, aFactor, 1.0 );
502 
503  return *this;
504 }
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
Definition: color4d.cpp:364
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
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:418
double r
Red component.
Definition: color4d.h:384

References b, FromHSV(), g, r, and ToHSV().

◆ ToHSL()

void COLOR4D::ToHSL ( double &  aOutHue,
double &  aOutSaturation,
double &  aOutValue 
) const

Converts current color (stored in RGB) to HSL format.

Parameters
aOutHueis the conversion result for hue component, in degrees 0 ... 360.0.
aOutSaturationis the conversion result for saturation component (0 ... 1.0).
aOutLightnessis conversion result for value component (0 ... 1.0).
Note
saturation is set to 0.0 for black color if r = g = b,

Definition at line 292 of file color4d.cpp.

293 {
294  auto min = std::min( r, std::min( g, b ) );
295  auto max = std::max( r, std::max( g, b ) );
296  auto diff = max - min;
297 
298  aOutLightness = ( max + min ) / 2.0;
299 
300  if( aOutLightness >= 1.0 )
301  aOutSaturation = 0.0;
302  else
303  aOutSaturation = diff / ( 1.0 - std::abs( 2.0 * aOutLightness - 1.0 ) );
304 
305  double hue;
306 
307  if( diff <= 0.0 )
308  hue = 0.0;
309  else if( max == r )
310  hue = ( g - b ) / diff;
311  else if( max == g )
312  hue = ( b - r ) / diff + 2.0;
313  else
314  hue = ( r - g ) / diff + 4.0;
315 
316  aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
317 
318  while( aOutHue < 0.0 )
319  aOutHue += 360.0;
320 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

References b, g, and r.

Referenced by PNS_TUNE_STATUS_POPUP::UpdateStatus().

◆ ToHSV()

void COLOR4D::ToHSV ( double &  aOutHue,
double &  aOutSaturation,
double &  aOutValue,
bool  aAlwaysDefineHue = false 
) const

Convert current color (stored in RGB) to HSV format.

Parameters
aOutHueis the conversion result for hue component, in degrees 0 ... 360.0.
aOutSaturationis the conversion result for saturation component (0 ... 1.0).
aOutValueis conversion result for value component (0 ... 1.0).
aAlwaysDefineHuecontrols the way hue is defined when r = v = b
Note
saturation is set to 0.0 for black color (r = v = b = 0), and if r = v = b, hue is set to 0.0 if aAlwaysDefineHue = true, and set to NAN if aAlwaysDefineHue = false. this option is useful to convert a 4D color to a legacy color, because Red has hue = 0, therefore aAlwaysDefineHue = false makes difference between Red and Gray colors.

Definition at line 364 of file color4d.cpp.

366 {
367  double min, max, delta;
368 
369  min = r < g ? r : g;
370  min = min < b ? min : b;
371 
372  max = r > g ? r : g;
373  max = max > b ? max : b;
374 
375  aOutValue = max; // value
376  delta = max - min;
377 
378  if( max > 0.0 )
379  {
380  aOutSaturation = ( delta / max );
381  }
382  else // for black color (r = g = b = 0 ) saturation is set to 0.
383  {
384  aOutSaturation = 0.0;
385  aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
386  return;
387  }
388 
389  /* Hue in degrees (0...360) is coded according to this table
390  * 0 or 360 : red
391  * 60 : yellow
392  * 120 : green
393  * 180 : cyan
394  * 240 : blue
395  * 300 : magenta
396  */
397  if( delta != 0.0 )
398  {
399  if( r >= max )
400  aOutHue = ( g - b ) / delta; // between yellow & magenta
401  else if( g >= max )
402  aOutHue = 2.0 + ( b - r ) / delta; // between cyan & yellow
403  else
404  aOutHue = 4.0 + ( r - g ) / delta; // between magenta & cyan
405 
406  aOutHue *= 60.0; // degrees
407 
408  if( aOutHue < 0.0 )
409  aOutHue += 360.0;
410  }
411  else // delta = 0 means r = g = b. hue is set to 0.0
412  {
413  aOutHue = aAlwaysDefineHue ? 0.0 : NAN;
414  }
415 }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
constexpr int delta
double r
Red component.
Definition: color4d.h:384

References b, delta, g, and r.

Referenced by DIALOG_COLOR_PICKER::buttColorClick(), DIALOG_COLOR_PICKER::DIALOG_COLOR_PICKER(), DIALOG_COLOR_PICKER::OnColorValueText(), DIALOG_COLOR_PICKER::OnResetButton(), DIALOG_COLOR_PICKER::onRGBMouseDrag(), Saturate(), and DIALOG_COLOR_PICKER::SetEditVals().

◆ WithAlpha()

COLOR4D KIGFX::COLOR4D::WithAlpha ( double  aAlpha) const
inline

Return a color with the same color, but the given alpha.

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

Definition at line 321 of file color4d.h.

322  {
323  wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
324 
325  return COLOR4D( r, g, b, aAlpha );
326  }
double g
Green component.
Definition: color4d.h:385
double b
Blue component.
Definition: color4d.h:386
double r
Red component.
Definition: color4d.h:384

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

Referenced by PNS_LOG_VIEWER_OVERLAY::Arc(), deemphasise(), LAYER_SELECTOR::DrawColorSwatch(), BOARD_PRINTOUT::DrawPage(), FAB_LAYER_COLOR::GetColor(), KIGFX::PCB_RENDER_SETTINGS::GetColor(), COLOR_SWATCH::MakeBitmap(), PCB_GRID_HELPER::PCB_GRID_HELPER(), PCB_EDIT_FRAME::PrepareLayerIndicator(), and EDIT_POINTS::ViewDraw().

Member Data Documentation

◆ a

double KIGFX::COLOR4D::a

Alpha component.

Definition at line 387 of file color4d.h.

Referenced by KIGFX::OPENGL_GAL::BitmapText(), KIGFX::CAIRO_GAL_BASE::blitCursor(), KIGFX::OPENGL_GAL::blitCursor(), Brightened(), DIALOG_COLOR_PICKER::buttColorClick(), KIGFX::CAIRO_GAL_BASE::ChangeGroupColor(), KIGFX::VERTEX_MANAGER::ChangeItemColor(), KIGFX::VERTEX_MANAGER::Color(), COLOR4D(), CUSTOM_COLOR_ITEM::CUSTOM_COLOR_ITEM(), Darkened(), DIALOG_COLOR_PICKER::DIALOG_COLOR_PICKER(), KIGFX::OPENGL_GAL::DrawArc(), KIGFX::CAIRO_GAL_BASE::DrawArcSegment(), KIGFX::OPENGL_GAL::DrawArcSegment(), KIGFX::CAIRO_GAL_BASE::drawAxes(), KIGFX::OPENGL_GAL::drawBitmapOverbar(), KIGFX::OPENGL_GAL::DrawCircle(), KIGFX::OPENGL_GAL::DrawGrid(), KIGFX::CAIRO_GAL_BASE::drawGridCross(), KIGFX::CAIRO_GAL_BASE::drawGridLine(), KIGFX::CAIRO_GAL_BASE::drawGridPoint(), KIGFX::CAIRO_GAL_BASE::DrawGroup(), KIGFX::OPENGL_GAL::DrawLine(), KIGFX::OPENGL_GAL::drawPolygon(), KIGFX::OPENGL_GAL::drawPolyline(), KIGFX::OPENGL_GAL::DrawRectangle(), KIGFX::CAIRO_GAL_BASE::DrawSegment(), KIGFX::OPENGL_GAL::DrawSegment(), KIGFX::OPENGL_GAL::drawSemiCircle(), KIGFX::OPENGL_GAL::drawTriangulatedPolyset(), KIGFX::CAIRO_GAL_BASE::flushPath(), formatStroke(), FromCSSRGBA(), BOARD_ADAPTER::GetColor(), BOARD_ADAPTER::InitSettings(), Inverted(), KI_TEST::IsColorNear(), KI_TEST::IsColorNearHex(), PCB_CONTROL::LayerAlphaDec(), PCB_CONTROL::LayerAlphaInc(), KIGFX::PCB_RENDER_SETTINGS::LoadColors(), PANEL_PREVIEW_3D_MODEL::loadSettings(), Mix(), DIALOG_COLOR_PICKER::OnChangeAlpha(), APPEARANCE_CONTROLS::OnLayerAlphaChanged(), DIALOG_COLOR_PICKER::OnResetButton(), KIGFX::operator<(), KIGFX::operator==(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveSheet(), DIALOG_COLOR_PICKER::SetEditVals(), KIGFX::CAIRO_GAL_BASE::SetFillColor(), SCH_LINE::SetLineColor(), KIGFX::CAIRO_GAL_BASE::SetStrokeColor(), KIGFX::CAIRO_GAL_BASE::storePath(), and ROUTER_PREVIEW_ITEM::Update().

◆ b

double KIGFX::COLOR4D::b

Blue component.

Definition at line 386 of file color4d.h.

Referenced by KIGFX::OPENGL_GAL::BitmapText(), KIGFX::CAIRO_GAL_BASE::blitCursor(), KIGFX::OPENGL_GAL::blitCursor(), Brighten(), Brightened(), DIALOG_COLOR_PICKER::buttColorClick(), KIGFX::CAIRO_GAL_BASE::ChangeGroupColor(), KIGFX::VERTEX_MANAGER::ChangeItemColor(), KIGFX::OPENGL_COMPOSITOR::ClearBuffer(), KIGFX::CAIRO_GAL_BASE::ClearScreen(), KIGFX::VERTEX_MANAGER::Color(), COLOR4D(), CUSTOM_COLOR_ITEM::CUSTOM_COLOR_ITEM(), Darken(), Darkened(), Distance(), KIGFX::OPENGL_GAL::DrawArc(), KIGFX::CAIRO_GAL_BASE::DrawArcSegment(), KIGFX::OPENGL_GAL::DrawArcSegment(), KIGFX::CAIRO_GAL_BASE::drawAxes(), KIGFX::OPENGL_GAL::drawBitmapOverbar(), KIGFX::OPENGL_GAL::DrawCircle(), KIGFX::OPENGL_GAL::DrawGrid(), KIGFX::CAIRO_GAL_BASE::drawGridCross(), KIGFX::CAIRO_GAL_BASE::drawGridLine(), KIGFX::CAIRO_GAL_BASE::drawGridPoint(), KIGFX::CAIRO_GAL_BASE::DrawGroup(), KIGFX::OPENGL_GAL::DrawLine(), KIGFX::OPENGL_GAL::drawPolygon(), KIGFX::OPENGL_GAL::drawPolyline(), KIGFX::OPENGL_GAL::DrawRectangle(), DIALOG_COLOR_PICKER::drawRGBPalette(), KIGFX::CAIRO_GAL_BASE::DrawSegment(), KIGFX::OPENGL_GAL::DrawSegment(), KIGFX::OPENGL_GAL::drawSemiCircle(), KIGFX::OPENGL_GAL::drawTriangulatedPolyset(), KIGFX::CAIRO_GAL_BASE::flushPath(), formatStroke(), FromCSSRGBA(), FromHSL(), FromHSV(), GetBrightness(), BOARD_ADAPTER::GetColor(), getDXFColorName(), TRANSLINE::Init(), Invert(), Inverted(), KI_TEST::IsColorNear(), KI_TEST::IsColorNearHex(), PANEL_PREVIEW_3D_MODEL::loadSettings(), Mix(), DIALOG_COLOR_PICKER::OnChangeEditBlue(), DIALOG_COLOR_PICKER::OnResetButton(), DIALOG_COLOR_PICKER::onRGBMouseDrag(), KIGFX::operator<(), KIGFX::operator==(), Saturate(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveSheet(), DIALOG_COLOR_PICKER::SetEditVals(), KIGFX::CAIRO_GAL_BASE::SetFillColor(), PANEL_TRANSLINE::SetPrmBgColor(), KIGFX::CAIRO_GAL_BASE::SetStrokeColor(), KIGFX::CAIRO_GAL_BASE::storePath(), ToHSL(), ToHSV(), PANEL_3D_OPENGL_OPTIONS::TransferDataFromWindow(), KIGFX::RENDER_SETTINGS::update(), and WithAlpha().

◆ BLACK

◆ CLEAR

const COLOR4D COLOR4D::CLEAR
static

Definition at line 395 of file color4d.h.

Referenced by KIGFX::PCB_PAINTER::draw(), and KIGFX::PCB_RENDER_SETTINGS::GetColor().

◆ g

double KIGFX::COLOR4D::g

Green component.

Definition at line 385 of file color4d.h.

Referenced by KIGFX::OPENGL_GAL::BitmapText(), KIGFX::CAIRO_GAL_BASE::blitCursor(), KIGFX::OPENGL_GAL::blitCursor(), Brighten(), Brightened(), DIALOG_COLOR_PICKER::buttColorClick(), KIGFX::CAIRO_GAL_BASE::ChangeGroupColor(), KIGFX::VERTEX_MANAGER::ChangeItemColor(), KIGFX::OPENGL_COMPOSITOR::ClearBuffer(), KIGFX::CAIRO_GAL_BASE::ClearScreen(), KIGFX::VERTEX_MANAGER::Color(), COLOR4D(), CUSTOM_COLOR_ITEM::CUSTOM_COLOR_ITEM(), Darken(), Darkened(), Distance(), KIGFX::OPENGL_GAL::DrawArc(), KIGFX::CAIRO_GAL_BASE::DrawArcSegment(), KIGFX::OPENGL_GAL::DrawArcSegment(), KIGFX::CAIRO_GAL_BASE::drawAxes(), KIGFX::OPENGL_GAL::drawBitmapOverbar(), KIGFX::OPENGL_GAL::DrawCircle(), KIGFX::OPENGL_GAL::DrawGrid(), KIGFX::CAIRO_GAL_BASE::drawGridCross(), KIGFX::CAIRO_GAL_BASE::drawGridLine(), KIGFX::CAIRO_GAL_BASE::drawGridPoint(), KIGFX::CAIRO_GAL_BASE::DrawGroup(), KIGFX::OPENGL_GAL::DrawLine(), KIGFX::OPENGL_GAL::drawPolygon(), KIGFX::OPENGL_GAL::drawPolyline(), KIGFX::OPENGL_GAL::DrawRectangle(), DIALOG_COLOR_PICKER::drawRGBPalette(), KIGFX::CAIRO_GAL_BASE::DrawSegment(), KIGFX::OPENGL_GAL::DrawSegment(), KIGFX::OPENGL_GAL::drawSemiCircle(), KIGFX::OPENGL_GAL::drawTriangulatedPolyset(), KIGFX::CAIRO_GAL_BASE::flushPath(), formatStroke(), FromCSSRGBA(), FromHSL(), FromHSV(), GetBrightness(), BOARD_ADAPTER::GetColor(), getDXFColorName(), TRANSLINE::Init(), Invert(), Inverted(), KI_TEST::IsColorNear(), KI_TEST::IsColorNearHex(), PANEL_PREVIEW_3D_MODEL::loadSettings(), Mix(), DIALOG_COLOR_PICKER::OnChangeEditGreen(), DIALOG_COLOR_PICKER::OnResetButton(), DIALOG_COLOR_PICKER::onRGBMouseDrag(), KIGFX::operator<(), KIGFX::operator==(), Saturate(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveSheet(), DIALOG_COLOR_PICKER::SetEditVals(), KIGFX::CAIRO_GAL_BASE::SetFillColor(), PANEL_TRANSLINE::SetPrmBgColor(), KIGFX::CAIRO_GAL_BASE::SetStrokeColor(), KIGFX::CAIRO_GAL_BASE::storePath(), ToHSL(), ToHSV(), PANEL_3D_OPENGL_OPTIONS::TransferDataFromWindow(), and WithAlpha().

◆ r

double KIGFX::COLOR4D::r

Red component.

Definition at line 384 of file color4d.h.

Referenced by KIGFX::OPENGL_GAL::BitmapText(), KIGFX::CAIRO_GAL_BASE::blitCursor(), KIGFX::OPENGL_GAL::blitCursor(), Brighten(), Brightened(), DIALOG_COLOR_PICKER::buttColorClick(), KIGFX::CAIRO_GAL_BASE::ChangeGroupColor(), KIGFX::VERTEX_MANAGER::ChangeItemColor(), KIGFX::OPENGL_COMPOSITOR::ClearBuffer(), KIGFX::CAIRO_GAL_BASE::ClearScreen(), KIGFX::VERTEX_MANAGER::Color(), COLOR4D(), CUSTOM_COLOR_ITEM::CUSTOM_COLOR_ITEM(), Darken(), Darkened(), Distance(), KIGFX::OPENGL_GAL::DrawArc(), KIGFX::CAIRO_GAL_BASE::DrawArcSegment(), KIGFX::OPENGL_GAL::DrawArcSegment(), KIGFX::CAIRO_GAL_BASE::drawAxes(), KIGFX::OPENGL_GAL::drawBitmapOverbar(), KIGFX::OPENGL_GAL::DrawCircle(), KIGFX::OPENGL_GAL::DrawGrid(), KIGFX::CAIRO_GAL_BASE::drawGridCross(), KIGFX::CAIRO_GAL_BASE::drawGridLine(), KIGFX::CAIRO_GAL_BASE::drawGridPoint(), KIGFX::CAIRO_GAL_BASE::DrawGroup(), KIGFX::OPENGL_GAL::DrawLine(), KIGFX::OPENGL_GAL::drawPolygon(), KIGFX::OPENGL_GAL::drawPolyline(), KIGFX::OPENGL_GAL::DrawRectangle(), DIALOG_COLOR_PICKER::drawRGBPalette(), KIGFX::CAIRO_GAL_BASE::DrawSegment(), KIGFX::OPENGL_GAL::DrawSegment(), KIGFX::OPENGL_GAL::drawSemiCircle(), KIGFX::OPENGL_GAL::drawTriangulatedPolyset(), KIGFX::CAIRO_GAL_BASE::flushPath(), formatStroke(), FromCSSRGBA(), FromHSL(), FromHSV(), GetBrightness(), BOARD_ADAPTER::GetColor(), getDXFColorName(), TRANSLINE::Init(), Invert(), Inverted(), KI_TEST::IsColorNear(), KI_TEST::IsColorNearHex(), PANEL_PREVIEW_3D_MODEL::loadSettings(), Mix(), DIALOG_COLOR_PICKER::OnChangeEditRed(), DIALOG_COLOR_PICKER::OnResetButton(), DIALOG_COLOR_PICKER::onRGBMouseDrag(), KIGFX::operator<(), KIGFX::operator==(), Saturate(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveSheet(), DIALOG_COLOR_PICKER::SetEditVals(), KIGFX::CAIRO_GAL_BASE::SetFillColor(), PANEL_TRANSLINE::SetPrmBgColor(), KIGFX::CAIRO_GAL_BASE::SetStrokeColor(), KIGFX::CAIRO_GAL_BASE::storePath(), ToHSL(), ToHSV(), PANEL_3D_OPENGL_OPTIONS::TransferDataFromWindow(), and WithAlpha().

◆ UNSPECIFIED

const COLOR4D COLOR4D::UNSPECIFIED
static

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

Definition at line 390 of file color4d.h.

Referenced by COLOR4D(), KIGFX::PCB_RENDER_SETTINGS::GetColor(), KIGFX::SCH_PAINTER::getRenderColor(), and KIGFX::GERBVIEW_RENDER_SETTINGS::LoadColors().

◆ WHITE

const COLOR4D COLOR4D::WHITE
static

Definition at line 393 of file color4d.h.

Referenced by PNS_TEST_DEBUG_DECORATOR::DEBUG_ENT::DEBUG_ENT().


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