KiCad PCB EDA Suite
colors.cpp File Reference
#include <colors.h>
#include <i18n_utility.h>

Go to the source code of this file.

Functions

EDA_COLOR_T ColorByName (const wxString &aName)
 Find a color by name. More...
 
bool ColorIsLight (EDA_COLOR_T aColor)
 Check if a color is light i.e. More...
 
EDA_COLOR_T ColorFindNearest (const wxColour &aColor)
 Find the nearest color match. More...
 
EDA_COLOR_T ColorFindNearest (int aR, int aG, int aB)
 Find the nearest color match. More...
 
EDA_COLOR_T ColorMix (EDA_COLOR_T aColor1, EDA_COLOR_T aColor2)
 Mix two colors in some way (hopefully like a logical OR) More...
 

Variables

const StructColors g_ColorRefs [NBCOLORS]
 The predefined colors used in KiCad. More...
 

Function Documentation

EDA_COLOR_T ColorByName ( const wxString &  aName)

Find a color by name.

Definition at line 72 of file colors.cpp.

References BLACK, ColorGetName(), NBCOLORS, NextColor(), and UNSPECIFIED_COLOR.

Referenced by PARAM_CFG_SETCOLOR::ReadParam().

73 {
74  // look for a match in the palette itself
75  for( EDA_COLOR_T trying = BLACK; trying < NBCOLORS; trying = NextColor(trying) )
76  {
77  if( 0 == aName.CmpNoCase( ColorGetName( trying ) ) )
78  return trying;
79  }
80 
81  // Not found, no idea...
82  return UNSPECIFIED_COLOR;
83 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
const wxChar * ColorGetName(EDA_COLOR_T aColor)
Definition: colors.h:185
Definition: colors.h:45
Number of colors.
Definition: colors.h:75
EDA_COLOR_T NextColor(EDA_COLOR_T &aColor)
Definition: colors.h:87
EDA_COLOR_T ColorFindNearest ( const wxColour &  aColor)

Find the nearest color match.

Definition at line 96 of file colors.cpp.

References ColorFindNearest().

Referenced by ColorFindNearest(), ColorMix(), getDXFColorName(), and CHOOSE_COLOR_DLG::init_Dialog().

97 {
98  return ColorFindNearest( aColor.Red(), aColor.Green(), aColor.Blue() );
99 }
EDA_COLOR_T ColorFindNearest(const wxColour &aColor)
Find the nearest color match.
Definition: colors.cpp:96
EDA_COLOR_T ColorFindNearest ( int  aR,
int  aG,
int  aB 
)

Find the nearest color match.

Parameters
aRis the red component of the color to be matched (in range 0-255)
aGis the green component of the color to be matched (in range 0-255)
aBis the blue component of the color to be matched (in range 0-255)

Definition at line 102 of file colors.cpp.

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

103 {
104  EDA_COLOR_T candidate = BLACK;
105 
106  /* Find the 'nearest' color in the palette. This is fun. There is
107  a gazilion of metrics for the color space and no one of the
108  useful one is in the RGB color space. Who cares, this is a CAD,
109  not a photosomething...
110 
111  I hereby declare that the distance is the sum of the square of the
112  component difference. Think about the RGB color cube. Now get the
113  euclidean distance, but without the square root... for ordering
114  purposes it's the same, obviously. Also each component can't be
115  less of the target one, since I found this currently work better...
116  */
117  int nearest_distance = 255 * 255 * 3 + 1; // Can't beat this
118 
119  for( EDA_COLOR_T trying = BLACK; trying < NBCOLORS; trying = NextColor(trying) )
120  {
121  const StructColors &c = g_ColorRefs[trying];
122  int distance = (aR - c.m_Red) * (aR - c.m_Red) +
123  (aG - c.m_Green) * (aG - c.m_Green) +
124  (aB - c.m_Blue) * (aB - c.m_Blue);
125 
126  if( distance < nearest_distance && c.m_Red >= aR &&
127  c.m_Green >= aG && c.m_Blue >= aB )
128  {
129  nearest_distance = distance;
130  candidate = trying;
131  }
132  }
133 
134  return candidate;
135 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
unsigned char m_Red
Definition: colors.h:146
unsigned char m_Green
Definition: colors.h:145
unsigned char m_Blue
Definition: colors.h:144
const StructColors g_ColorRefs[NBCOLORS]
The predefined colors used in KiCad.
Definition: colors.cpp:37
Definition: colors.h:45
Number of colors.
Definition: colors.h:75
EDA_COLOR_T NextColor(EDA_COLOR_T &aColor)
Definition: colors.h:87
bool ColorIsLight ( EDA_COLOR_T  aColor)

Check if a color is light i.e.

if black would be more readable than white on it

Definition at line 86 of file colors.cpp.

References ColorGetBase(), StructColors::m_Blue, StructColors::m_Green, and StructColors::m_Red.

87 {
88  const StructColors &c = g_ColorRefs[ColorGetBase( aColor )];
89  int r = c.m_Red;
90  int g = c.m_Green;
91  int b = c.m_Blue;
92  return ((r * r) + (g * g) + (b * b)) > (128 * 128 * 3);
93 }
EDA_COLOR_T ColorGetBase(EDA_COLOR_T aColor)
Return only the plain color part.
Definition: colors.h:96
unsigned char m_Red
Definition: colors.h:146
unsigned char m_Green
Definition: colors.h:145
unsigned char m_Blue
Definition: colors.h:144
const StructColors g_ColorRefs[NBCOLORS]
The predefined colors used in KiCad.
Definition: colors.cpp:37
EDA_COLOR_T ColorMix ( EDA_COLOR_T  aColor1,
EDA_COLOR_T  aColor2 
)

Mix two colors in some way (hopefully like a logical OR)

Definition at line 138 of file colors.cpp.

References BLACK, ColorFindNearest(), ColorGetBase(), DARKDARKGRAY, StructColors::m_Blue, StructColors::m_Green, StructColors::m_Red, and NBCOLORS.

139 {
140  /* Memoization storage. This could be potentially called for each
141  * color merge so a cache is useful (there are few colours anyway) */
142  static EDA_COLOR_T mix_cache[NBCOLORS][NBCOLORS];
143 
144  // TODO how is alpha used? it's a mac only thing, I have no idea
145  aColor1 = ColorGetBase( aColor1 );
146  aColor2 = ColorGetBase( aColor2 );
147 
148  // First easy thing: a black gives always the other colour
149  if( aColor1 == BLACK )
150  return aColor2;
151 
152  if( aColor2 == BLACK)
153  return aColor1;
154 
155  /* Now we are sure that black can't occur, so the rule is:
156  * BLACK means not computed yet. If we're lucky we already have
157  * an answer */
158  EDA_COLOR_T candidate = mix_cache[aColor1][aColor2];
159 
160  if( candidate != BLACK )
161  return candidate;
162 
163  // Blend the two colors (i.e. OR the RGB values)
164  const StructColors &c1 = g_ColorRefs[aColor1];
165  const StructColors &c2 = g_ColorRefs[aColor2];
166 
167  // Ask the palette for the nearest color to the mix
168  wxColour mixed( c1.m_Red | c2.m_Red,
169  c1.m_Green | c2.m_Green,
170  c1.m_Blue | c2.m_Blue );
171  candidate = ColorFindNearest( mixed );
172 
173  /* Here, BLACK is *not* a good answer, since it would recompute the next time.
174  * Even theorically its not possible (with the current rules), but
175  * maybe the metric will change in the future */
176  if( candidate == BLACK )
177  candidate = DARKDARKGRAY;
178 
179  // Store the result in the cache. The operation is commutative, too
180  mix_cache[aColor1][aColor2] = candidate;
181  mix_cache[aColor2][aColor1] = candidate;
182  return candidate;
183 }
EDA_COLOR_T ColorGetBase(EDA_COLOR_T aColor)
Return only the plain color part.
Definition: colors.h:96
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
EDA_COLOR_T ColorFindNearest(const wxColour &aColor)
Find the nearest color match.
Definition: colors.cpp:96
unsigned char m_Red
Definition: colors.h:146
unsigned char m_Green
Definition: colors.h:145
unsigned char m_Blue
Definition: colors.h:144
const StructColors g_ColorRefs[NBCOLORS]
The predefined colors used in KiCad.
Definition: colors.cpp:37
Definition: colors.h:45
Number of colors.
Definition: colors.h:75

Variable Documentation

const StructColors g_ColorRefs[NBCOLORS]

The predefined colors used in KiCad.

list of existing Colors

Please: if you change a value, remember these values are carefully chosen to have good results in Pcbnew, that uses the ORed value of basic colors when displaying superimposed objects This list must have exactly NBCOLORS items

Definition at line 37 of file colors.cpp.

Referenced by KIGFX::COLOR4D::COLOR4D(), and CHOOSE_COLOR_DLG::init_Dialog().