KiCad PCB EDA Suite
COLOR_SWATCH Class Reference

A simple color swatch of the kind used to set layer colors. More...

#include <color_swatch.h>

Inheritance diagram for COLOR_SWATCH:

Public Member Functions

 COLOR_SWATCH (wxWindow *aParent, const KIGFX::COLOR4D &aColor, int aID, const KIGFX::COLOR4D &aBackground, const KIGFX::COLOR4D &aDefault, SWATCH_SIZE aSwatchType)
 Construct a COLOR_SWATCH. More...
 
 COLOR_SWATCH (wxWindow *aParent, wxWindowID aId, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=0)
 constructor for wxFormBuilder More...
 
void SetSwatchColor (const KIGFX::COLOR4D &aColor, bool aSendEvent)
 Set the current swatch color directly. More...
 
void SetDefaultColor (const KIGFX::COLOR4D &aColor)
 Sets the color that will be chosen with the "Reset to Default" button in the chooser. More...
 
void SetSwatchBackground (const KIGFX::COLOR4D &aBackground)
 Set the swatch background color. More...
 
void SetUserColors (CUSTOM_COLORS_LIST *aUserColors)
 Fetch a reference to the user colors list. More...
 
KIGFX::COLOR4D GetSwatchColor () const
 
void SetWindowID (wxWindowID aId)
 Updates the window ID of this control and its children. More...
 
void GetNewSwatchColor ()
 Prompt for a new colour, using the colour picker dialog. More...
 
void SetReadOnly (bool aReadOnly=true)
 
bool IsReadOnly () const
 
void SetSupportsOpacity (bool aSupportsOpacity)
 
void SetReadOnlyCallback (std::function< void()> aCallback)
 Registers a handler for when the user tries to interact with a read-only swatch. More...
 

Static Public Member Functions

static wxBitmap MakeBitmap (const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
 

Private Member Functions

void setupEvents ()
 
void rePostEvent (wxEvent &aEvent)
 Pass unwanted events on to listeners of this object. More...
 

Private Attributes

KIGFX::COLOR4D m_color
 
KIGFX::COLOR4D m_background
 
KIGFX::COLOR4D m_default
 
CUSTOM_COLORS_LISTm_userColors
 
wxStaticBitmap * m_swatch
 
wxSize m_size
 
wxSize m_checkerboardSize
 
KIGFX::COLOR4D m_checkerboardBg
 
bool m_readOnly
 A read-only swatch won't show the color chooser dialog but otherwise works normally. More...
 
std::function< void()> m_readOnlyCallback
 
bool m_supportsOpacity
 If opacity is not supported the color chooser dialog will be displayed without it. More...
 

Detailed Description

A simple color swatch of the kind used to set layer colors.

Definition at line 56 of file color_swatch.h.

Constructor & Destructor Documentation

◆ COLOR_SWATCH() [1/2]

COLOR_SWATCH::COLOR_SWATCH ( wxWindow *  aParent,
const KIGFX::COLOR4D aColor,
int  aID,
const KIGFX::COLOR4D aBackground,
const KIGFX::COLOR4D aDefault,
SWATCH_SIZE  aSwatchType 
)

Construct a COLOR_SWATCH.

Parameters
aParentparent window
aColorinitial swatch color
aIDid to use when sending swatch events

Definition at line 112 of file color_swatch.cpp.

114 :
115 wxPanel( aParent, aID ),
116 m_color( aColor ),
117 m_background( aBackground ),
118 m_default( aDefault ),
119 m_userColors( nullptr ),
120 m_readOnly( false ),
121 m_supportsOpacity( true )
122{
123 wxASSERT_MSG( aSwatchSize != SWATCH_EXPAND, "SWATCH_EXPAND not supported in COLOR_SWATCH" );
124
125 switch( aSwatchSize )
126 {
127 case SWATCH_MEDIUM: m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
128 case SWATCH_SMALL: m_size = ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
129 case SWATCH_LARGE: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
130 case SWATCH_EXPAND: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
131 }
132
133 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
134 m_checkerboardBg = aParent->GetBackgroundColour();
135
136 auto sizer = new wxBoxSizer( wxHORIZONTAL );
137 SetSizer( sizer );
138
139 wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( aColor, aBackground, m_size,
141 m_swatch = new wxStaticBitmap( this, aID, bitmap );
142
143 sizer->Add( m_swatch, 0, 0 );
144
145 setupEvents();
146}
wxSize m_checkerboardSize
Definition: color_swatch.h:147
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:148
KIGFX::COLOR4D m_default
Definition: color_swatch.h:141
bool m_supportsOpacity
If opacity is not supported the color chooser dialog will be displayed without it.
Definition: color_swatch.h:155
wxStaticBitmap * m_swatch
Definition: color_swatch.h:144
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:151
void setupEvents()
KIGFX::COLOR4D m_color
Definition: color_swatch.h:139
KIGFX::COLOR4D m_background
Definition: color_swatch.h:140
CUSTOM_COLORS_LIST * m_userColors
Definition: color_swatch.h:142
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
@ SWATCH_MEDIUM
Definition: color_swatch.h:41
@ SWATCH_LARGE
Definition: color_swatch.h:42
@ SWATCH_EXPAND
Definition: color_swatch.h:43
@ SWATCH_SMALL
Definition: color_swatch.h:40

References CHECKERBOARD_SIZE_DU(), m_checkerboardBg, m_checkerboardSize, m_size, m_swatch, MakeBitmap(), setupEvents(), SWATCH_EXPAND, SWATCH_LARGE, SWATCH_MEDIUM, SWATCH_SIZE_LARGE_DU(), SWATCH_SIZE_MEDIUM_DU(), SWATCH_SIZE_SMALL_DU(), and SWATCH_SMALL.

◆ COLOR_SWATCH() [2/2]

COLOR_SWATCH::COLOR_SWATCH ( wxWindow *  aParent,
wxWindowID  aId,
const wxPoint &  aPos = wxDefaultPosition,
const wxSize &  aSize = wxDefaultSize,
long  aStyle = 0 
)

constructor for wxFormBuilder

Definition at line 149 of file color_swatch.cpp.

150 :
151 wxPanel( aParent, aID, aPos, aSize, aStyle ),
152 m_userColors( nullptr ),
153 m_readOnly( false ),
154 m_supportsOpacity( true )
155{
156 if( aSize == wxDefaultSize )
157 m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU );
158 else
159 m_size = aSize;
160
161 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
162 m_checkerboardBg = aParent->GetBackgroundColour();
163
164 SetSize( m_size );
165
166 auto sizer = new wxBoxSizer( wxHORIZONTAL );
167 SetSizer( sizer );
168
169 wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( COLOR4D::UNSPECIFIED, COLOR4D::UNSPECIFIED,
171 m_swatch = new wxStaticBitmap( this, aID, bitmap );
172
173 sizer->Add( m_swatch, 0, 0 );
174
175 setupEvents();
176}

References CHECKERBOARD_SIZE_DU(), m_checkerboardBg, m_checkerboardSize, m_size, m_swatch, MakeBitmap(), setupEvents(), and SWATCH_SIZE_MEDIUM_DU().

Member Function Documentation

◆ GetNewSwatchColor()

void COLOR_SWATCH::GetNewSwatchColor ( )

Prompt for a new colour, using the colour picker dialog.

A colour change event will be sent if it's set.

Definition at line 267 of file color_swatch.cpp.

268{
269 if( m_readOnly )
270 {
273
274 return;
275 }
276
277 DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, m_supportsOpacity,
279
280 if( dialog.ShowModal() == wxID_OK )
281 {
282 COLOR4D newColor = dialog.GetColor();
283
284 if( newColor != COLOR4D::UNSPECIFIED || m_default == COLOR4D::UNSPECIFIED )
285 {
286 m_color = newColor;
287
288 wxBitmap bm = MakeBitmap( newColor, m_background, m_size, m_checkerboardSize,
290 m_swatch->SetBitmap( bm );
291
292 sendSwatchChangeEvent( *this );
293 }
294 }
295}
std::function< void()> m_readOnlyCallback
Definition: color_swatch.h:152
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)

References DIALOG_COLOR_PICKER::GetColor(), m_background, m_checkerboardBg, m_checkerboardSize, m_color, m_default, m_readOnly, m_readOnlyCallback, m_size, m_supportsOpacity, m_swatch, m_userColors, MakeBitmap(), and sendSwatchChangeEvent().

Referenced by DIALOG_LIB_SHAPE_PROPERTIES::onFill(), APPEARANCE_CONTROLS::onNetclassContextMenu(), LAYER_WIDGET::OnRightDownRender(), and setupEvents().

◆ GetSwatchColor()

◆ IsReadOnly()

bool COLOR_SWATCH::IsReadOnly ( ) const
inline

Definition at line 120 of file color_swatch.h.

120{ return m_readOnly; }

References m_readOnly.

◆ MakeBitmap()

wxBitmap COLOR_SWATCH::MakeBitmap ( const KIGFX::COLOR4D aColor,
const KIGFX::COLOR4D aBackground,
const wxSize &  aSize,
const wxSize &  aCheckerboardSize,
const KIGFX::COLOR4D aCheckerboardBackground 
)
static

Definition at line 39 of file color_swatch.cpp.

42{
43 wxBitmap bitmap( aSize );
44 wxBrush brush;
45 wxPen pen;
46 wxMemoryDC iconDC;
47
48 iconDC.SelectObject( bitmap );
49 brush.SetStyle( wxBRUSHSTYLE_SOLID );
50
51 if( aColor == COLOR4D::UNSPECIFIED )
52 {
53 // Draw a checkerboard
54 COLOR4D white;
55 COLOR4D black;
56 bool rowCycle;
57
58 if( aCheckerboardBackground.GetBrightness() > 0.4 )
59 {
60 white = COLOR4D::WHITE;
61 black = white.Darkened( 0.15 );
62 rowCycle = true;
63 }
64 else
65 {
66 black = COLOR4D::BLACK;
67 white = black.Brightened( 0.15 );
68 rowCycle = false;
69 }
70
71 for( int x = 0; x < aSize.x; x += aCheckerboardSize.x )
72 {
73 bool colCycle = rowCycle;
74
75 for( int y = 0; y < aSize.y; y += aCheckerboardSize.y )
76 {
77 COLOR4D color = colCycle ? black : white;
78 brush.SetColour( color.ToColour() );
79 pen.SetColour( color.ToColour() );
80
81 iconDC.SetBrush( brush );
82 iconDC.SetPen( pen );
83 iconDC.DrawRectangle( x, y, x + aCheckerboardSize.x, y + aCheckerboardSize.y );
84
85 colCycle = !colCycle;
86 }
87
88 rowCycle = !rowCycle;
89 }
90 }
91 else
92 {
93 brush.SetColour( aBackground.WithAlpha(1.0).ToColour() );
94 pen.SetColour( aBackground.WithAlpha(1.0).ToColour() );
95
96 iconDC.SetBrush( brush );
97 iconDC.SetPen( pen );
98 iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
99
100 brush.SetColour( aColor.ToColour() );
101 pen.SetColour( aColor.ToColour() );
102
103 iconDC.SetBrush( brush );
104 iconDC.SetPen( pen );
105 iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
106 }
107
108 return bitmap;
109}
int color
Definition: DXF_plotter.cpp:57
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:321
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition: color4d.h:292
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition: color4d.h:343
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:278
@ WHITE
Definition: color4d.h:48
@ BLACK
Definition: color4d.h:44

References BLACK, KIGFX::COLOR4D::Brightened(), color, KIGFX::COLOR4D::Darkened(), KIGFX::COLOR4D::GetBrightness(), WHITE, and KIGFX::COLOR4D::WithAlpha().

Referenced by COLOR_SWATCH(), GRID_CELL_COLOR_RENDERER::Draw(), GetNewSwatchColor(), DIALOG_COLOR_PICKER::initDefinedColors(), SetSwatchBackground(), SetSwatchColor(), ZONE_SETTINGS::SetupLayersList(), and DIALOG_COLOR_PICKER::updatePreview().

◆ rePostEvent()

void COLOR_SWATCH::rePostEvent ( wxEvent &  aEvent)
private

Pass unwanted events on to listeners of this object.

Definition at line 217 of file color_swatch.cpp.

218{
219 wxPostEvent( this, aEvent );
220}

Referenced by setupEvents().

◆ SetDefaultColor()

◆ SetReadOnly()

void COLOR_SWATCH::SetReadOnly ( bool  aReadOnly = true)
inline

Definition at line 119 of file color_swatch.h.

119{ m_readOnly = aReadOnly; }

References m_readOnly.

Referenced by APPEARANCE_CONTROLS::rebuildLayers().

◆ SetReadOnlyCallback()

void COLOR_SWATCH::SetReadOnlyCallback ( std::function< void()>  aCallback)
inline

Registers a handler for when the user tries to interact with a read-only swatch.

Definition at line 125 of file color_swatch.h.

125{ m_readOnlyCallback = aCallback; }

References m_readOnlyCallback.

Referenced by APPEARANCE_CONTROLS::rebuildLayers(), and APPEARANCE_CONTROLS::rebuildObjects().

◆ SetSupportsOpacity()

void COLOR_SWATCH::SetSupportsOpacity ( bool  aSupportsOpacity)
inline

Definition at line 122 of file color_swatch.h.

122{ m_supportsOpacity = aSupportsOpacity; }

References m_supportsOpacity.

Referenced by PANEL_3D_OPENGL_OPTIONS::PANEL_3D_OPENGL_OPTIONS(), and PANEL_3D_COLORS::TransferDataToWindow().

◆ SetSwatchBackground()

◆ SetSwatchColor()

void COLOR_SWATCH::SetSwatchColor ( const KIGFX::COLOR4D aColor,
bool  aSendEvent 
)

Set the current swatch color directly.

Definition at line 235 of file color_swatch.cpp.

236{
237 m_color = aColor;
238
240 m_swatch->SetBitmap( bm );
241
242 if( aSendEvent )
243 sendSwatchChangeEvent( *this );
244}

References m_background, m_checkerboardBg, m_checkerboardSize, m_color, m_size, m_swatch, MakeBitmap(), and sendSwatchChangeEvent().

Referenced by PROPERTIES_FRAME::CopyPrmsFromItemToPanel(), DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS(), PANEL_EESCHEMA_EDITING_OPTIONS::loadEEschemaSettings(), PANEL_3D_OPENGL_OPTIONS::loadSettings(), DIALOG_LIB_SHAPE_PROPERTIES::onBorderSwatch(), DIALOG_LIB_SHAPE_PROPERTIES::onFill(), DIALOG_JUNCTION_PROPS::resetDefaults(), DIALOG_LINE_PROPERTIES::resetDefaults(), DIALOG_WIRE_BUS_PROPERTIES::resetDefaults(), PANEL_3D_COLORS::ResetPanel(), PANEL_COLOR_SETTINGS::ResetPanel(), PANEL_3D_COLORS::TransferDataToWindow(), DIALOG_FIELD_PROPERTIES::TransferDataToWindow(), DIALOG_JUNCTION_PROPS::TransferDataToWindow(), DIALOG_LABEL_PROPERTIES::TransferDataToWindow(), DIALOG_LIB_SHAPE_PROPERTIES::TransferDataToWindow(), DIALOG_LIB_TEXT_PROPERTIES::TransferDataToWindow(), DIALOG_LIB_TEXTBOX_PROPERTIES::TransferDataToWindow(), DIALOG_LINE_PROPERTIES::TransferDataToWindow(), DIALOG_SHAPE_PROPERTIES::TransferDataToWindow(), DIALOG_SHEET_PIN_PROPERTIES::TransferDataToWindow(), DIALOG_SHEET_PROPERTIES::TransferDataToWindow(), DIALOG_TEXT_PROPERTIES::TransferDataToWindow(), and DIALOG_WIRE_BUS_PROPERTIES::TransferDataToWindow().

◆ setupEvents()

void COLOR_SWATCH::setupEvents ( )
private

Definition at line 179 of file color_swatch.cpp.

180{
181 wxWindow* topLevelParent = GetParent();
182
183 while( topLevelParent && !topLevelParent->IsTopLevel() )
184 topLevelParent = topLevelParent->GetParent();
185
186 if( topLevelParent && dynamic_cast<DIALOG_SHIM*>( topLevelParent ) )
187 {
188 m_swatch->Bind( wxEVT_LEFT_DOWN,
189 [this] ( wxMouseEvent& aEvt )
190 {
192 } );
193 }
194 else
195 {
196 // forward click to any other listeners, since we don't want them
197 m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
198
199 // bind the events that trigger the dialog
200 m_swatch->Bind( wxEVT_LEFT_DCLICK,
201 [this] ( wxMouseEvent& aEvt )
202 {
204 } );
205 }
206
207 m_swatch->Bind( wxEVT_MIDDLE_DOWN,
208 [this] ( wxMouseEvent& aEvt )
209 {
211 } );
212
213 m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
214}
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83

References GetNewSwatchColor(), m_swatch, and rePostEvent().

Referenced by COLOR_SWATCH().

◆ SetUserColors()

void COLOR_SWATCH::SetUserColors ( CUSTOM_COLORS_LIST aUserColors)
inline

Fetch a reference to the user colors list.

Definition at line 95 of file color_swatch.h.

95{ m_userColors = aUserColors; }

References m_userColors.

Referenced by PANEL_3D_COLORS::PANEL_3D_COLORS().

◆ SetWindowID()

void COLOR_SWATCH::SetWindowID ( wxWindowID  aId)
inline

Updates the window ID of this control and its children.

Parameters
aIdnew Window ID to set

Definition at line 106 of file color_swatch.h.

107 {
108 SetId( aId );
109 m_swatch->SetId( aId );
110 }

References m_swatch.

Member Data Documentation

◆ m_background

KIGFX::COLOR4D COLOR_SWATCH::m_background
private

Definition at line 140 of file color_swatch.h.

Referenced by GetNewSwatchColor(), SetSwatchBackground(), and SetSwatchColor().

◆ m_checkerboardBg

KIGFX::COLOR4D COLOR_SWATCH::m_checkerboardBg
private

◆ m_checkerboardSize

wxSize COLOR_SWATCH::m_checkerboardSize
private

◆ m_color

KIGFX::COLOR4D COLOR_SWATCH::m_color
private

◆ m_default

KIGFX::COLOR4D COLOR_SWATCH::m_default
private

Definition at line 141 of file color_swatch.h.

Referenced by GetNewSwatchColor(), and SetDefaultColor().

◆ m_readOnly

bool COLOR_SWATCH::m_readOnly
private

A read-only swatch won't show the color chooser dialog but otherwise works normally.

Definition at line 151 of file color_swatch.h.

Referenced by GetNewSwatchColor(), IsReadOnly(), and SetReadOnly().

◆ m_readOnlyCallback

std::function<void()> COLOR_SWATCH::m_readOnlyCallback
private

Definition at line 152 of file color_swatch.h.

Referenced by GetNewSwatchColor(), and SetReadOnlyCallback().

◆ m_size

wxSize COLOR_SWATCH::m_size
private

◆ m_supportsOpacity

bool COLOR_SWATCH::m_supportsOpacity
private

If opacity is not supported the color chooser dialog will be displayed without it.

Definition at line 155 of file color_swatch.h.

Referenced by GetNewSwatchColor(), and SetSupportsOpacity().

◆ m_swatch

wxStaticBitmap* COLOR_SWATCH::m_swatch
private

◆ m_userColors

CUSTOM_COLORS_LIST* COLOR_SWATCH::m_userColors
private

Definition at line 142 of file color_swatch.h.

Referenced by GetNewSwatchColor(), and SetUserColors().


The documentation for this class was generated from the following files: