KiCad PCB EDA Suite
color_settings.cpp
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) 2020 Jon Evans <jon@craftyjon.com>
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 #include <pgm_base.h>
24 #include <settings/parameters.h>
26 
27 
28 extern const char* traceSettings;
29 
31 const int colorsSchemaVersion = 1;
32 
33 
34 COLOR_SETTINGS::COLOR_SETTINGS( std::string aFilename ) :
35  JSON_SETTINGS( std::move( aFilename ), SETTINGS_LOC::COLORS, colorsSchemaVersion ),
36  m_overrideSchItemColors( false )
37 {
38 
39  m_params.emplace_back( new PARAM<wxString>( "meta.name", &m_displayName, "KiCad Default" ) );
40 
41  std::vector<COLOR4D> default_palette = {
42  COLOR4D( RED ),
43  COLOR4D( YELLOW ),
45  COLOR4D( LIGHTRED ),
46  COLOR4D( CYAN ),
47  COLOR4D( GREEN ),
48  COLOR4D( BLUE ),
49  COLOR4D( DARKGRAY ),
50  COLOR4D( MAGENTA ),
51  COLOR4D( LIGHTGRAY ),
52  COLOR4D( MAGENTA ),
53  COLOR4D( RED ),
54  COLOR4D( BROWN ),
55  COLOR4D( LIGHTGRAY ),
56  COLOR4D( BLUE ),
57  COLOR4D( GREEN )
58  };
59 
60  // TODO(JE) in actual usage, how long does the default palette need to be?
61  m_params.emplace_back( new PARAM_LIST<COLOR4D>( "palette", &m_Palette, default_palette ) );
62 
63  m_params.emplace_back( new PARAM<bool>( "schematic.override_item_colors",
64  &m_overrideSchItemColors, false ) );
65 
66 #define CLR( x, y, z ) m_params.emplace_back( new COLOR_MAP_PARAM( x, y, z, &m_colors ) )
67 
68  CLR( "schematic.background", LAYER_SCHEMATIC_BACKGROUND, COLOR4D( WHITE ) );
69  CLR( "schematic.brightened", LAYER_BRIGHTENED, COLOR4D( PUREMAGENTA ) );
70  CLR( "schematic.bus", LAYER_BUS, COLOR4D( BLUE ) );
71  CLR( "schematic.bus_junction", LAYER_BUS_JUNCTION, COLOR4D( BLUE ) );
72  CLR( "schematic.component_body", LAYER_DEVICE_BACKGROUND, COLOR4D( LIGHTYELLOW ) );
73  CLR( "schematic.component_outline", LAYER_DEVICE, COLOR4D( RED ) );
74  CLR( "schematic.cursor", LAYER_SCHEMATIC_CURSOR, COLOR4D( BLACK ) );
75  CLR( "schematic.erc_error", LAYER_ERC_ERR, COLOR4D( PURERED ).WithAlpha( 0.8 ) );
76  CLR( "schematic.erc_warning", LAYER_ERC_WARN, COLOR4D( PUREGREEN ).WithAlpha( 0.8 ) );
77  CLR( "schematic.fields", LAYER_FIELDS, COLOR4D( MAGENTA ) );
78  CLR( "schematic.grid", LAYER_SCHEMATIC_GRID, COLOR4D( DARKGRAY ) );
79  CLR( "schematic.grid_axes", LAYER_SCHEMATIC_GRID_AXES, COLOR4D( BLUE ) );
80  CLR( "schematic.hidden", LAYER_HIDDEN, COLOR4D( LIGHTGRAY ) );
81  CLR( "schematic.junction", LAYER_JUNCTION, COLOR4D( GREEN ) );
82  CLR( "schematic.label_global", LAYER_GLOBLABEL, COLOR4D( RED ) );
83  CLR( "schematic.label_hier", LAYER_HIERLABEL, COLOR4D( BROWN ) );
84  CLR( "schematic.label_local", LAYER_LOCLABEL, COLOR4D( BLACK ) );
85  CLR( "schematic.net_name", LAYER_NETNAM, COLOR4D( DARKGRAY ) );
86  CLR( "schematic.no_connect", LAYER_NOCONNECT, COLOR4D( BLUE ) );
87  CLR( "schematic.note", LAYER_NOTES, COLOR4D( LIGHTBLUE ) );
88  CLR( "schematic.pin", LAYER_PIN, COLOR4D( RED ) );
89  CLR( "schematic.pin_name", LAYER_PINNAM, COLOR4D( CYAN ) );
90  CLR( "schematic.pin_number", LAYER_PINNUM, COLOR4D( RED ) );
91  CLR( "schematic.reference", LAYER_REFERENCEPART, COLOR4D( CYAN ) );
92  // Macs look better with a lighter shadow
93 #ifdef __WXMAC__
94  CLR( "schematic.shadow", LAYER_SELECTION_SHADOWS, COLOR4D( .78, .92, 1.0, 0.8 ) );
95 #else
96  CLR( "schematic.shadow", LAYER_SELECTION_SHADOWS, COLOR4D( .4, .7, 1.0, 0.8 ) );
97 #endif
98  CLR( "schematic.sheet", LAYER_SHEET, COLOR4D( MAGENTA ) );
99  CLR( "schematic.sheet_background", LAYER_SHEET_BACKGROUND, COLOR4D( WHITE ).WithAlpha( 0.0 ) );
100  CLR( "schematic.sheet_filename", LAYER_SHEETFILENAME, COLOR4D( BROWN ) );
101  CLR( "schematic.sheet_fields", LAYER_SHEETFIELDS, COLOR4D( MAGENTA ) );
102  CLR( "schematic.sheet_label", LAYER_SHEETLABEL, COLOR4D( CYAN ) );
103  CLR( "schematic.sheet_name", LAYER_SHEETNAME, COLOR4D( CYAN ) );
104  CLR( "schematic.value", LAYER_VALUEPART, COLOR4D( CYAN ) );
105  CLR( "schematic.wire", LAYER_WIRE, COLOR4D( GREEN ) );
106  CLR( "schematic.worksheet", LAYER_SCHEMATIC_WORKSHEET, COLOR4D( RED ) );
107 
108  CLR( "gerbview.axes", LAYER_GERBVIEW_AXES, COLOR4D( BLUE ) );
109  CLR( "gerbview.background", LAYER_GERBVIEW_BACKGROUND, COLOR4D( BLACK ) );
110  CLR( "gerbview.dcodes", LAYER_DCODES, COLOR4D( WHITE ) );
111  CLR( "gerbview.grid", LAYER_GERBVIEW_GRID, COLOR4D( MAGENTA ) );
112  CLR( "gerbview.negative_objects", LAYER_NEGATIVE_OBJECTS, COLOR4D( DARKGRAY ) );
113  CLR( "gerbview.worksheet", LAYER_GERBVIEW_WORKSHEET, COLOR4D( BLUE ) );
114 
115  // TODO(JE) New default scheme for GerbView
116  for( int i = 0, id = GERBVIEW_LAYER_ID_START;
118  {
119  CLR( "gerbview.layers." + std::to_string( i ), id,
120  default_palette[ i % default_palette.size() ] );
121  }
122 
123  CLR( "board.anchor", LAYER_ANCHOR, COLOR4D( BLUE ) );
124  CLR( "board.aux_items", LAYER_AUX_ITEMS, COLOR4D( WHITE ) );
125  CLR( "board.background", LAYER_PCB_BACKGROUND, COLOR4D( BLACK ) );
126  CLR( "board.cursor", LAYER_CURSOR, COLOR4D( WHITE ) );
127  CLR( "board.drc_error", LAYER_DRC_ERROR, COLOR4D( PURERED ) );
128  CLR( "board.drc_warning", LAYER_DRC_WARNING, COLOR4D( PUREYELLOW ) );
129  CLR( "board.footprint_text_back", LAYER_MOD_TEXT_BK, COLOR4D( BLUE ) );
130  CLR( "board.footprint_text_front", LAYER_MOD_TEXT_FR, COLOR4D( LIGHTGRAY ) );
131  CLR( "board.footprint_text_invisible", LAYER_MOD_TEXT_INVISIBLE, COLOR4D( LIGHTGRAY ) );
132  CLR( "board.grid", LAYER_GRID, COLOR4D( DARKGRAY ) );
133  CLR( "board.grid_axes", LAYER_GRID_AXES, COLOR4D( LIGHTGRAY ) );
134  CLR( "board.no_connect", LAYER_NO_CONNECTS, COLOR4D( BLUE ) );
135  CLR( "board.pad_back", LAYER_PAD_BK, COLOR4D( GREEN ) );
136  CLR( "board.pad_front", LAYER_PAD_FR, COLOR4D( RED ) );
137  CLR( "board.pad_plated_hole", LAYER_PADS_PLATEDHOLES, COLOR4D( YELLOW ) );
138  CLR( "board.pad_through_hole", LAYER_PADS_TH, COLOR4D( YELLOW ) );
139  CLR( "board.plated_hole", LAYER_NON_PLATEDHOLES, COLOR4D( YELLOW ) );
140  CLR( "board.ratsnest", LAYER_RATSNEST, COLOR4D( WHITE ) );
141  CLR( "board.select_overlay", LAYER_SELECT_OVERLAY, COLOR4D( DARKRED ) );
142  CLR( "board.through_via", LAYER_VIA_THROUGH, COLOR4D( LIGHTGRAY ) );
143  CLR( "board.via", LAYER_VIAS, COLOR4D( BLACK ) );
144  CLR( "board.via_blind_buried", LAYER_VIA_BBLIND, COLOR4D( BROWN ) );
145  CLR( "board.via_hole", LAYER_VIAS_HOLES, COLOR4D( WHITE ) );
146  CLR( "board.via_micro", LAYER_VIA_MICROVIA, COLOR4D( CYAN ) );
147  CLR( "board.via_through", LAYER_VIA_THROUGH, COLOR4D( LIGHTGRAY ) );
148  CLR( "board.worksheet", LAYER_WORKSHEET, COLOR4D( DARKRED ) );
149 
150  CLR( "board.copper.f", F_Cu, COLOR4D( RED ) );
151  CLR( "board.copper.in1", In1_Cu, COLOR4D( YELLOW ) );
152  CLR( "board.copper.in2", In2_Cu, COLOR4D( LIGHTMAGENTA ) );
153  CLR( "board.copper.in3", In3_Cu, COLOR4D( LIGHTRED ) );
154  CLR( "board.copper.in4", In4_Cu, COLOR4D( CYAN ) );
155  CLR( "board.copper.in5", In5_Cu, COLOR4D( GREEN ) );
156  CLR( "board.copper.in6", In6_Cu, COLOR4D( BLUE ) );
157  CLR( "board.copper.in7", In7_Cu, COLOR4D( DARKGRAY ) );
158  CLR( "board.copper.in8", In8_Cu, COLOR4D( MAGENTA ) );
159  CLR( "board.copper.in9", In9_Cu, COLOR4D( LIGHTGRAY ) );
160  CLR( "board.copper.in10", In10_Cu, COLOR4D( MAGENTA ) );
161  CLR( "board.copper.in11", In11_Cu, COLOR4D( RED ) );
162  CLR( "board.copper.in12", In12_Cu, COLOR4D( BROWN ) );
163  CLR( "board.copper.in13", In13_Cu, COLOR4D( LIGHTGRAY ) );
164  CLR( "board.copper.in14", In14_Cu, COLOR4D( BLUE ) );
165  CLR( "board.copper.in15", In15_Cu, COLOR4D( GREEN ) );
166  CLR( "board.copper.in16", In16_Cu, COLOR4D( RED ) );
167  CLR( "board.copper.in17", In17_Cu, COLOR4D( YELLOW ) );
168  CLR( "board.copper.in18", In18_Cu, COLOR4D( LIGHTMAGENTA ) );
169  CLR( "board.copper.in19", In19_Cu, COLOR4D( LIGHTRED ) );
170  CLR( "board.copper.in20", In20_Cu, COLOR4D( CYAN ) );
171  CLR( "board.copper.in21", In21_Cu, COLOR4D( GREEN ) );
172  CLR( "board.copper.in22", In22_Cu, COLOR4D( BLUE ) );
173  CLR( "board.copper.in23", In23_Cu, COLOR4D( DARKGRAY ) );
174  CLR( "board.copper.in24", In24_Cu, COLOR4D( MAGENTA ) );
175  CLR( "board.copper.in25", In25_Cu, COLOR4D( LIGHTGRAY ) );
176  CLR( "board.copper.in26", In26_Cu, COLOR4D( MAGENTA ) );
177  CLR( "board.copper.in27", In27_Cu, COLOR4D( RED ) );
178  CLR( "board.copper.in28", In28_Cu, COLOR4D( BROWN ) );
179  CLR( "board.copper.in29", In29_Cu, COLOR4D( LIGHTGRAY ) );
180  CLR( "board.copper.in30", In30_Cu, COLOR4D( BLUE ) );
181  CLR( "board.copper.b", B_Cu, COLOR4D( GREEN ) );
182 
183  CLR( "board.b_adhes", B_Adhes, COLOR4D( BLUE ) );
184  CLR( "board.f_adhes", F_Adhes, COLOR4D( MAGENTA ) );
185  CLR( "board.b_paste", B_Paste, COLOR4D( LIGHTCYAN ) );
186  CLR( "board.f_paste", F_Paste, COLOR4D( RED ) );
187  CLR( "board.b_silks", B_SilkS, COLOR4D( MAGENTA ) );
188  CLR( "board.f_silks", F_SilkS, COLOR4D( CYAN ) );
189  CLR( "board.b_mask", B_Mask, COLOR4D( BROWN ) );
190  CLR( "board.f_mask", F_Mask, COLOR4D( MAGENTA ) );
191  CLR( "board.dwgs_user", Dwgs_User, COLOR4D( LIGHTGRAY ) );
192  CLR( "board.cmts_user", Cmts_User, COLOR4D( BLUE ) );
193  CLR( "board.eco1_user", Eco1_User, COLOR4D( GREEN ) );
194  CLR( "board.eco2_user", Eco2_User, COLOR4D( YELLOW ) );
195  CLR( "board.edge_cuts", Edge_Cuts, COLOR4D( YELLOW ) );
196  CLR( "board.margin", Margin, COLOR4D( LIGHTMAGENTA ) );
197  CLR( "board.b_crtyd", B_CrtYd, COLOR4D( DARKGRAY ) );
198  CLR( "board.f_crtyd", F_CrtYd, COLOR4D( LIGHTGRAY ) );
199  CLR( "board.b_fab", B_Fab, COLOR4D( BLUE ) );
200  CLR( "board.f_fab", F_Fab, COLOR4D( DARKGRAY ) );
201 
202  // Colors for 3D viewer, which are used as defaults unless overridden by the board
203  CLR( "3d_viewer.background_bottom", LAYER_3D_BACKGROUND_BOTTOM, COLOR4D( 0.4, 0.4, 0.5, 1.0 ) );
204  CLR( "3d_viewer.background_top", LAYER_3D_BACKGROUND_TOP, COLOR4D( 0.8, 0.8, 0.9, 1.0 ) );
205  CLR( "3d_viewer.board", LAYER_3D_BOARD, COLOR4D( 0.2, 0.17, 0.09, 1.0 ) );
206  CLR( "3d_viewer.copper", LAYER_3D_COPPER, COLOR4D( 0.7, 0.61, 0.0, 1.0 ) );
207  CLR( "3d_viewer.silkscreen_bottom", LAYER_3D_SILKSCREEN_BOTTOM, COLOR4D( 0.9, 0.9, 0.9, 1.0 ) );
208  CLR( "3d_viewer.silkscreen_top", LAYER_3D_SILKSCREEN_TOP, COLOR4D( 0.9, 0.9, 0.9, 1.0 ) );
209  CLR( "3d_viewer.soldermask", LAYER_3D_SOLDERMASK, COLOR4D( 0.08, 0.2, 0.14, 1.0 ) );
210  CLR( "3d_viewer.solderpaste", LAYER_3D_SOLDERPASTE, COLOR4D( 0.5, 0.5, 0.5, 1.0 ) );
211 }
212 
213 
214 bool COLOR_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
215 {
216  return false;
217 }
218 
219 
221 {
222  bool ret = true;
223  int filever = at( PointerFromString( "meta.version" ) ).get<int>();
224 
225  if( filever == 0 )
226  {
227  ret &= migrateSchema0to1();
228 
229  if( ret )
230  {
231  ( *this )[PointerFromString( "meta.version" )] = 1;
232  }
233  }
234 
235  return ret;
236 }
237 
238 
240 {
248  if( !m_manager )
249  {
250  wxLogTrace( traceSettings, "Error: COLOR_SETTINGS migration cannot run unmanaged!" );
251  return false;
252  }
253 
254  nlohmann::json::json_pointer board( "/board" );
255  nlohmann::json::json_pointer fpedit( "/fpedit" );
256 
257  if( !contains( fpedit ) )
258  {
259  wxLogTrace( traceSettings, "migrateSchema0to1: %s doesn't have fpedit settings; skipping.",
260  m_filename );
261  return true;
262  }
263 
264  wxString filename = m_filename + wxT( "_footprints" );
265 
266  COLOR_SETTINGS* fpsettings = m_manager->AddNewColorSettings( filename );
267 
268  // Start out with a clone
269  nlohmann::json::json_pointer root( "" );
270  ( *fpsettings )[root] = at( root );
271 
272  // Footprint editor now just looks at the "board" namespace
273  ( *fpsettings )[board] = fpsettings->at( fpedit );
274 
275  fpsettings->erase( "fpedit" );
276  fpsettings->Load();
277  fpsettings->SetName( fpsettings->GetName() + _( " (Footprints)" ) );
278  m_manager->Save( fpsettings );
279 
280  // Now we can get rid of our own copy
281  erase( "fpedit" );
282 
283  return true;
284 }
285 
286 
288 {
289  if( m_colors.count( aLayer ) )
290  return m_colors.at( aLayer );
291 
292  return COLOR4D::UNSPECIFIED;
293 }
294 
295 
297 {
298  if( !m_defaultColors.count( aLayer ) )
299  {
300  COLOR_MAP_PARAM* p = nullptr;
301 
302  for( PARAM_BASE* param : m_params )
303  {
304  COLOR_MAP_PARAM* cmp = dynamic_cast<COLOR_MAP_PARAM*>( param );
305 
306  if( cmp && cmp->GetKey() == aLayer )
307  p = cmp;
308  }
309 
310  if( p )
311  m_defaultColors[aLayer] = p->GetDefault();
312  else
313  m_defaultColors[aLayer] = COLOR4D::UNSPECIFIED;
314  }
315 
316  return m_defaultColors.at( aLayer );;
317 }
318 
319 
320 void COLOR_SETTINGS::SetColor( int aLayer, COLOR4D aColor )
321 {
322  m_colors[ aLayer ] = aColor;
323 }
Definition: colors.h:57
to draw blind/buried vias
show a marker on pads with no nets
std::string m_filename
The filename (not including path) of this settings file.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
multilayer pads, usually with holes
handle color for not plated holes (holes, not pads)
anchor of items having an anchor point (texts, footprints)
COLOR_SETTINGS(std::string aFilename="user")
Definition: colors.h:61
bool m_overrideSchItemColors
SETTINGS_LOC
Definition: json_settings.h:36
to draw via holes (pad holes do not use this layer)
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
const int colorsSchemaVersion
! Update the schema version whenever a migration is required
wxString m_displayName
const wxString & GetName() const
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Registers a new color settings object with the given filename.
COLOR4D GetDefault() const
Definition: colors.h:54
The color scheme directory (e.g. ~/.config/kicad/colors/)
Definition: colors.h:72
Auxiliary items (guides, rule, etc)
#define GERBER_DRAWLAYERS_COUNT
#define CLR(x, y, z)
to draw usual through hole vias
Definition: colors.h:59
COLOR4D GetDefaultColor(int aLayer)
void SetName(const wxString &aName)
Definition: colors.h:60
std::unordered_map< int, COLOR4D > m_defaultColors
layer for drc markers with SEVERITY_WARNING
std::vector< COLOR4D > m_Palette
m_Pallete stores a list of colors that are used, in order, when a list of colors needs to be generate...
COLOR4D GetColor(int aLayer) const
std::unordered_map< int, COLOR4D > m_colors
Map of all layer colors.
smd pads, front layer
Definition: colors.h:58
see class PGM_BASE
Board layer functions and definitions.
#define _(s)
Definition: 3d_actions.cpp:33
currently selected items overlay
Definition: colors.h:49
bool MigrateFromLegacy(wxConfigBase *aCfg) override
Migrates from wxConfig to JSON-based configuration.
bool Migrate() override
Migrates the schema of this settings from the version in the file to the latest version.
Color settings are a bit different than most of the settings objects in that there can be more than o...
Definition: colors.h:45
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
const char * traceSettings
Flag to enable settings tracing.
Definition: colors.h:68
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
layer for drc markers with SEVERITY_ERROR
void SetColor(int aLayer, COLOR4D aColor)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
Definition: colors.h:62