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 ()
 
constexpr 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 ), g( 0 ), b( 0 ), a( 1.0 )
109  {
110  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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

◆ COLOR4D() [2/3]

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

118  :
119  r( aRed ), g( aGreen ), b( aBlue ), a( aAlpha )
120  {
121  assert( r >= 0.0 && r <= 1.0 );
122  assert( g >= 0.0 && g <= 1.0 );
123  assert( b >= 0.0 && b <= 1.0 );
124  assert( a >= 0.0 && a <= 1.0 );
125  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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:372
double b
Blue component.
Definition: color4d.h:373
Number of colors.
Definition: color4d.h:79
double a
Alpha component.
Definition: color4d.h:374
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:377
double r
Red component.
Definition: color4d.h:371

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

213  {
214  assert( 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  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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

266  {
267  assert( 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  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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

Referenced by 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 229 of file color4d.h.

230  {
231  assert( 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  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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

280  {
281  assert( aFactor >= 0.0 && aFactor <= 1.0 );
282 
283  return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
284  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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 506 of file color4d.cpp.

507 {
508  return ( r - other.r ) * ( r - other.r )
509  + ( g - other.g ) * ( g - other.g )
510  + ( b - other.b ) * ( b - other.b );
511 }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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 514 of file color4d.cpp.

515 {
516  EDA_COLOR_T candidate = EDA_COLOR_T::BLACK;
517 
518  /* Find the 'nearest' color in the palette. This is fun. There is
519  a gazilion of metrics for the color space and no one of the
520  useful one is in the RGB color space. Who cares, this is a CAD,
521  not a photosomething...
522 
523  I hereby declare that the distance is the sum of the square of the
524  component difference. Think about the RGB color cube. Now get the
525  euclidean distance, but without the square root... for ordering
526  purposes it's the same, obviously. Also each component can't be
527  less of the target one, since I found this currently work better...
528  */
529  int nearest_distance = 255 * 255 * 3 + 1; // Can't beat this
530 
531  for( EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS;
532  trying = static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
533  {
534  const StructColors &c = colorRefs()[trying];
535  int distance = ( aR - c.m_Red ) * ( aR - c.m_Red ) +
536  ( aG - c.m_Green ) * ( aG - c.m_Green ) +
537  ( aB - c.m_Blue ) * ( aB - c.m_Blue );
538 
539  if( distance < nearest_distance && c.m_Red >= aR &&
540  c.m_Green >= aG && c.m_Blue >= aB )
541  {
542  nearest_distance = distance;
543  candidate = trying;
544  }
545  }
546 
547  return candidate;
548 }
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 551 of file color4d.cpp.

552 {
553  r = std::max( 0, std::min( 255, aRed ) ) / 255.0;
554  g = std::max( 0, std::min( 255, aGreen ) ) / 255.0;
555  b = std::max( 0, std::min( 255, aBlue ) ) / 255.0;
556  a = std::max( 0.0, std::min( 1.0, aAlpha ) );
557 
558  return *this;
559 }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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 316 of file color4d.cpp.

317 {
318  const auto P = ( 1.0 - std::abs( 2.0 * aInLightness - 1.0 ) ) * aInSaturation;
319  const auto scaled_hue = aInHue / 60.0;
320  const auto Q = P * ( 1.0 - std::abs( std::fmod( scaled_hue, 2.0 ) - 1.0 ) );
321 
322  r = g = b = aInLightness - P / 2.0;
323 
324  if (scaled_hue < 1.0)
325  {
326  r += P;
327  g += Q;
328  }
329  else if (scaled_hue < 2.0)
330  {
331  r += Q;
332  g += P;
333  }
334  else if (scaled_hue < 3.0)
335  {
336  g += P;
337  b += Q;
338  }
339  else if (scaled_hue < 4.0)
340  {
341  g += Q;
342  b += P;
343  }
344  else if (scaled_hue < 5.0)
345  {
346  r += Q;
347  b += P;
348  }
349  else
350  {
351  r += P;
352  b += Q;
353  }
354 }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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 411 of file color4d.cpp.

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

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

331  {
332  // Weighted W3C formula
333  return r * 0.299 + g * 0.587 + b * 0.117;
334  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

References b, g, and r.

Referenced by 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 245 of file color4d.h.

246  {
247  r = ( 1.0 - r );
248  g = ( 1.0 - g );
249  b = ( 1.0 - b );
250 
251  return *this;
252  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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

321  {
322  return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
323  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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

293  {
294  assert( 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  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:374
double r
Red component.
Definition: color4d.h:371

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 485 of file color4d.cpp.

486 {
487  // One can saturate a color only when r, v, b are not equal
488  if( r == g && r == b )
489  return *this;
490 
491  double h, s, v;
492 
493  ToHSV( h, s, v, true );
494  FromHSV( h, aFactor, 1.0 );
495 
496  return *this;
497 }
void ToHSV(double &aOutHue, double &aOutSaturation, double &aOutValue, bool aAlwaysDefineHue=false) const
Convert current color (stored in RGB) to HSV format.
Definition: color4d.cpp:357
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
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:411
double r
Red component.
Definition: color4d.h:371

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 285 of file color4d.cpp.

286 {
287  auto min = std::min( r, std::min( g, b ) );
288  auto max = std::max( r, std::max( g, b ) );
289  auto diff = max - min;
290 
291  aOutLightness = ( max + min ) / 2.0;
292 
293  if( aOutLightness >= 1.0 )
294  aOutSaturation = 0.0;
295  else
296  aOutSaturation = diff / ( 1.0 - std::abs( 2.0 * aOutLightness - 1.0 ) );
297 
298  double hue;
299 
300  if( diff <= 0.0 )
301  hue = 0.0;
302  else if( max == r )
303  hue = ( g - b ) / diff;
304  else if( max == g )
305  hue = ( b - r ) / diff + 2.0;
306  else
307  hue = ( r - g ) / diff + 4.0;
308 
309  aOutHue = hue > 0.0 ? hue * 60.0 : hue * 60.0 + 360.0;
310 
311  while( aOutHue < 0.0 )
312  aOutHue += 360.0;
313 }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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 357 of file color4d.cpp.

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

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

309  {
310  assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
311 
312  return COLOR4D( r, g, b, aAlpha );
313  }
double g
Green component.
Definition: color4d.h:372
double b
Blue component.
Definition: color4d.h:373
double r
Red component.
Definition: color4d.h:371

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 374 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 373 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(), PCB_CALCULATOR_FRAME::SetPrmBgColor(), KIGFX::CAIRO_GAL_BASE::SetStrokeColor(), KIGFX::CAIRO_GAL_BASE::storePath(), ToHSL(), ToHSV(), PANEL_3D_OPENGL_OPTIONS::TransferDataFromWindow(), and WithAlpha().

◆ BLACK

◆ CLEAR

const COLOR4D COLOR4D::CLEAR
static

Definition at line 382 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 372 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(), PCB_CALCULATOR_FRAME::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 371 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(), PCB_CALCULATOR_FRAME::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 377 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 380 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: