KiCad PCB EDA Suite
colors.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2014 KiCad Developers, see AUTHORS.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
34 #ifndef COLORS_H_
35 #define COLORS_H_
36 
37 #include <wx/wx.h>
38 
43 {
45  BLACK = 0,
60  RED,
76  HIGHLIGHT_FLAG = ( 1<<19 ),
77  MASKCOLOR = 31
78 };
79 
81 inline EDA_COLOR_T ColorFromInt( int aColor )
82 {
83  wxASSERT( aColor >= UNSPECIFIED_COLOR && aColor < NBCOLORS );
84  return static_cast<EDA_COLOR_T>( aColor );
85 }
86 
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 }
94 
97 {
98  EDA_COLOR_T base = static_cast<EDA_COLOR_T>( aColor & MASKCOLOR );
99  return base;
100 }
101 
103 EDA_COLOR_T ColorMix( EDA_COLOR_T aColor1, EDA_COLOR_T aColor2 );
104 
106 inline void ColorTurnToDarkDarkGray( EDA_COLOR_T *aColor )
107 {
108  *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) & ~MASKCOLOR) | DARKDARKGRAY );
109 }
110 
111 inline void ColorChangeHighlightFlag( EDA_COLOR_T *aColor, bool 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 }
118 
123 inline void SetAlpha( EDA_COLOR_T* aColor, unsigned char aBlend )
124 {
125  const unsigned char MASKALPHA = 0xFF;
126 
127  *aColor = static_cast<EDA_COLOR_T>((*aColor & ~(MASKALPHA << 24))
128  | ((aBlend & MASKALPHA) << 24));
129 }
130 
135 inline unsigned char GetAlpha( EDA_COLOR_T aColor )
136 {
137  const unsigned char MASKALPHA = 0xFF;
138  return (aColor >> 24) & MASKALPHA;
139 }
140 
141 
143 {
144  unsigned char m_Blue;
145  unsigned char m_Green;
146  unsigned char m_Red;
148 
149  const wxChar* m_ColorName;
151 };
152 
154 extern const StructColors g_ColorRefs[NBCOLORS];
155 
157 inline void ColorApplyHighlightFlag( EDA_COLOR_T *aColor )
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 }
164 
166 EDA_COLOR_T ColorByName( const wxString& aName );
167 
169 EDA_COLOR_T ColorFindNearest( const wxColour &aColor );
170 
177 EDA_COLOR_T ColorFindNearest( int aR, int aG, int aB );
178 
183 bool ColorIsLight( EDA_COLOR_T aColor );
184 
185 inline const wxChar *ColorGetName( EDA_COLOR_T aColor )
186 {
187  EDA_COLOR_T base = ColorGetBase( aColor );
188  wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
189  return g_ColorRefs[base].m_ColorName;
190 }
191 
192 inline void ColorSetBrush( wxBrush *aBrush, EDA_COLOR_T aColor )
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 }
199 
208 inline wxColour MakeColour( EDA_COLOR_T aColor )
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 }
221 
222 #endif // COLORS_H_
Definition: colors.h:57
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
EDA_COLOR_T ColorFromInt(int aColor)
Checked cast. Use only when necessary (usually I/O)
Definition: colors.h:81
Definition: colors.h:61
unsigned char m_Green
Definition: colors.h:145
EDA_COLOR_T ColorMix(EDA_COLOR_T aColor1, EDA_COLOR_T aColor2)
Mix two colors in some way (hopefully like a logical OR)
Definition: colors.cpp:138
const wxChar * m_ColorName
Definition: colors.h:149
mask for color index into g_ColorRefs[]
Definition: colors.h:77
void ColorTurnToDarkDarkGray(EDA_COLOR_T *aColor)
Force the color part of a color to darkdarkgray.
Definition: colors.h:106
Definition: colors.h:54
wxColour MakeColour(EDA_COLOR_T aColor)
Function MakeColour returns a wxWidgets wxColor from a KiCad color index with alpha value...
Definition: colors.h:208
Definition: colors.h:72
EDA_COLOR_T ColorByName(const wxString &aName)
Find a color by name.
Definition: colors.cpp:72
unsigned char m_Blue
Definition: colors.h:144
Definition: colors.h:59
unsigned char GetAlpha(EDA_COLOR_T aColor)
Function GetAlpha returns the alpha blend parameter from a color index.
Definition: colors.h:135
const wxChar * ColorGetName(EDA_COLOR_T aColor)
Definition: colors.h:185
void ColorChangeHighlightFlag(EDA_COLOR_T *aColor, bool flag)
Definition: colors.h:111
Definition: colors.h:60
bool ColorIsLight(EDA_COLOR_T aColor)
Check if a color is light i.e.
Definition: colors.cpp:86
void ColorApplyHighlightFlag(EDA_COLOR_T *aColor)
Step a color to the highlighted version if the highlight flag is set.
Definition: colors.h:157
Definition: colors.h:58
EDA_COLOR_T m_LightColor
Definition: colors.h:150
void ColorSetBrush(wxBrush *aBrush, EDA_COLOR_T aColor)
Definition: colors.h:192
Definition: colors.h:49
EDA_COLOR_T ColorFindNearest(const wxColour &aColor)
Find the nearest color match.
Definition: colors.cpp:96
void SetAlpha(EDA_COLOR_T *aColor, unsigned char aBlend)
Function SetAlpha ORs in the alpha blend parameter in to a color index.
Definition: colors.h:123
EDA_COLOR_T m_Numcolor
Definition: colors.h:147
Definition: colors.h:45
Definition: colors.h:68
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)
Definition: colors.h:87
Definition: colors.h:62