KiCad PCB EDA Suite
colors.h File Reference
#include <wx/wx.h>

Go to the source code of this file.

Classes

struct  StructColors
 

Enumerations

enum  EDA_COLOR_T {
  UNSPECIFIED_COLOR = -1, BLACK = 0, DARKDARKGRAY, DARKGRAY,
  LIGHTGRAY, WHITE, LIGHTYELLOW, DARKBLUE,
  DARKGREEN, DARKCYAN, DARKRED, DARKMAGENTA,
  DARKBROWN, BLUE, GREEN, CYAN,
  RED, MAGENTA, BROWN, LIGHTBLUE,
  LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA,
  YELLOW, PUREBLUE, PUREGREEN, PURECYAN,
  PURERED, PUREMAGENTA, PUREYELLOW, NBCOLORS,
  HIGHLIGHT_FLAG = ( 1<<19 ), MASKCOLOR = 31
}
 NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas. More...
 

Functions

EDA_COLOR_T ColorFromInt (int aColor)
 Checked cast. Use only when necessary (usually I/O) More...
 
EDA_COLOR_T NextColor (EDA_COLOR_T &aColor)
 
EDA_COLOR_T ColorGetBase (EDA_COLOR_T aColor)
 Return only the plain color part. 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...
 
void ColorTurnToDarkDarkGray (EDA_COLOR_T *aColor)
 Force the color part of a color to darkdarkgray. More...
 
void ColorChangeHighlightFlag (EDA_COLOR_T *aColor, bool flag)
 
void SetAlpha (EDA_COLOR_T *aColor, unsigned char aBlend)
 Function SetAlpha ORs in the alpha blend parameter in to a color index. More...
 
unsigned char GetAlpha (EDA_COLOR_T aColor)
 Function GetAlpha returns the alpha blend parameter from a color index. More...
 
void ColorApplyHighlightFlag (EDA_COLOR_T *aColor)
 Step a color to the highlighted version if the highlight flag is set. More...
 
EDA_COLOR_T ColorByName (const wxString &aName)
 Find a color by name. 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...
 
bool ColorIsLight (EDA_COLOR_T aColor)
 Check if a color is light i.e. More...
 
const wxChar * ColorGetName (EDA_COLOR_T aColor)
 
void ColorSetBrush (wxBrush *aBrush, EDA_COLOR_T aColor)
 
wxColour MakeColour (EDA_COLOR_T aColor)
 Function MakeColour returns a wxWidgets wxColor from a KiCad color index with alpha value. More...
 

Variables

const StructColors g_ColorRefs [NBCOLORS]
 list of existing Colors More...
 

Enumeration Type Documentation

NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.

Once there is no need for legacy support, the color table g_ColorRefs in colors.cpp can be re-written in COLOR4D and this file can go away.

Please use COLOR4D instead of including this file. The color enumeration. Also contains a flag and the alpha value in the upper bits

Enumerator
UNSPECIFIED_COLOR 
BLACK 
DARKDARKGRAY 
DARKGRAY 
LIGHTGRAY 
WHITE 
LIGHTYELLOW 
DARKBLUE 
DARKGREEN 
DARKCYAN 
DARKRED 
DARKMAGENTA 
DARKBROWN 
BLUE 
GREEN 
CYAN 
RED 
MAGENTA 
BROWN 
LIGHTBLUE 
LIGHTGREEN 
LIGHTCYAN 
LIGHTRED 
LIGHTMAGENTA 
YELLOW 
PUREBLUE 
PUREGREEN 
PURECYAN 
PURERED 
PUREMAGENTA 
PUREYELLOW 
NBCOLORS 

Number of colors.

HIGHLIGHT_FLAG 
MASKCOLOR 

mask for color index into g_ColorRefs[]

Definition at line 42 of file colors.h.

43 {
44  UNSPECIFIED_COLOR = -1,
45  BLACK = 0,
47  DARKGRAY,
48  LIGHTGRAY,
49  WHITE,
51  DARKBLUE,
52  DARKGREEN,
53  DARKCYAN,
54  DARKRED,
56  DARKBROWN,
57  BLUE,
58  GREEN,
59  CYAN,
60  RED,
61  MAGENTA,
62  BROWN,
63  LIGHTBLUE,
64  LIGHTGREEN,
65  LIGHTCYAN,
66  LIGHTRED,
68  YELLOW,
69  PUREBLUE,
70  PUREGREEN,
71  PURECYAN,
72  PURERED,
74  PUREYELLOW,
75  NBCOLORS,
76  HIGHLIGHT_FLAG = ( 1<<19 ),
77  MASKCOLOR = 31
78 };
Definition: colors.h:57
Definition: colors.h:61
mask for color index into g_ColorRefs[]
Definition: colors.h:77
Definition: colors.h:54
Definition: colors.h:72
Definition: colors.h:59
Definition: colors.h:60
Definition: colors.h:58
Definition: colors.h:49
Definition: colors.h:45
Definition: colors.h:68
Number of colors.
Definition: colors.h:75
Definition: colors.h:62

Function Documentation

void ColorApplyHighlightFlag ( EDA_COLOR_T aColor)
inline

Step a color to the highlighted version if the highlight flag is set.

Definition at line 157 of file colors.h.

References ColorGetBase(), HIGHLIGHT_FLAG, StructColors::m_LightColor, NBCOLORS, and UNSPECIFIED_COLOR.

158 {
159  EDA_COLOR_T base = ColorGetBase( *aColor );
160  wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
161  if( *aColor & HIGHLIGHT_FLAG )
162  *aColor = g_ColorRefs[base].m_LightColor;
163 }
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 m_LightColor
Definition: colors.h:150
Number of colors.
Definition: colors.h:75
const StructColors g_ColorRefs[NBCOLORS]
list of existing Colors
Definition: colors.cpp:37
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
void ColorChangeHighlightFlag ( EDA_COLOR_T aColor,
bool  flag 
)
inline

Definition at line 111 of file colors.h.

References HIGHLIGHT_FLAG.

112 {
113  if( flag )
114  *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) | HIGHLIGHT_FLAG ) );
115  else
116  *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) & ~HIGHLIGHT_FLAG ) );
117 }
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 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
EDA_COLOR_T ColorFromInt ( int  aColor)
inline

Checked cast. Use only when necessary (usually I/O)

Definition at line 81 of file colors.h.

References NBCOLORS, and UNSPECIFIED_COLOR.

Referenced by LAYER_WIDGET::GetLayerColor().

82 {
83  wxASSERT( aColor >= UNSPECIFIED_COLOR && aColor < NBCOLORS );
84  return static_cast<EDA_COLOR_T>( aColor );
85 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
Number of colors.
Definition: colors.h:75
EDA_COLOR_T ColorGetBase ( EDA_COLOR_T  aColor)
inline

Return only the plain color part.

Definition at line 96 of file colors.h.

References MASKCOLOR.

Referenced by ColorApplyHighlightFlag(), ColorGetName(), ColorIsLight(), ColorMix(), ColorSetBrush(), and MakeColour().

97 {
98  EDA_COLOR_T base = static_cast<EDA_COLOR_T>( aColor & MASKCOLOR );
99  return base;
100 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
mask for color index into g_ColorRefs[]
Definition: colors.h:77
const wxChar* ColorGetName ( EDA_COLOR_T  aColor)
inline

Definition at line 185 of file colors.h.

References ColorGetBase(), StructColors::m_ColorName, NBCOLORS, and UNSPECIFIED_COLOR.

Referenced by ColorByName(), and CHOOSE_COLOR_DLG::init_Dialog().

186 {
187  EDA_COLOR_T base = ColorGetBase( aColor );
188  wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
189  return g_ColorRefs[base].m_ColorName;
190 }
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
const wxChar * m_ColorName
Definition: colors.h:149
Number of colors.
Definition: colors.h:75
const StructColors g_ColorRefs[NBCOLORS]
list of existing Colors
Definition: colors.cpp:37
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
void ColorSetBrush ( wxBrush *  aBrush,
EDA_COLOR_T  aColor 
)
inline

Definition at line 192 of file colors.h.

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

193 {
194  EDA_COLOR_T base = ColorGetBase( aColor );
195  wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
196  const StructColors &col = g_ColorRefs[base];
197  aBrush->SetColour( col.m_Red, col.m_Green, col.m_Blue );
198 }
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
unsigned char m_Red
Definition: colors.h:146
unsigned char m_Green
Definition: colors.h:145
unsigned char m_Blue
Definition: colors.h:144
Number of colors.
Definition: colors.h:75
const StructColors g_ColorRefs[NBCOLORS]
list of existing Colors
Definition: colors.cpp:37
void ColorTurnToDarkDarkGray ( EDA_COLOR_T aColor)
inline

Force the color part of a color to darkdarkgray.

Definition at line 106 of file colors.h.

References DARKDARKGRAY, and MASKCOLOR.

107 {
108  *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) & ~MASKCOLOR) | DARKDARKGRAY );
109 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
mask for color index into g_ColorRefs[]
Definition: colors.h:77
unsigned char GetAlpha ( EDA_COLOR_T  aColor)
inline

Function GetAlpha returns the alpha blend parameter from a color index.

Definition at line 135 of file colors.h.

Referenced by MakeColour().

136 {
137  const unsigned char MASKALPHA = 0xFF;
138  return (aColor >> 24) & MASKALPHA;
139 }
wxColour MakeColour ( EDA_COLOR_T  aColor)
inline

Function MakeColour returns a wxWidgets wxColor from a KiCad color index with alpha value.

Note that alpha support is not available on every wxWidgets platform. On such platform the behavior is the same as for wxALPHA_OPAQUE and that means the alpha value has no effect and will be ignored.

Returns
wxColour - given a KiCad color index with alpha value

Definition at line 208 of file colors.h.

References ColorGetBase(), GetAlpha(), NBCOLORS, and UNSPECIFIED_COLOR.

Referenced by PLEDITOR_PRINTOUT::DrawPage().

209 {
210  int alpha = GetAlpha( aColor );
211  alpha = alpha ? alpha : wxALPHA_OPAQUE;
212  EDA_COLOR_T ndx = ColorGetBase( aColor );
213  wxASSERT( ndx > UNSPECIFIED_COLOR && ndx < NBCOLORS );
214 
215  return wxColour( g_ColorRefs[ndx].m_Red,
216  g_ColorRefs[ndx].m_Green,
217  g_ColorRefs[ndx].m_Blue
218  ,(unsigned char) alpha
219  );
220 }
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
unsigned char GetAlpha(EDA_COLOR_T aColor)
Function GetAlpha returns the alpha blend parameter from a color index.
Definition: colors.h:135
Number of colors.
Definition: colors.h:75
const StructColors g_ColorRefs[NBCOLORS]
list of existing Colors
Definition: colors.cpp:37
EDA_COLOR_T NextColor ( EDA_COLOR_T aColor)
inline

Definition at line 87 of file colors.h.

References NBCOLORS, and UNSPECIFIED_COLOR.

Referenced by ColorByName(), ColorFindNearest(), and DXF_PLOTTER::StartPlot().

88 {
89  // We have to accept NBCOLORS for loop termination conditions
90  wxASSERT( aColor >= UNSPECIFIED_COLOR && aColor <= NBCOLORS );
91  aColor = static_cast<EDA_COLOR_T>( int( aColor ) + 1 );
92  return aColor;
93 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42
Number of colors.
Definition: colors.h:75
void SetAlpha ( EDA_COLOR_T aColor,
unsigned char  aBlend 
)
inline

Function SetAlpha ORs in the alpha blend parameter in to a color index.

Definition at line 123 of file colors.h.

124 {
125  const unsigned char MASKALPHA = 0xFF;
126 
127  *aColor = static_cast<EDA_COLOR_T>((*aColor & ~(MASKALPHA << 24))
128  | ((aBlend & MASKALPHA) << 24));
129 }
EDA_COLOR_T
NOTE: EDA_COLOR_T is deprecated and is kept around for compatibility with legacy canvas.
Definition: colors.h:42

Variable Documentation

const StructColors g_ColorRefs[NBCOLORS]

list of existing Colors

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(), CHOOSE_COLOR_DLG::init_Dialog(), and COLOR4D_PICKER_DLG::initDefinedColors().