KiCad PCB EDA Suite
COLOR_SWATCH Class Reference

Class representing 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, KIGFX::COLOR4D aColor, int aID, 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 (KIGFX::COLOR4D aColor, bool aSendEvent)
 Set the current swatch color directly. More...
 
void SetDefaultColor (KIGFX::COLOR4D aColor)
 Sets the color that will be chosen with the "Reset to Default" button in the chooser. More...
 
void SetSwatchBackground (KIGFX::COLOR4D aBackground)
 Set the swatch background color. More...
 
KIGFX::COLOR4D GetSwatchColor () const
 
void GetNewSwatchColor ()
 Prompt for a new colour, using the colour picker dialog. More...
 
void SetReadOnly (bool aReadOnly=true)
 
bool IsReadOnly () const
 
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 (KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
 Make a simple color swatch bitmap. More...
 

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
 
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
 

Detailed Description

Class representing a simple color swatch, of the kind used to set layer colors.

Definition at line 57 of file color_swatch.h.

Constructor & Destructor Documentation

◆ COLOR_SWATCH() [1/2]

COLOR_SWATCH::COLOR_SWATCH ( wxWindow *  aParent,
KIGFX::COLOR4D  aColor,
int  aID,
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 116 of file color_swatch.cpp.

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

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 150 of file color_swatch.cpp.

151  :
152  wxPanel( aParent, aID, aPos, aSize, aStyle ),
153  m_readOnly( false )
154 {
155  if( aSize == wxDefaultSize )
156  m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU );
157  else
158  m_size = aSize;
159 
160  m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
161  m_checkerboardBg = aParent->GetBackgroundColour();
162 
163  SetSize( m_size );
164 
165  auto sizer = new wxBoxSizer( wxHORIZONTAL );
166  SetSizer( sizer );
167 
168  wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( COLOR4D::UNSPECIFIED, COLOR4D::UNSPECIFIED,
170  m_swatch = new wxStaticBitmap( this, aID, bitmap );
171 
172  sizer->Add( m_swatch, 0, 0 );
173 
174  setupEvents();
175 }
void setupEvents()
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
Make a simple color swatch bitmap.
wxSize m_checkerboardSize
Definition: color_swatch.h:128
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:129
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:132
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)

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 266 of file color_swatch.cpp.

267 {
268  if( m_readOnly )
269  {
270  if( m_readOnlyCallback )
272 
273  return;
274  }
275 
276  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true, nullptr, m_default );
277 
278  if( dialog.ShowModal() == wxID_OK )
279  {
280  COLOR4D newColor = dialog.GetColor();
281 
282  if( newColor != COLOR4D::UNSPECIFIED || m_default == COLOR4D::UNSPECIFIED )
283  {
284  m_color = newColor;
285 
286  wxBitmap bm = MakeBitmap( newColor, m_background, m_size, m_checkerboardSize,
288  m_swatch->SetBitmap( bm );
289 
290  sendSwatchChangeEvent( *this );
291  }
292  }
293 }
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
Make a simple color swatch bitmap.
KIGFX::COLOR4D m_default
Definition: color_swatch.h:123
wxSize m_checkerboardSize
Definition: color_swatch.h:128
KIGFX::COLOR4D m_background
Definition: color_swatch.h:122
std::function< void()> m_readOnlyCallback
Definition: color_swatch.h:133
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:129
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:132
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
Definition: color_swatch.h:121
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100

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

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

◆ GetSwatchColor()

◆ IsReadOnly()

bool COLOR_SWATCH::IsReadOnly ( ) const
inline

Definition at line 105 of file color_swatch.h.

105 { return m_readOnly; }
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:132

References m_readOnly.

◆ MakeBitmap()

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

Make a simple color swatch bitmap.

Parameters
aWindow- window used as context for device-independent size

Definition at line 44 of file color_swatch.cpp.

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

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 216 of file color_swatch.cpp.

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

Referenced by setupEvents().

◆ SetDefaultColor()

void COLOR_SWATCH::SetDefaultColor ( KIGFX::COLOR4D  aColor)

Sets the color that will be chosen with the "Reset to Default" button in the chooser.

Definition at line 246 of file color_swatch.cpp.

247 {
248  m_default = aColor;
249 }
KIGFX::COLOR4D m_default
Definition: color_swatch.h:123

References m_default.

Referenced by DIALOG_EDIT_LINE_STYLE::DIALOG_EDIT_LINE_STYLE(), DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS(), DIALOG_JUNCTION_PROPS::DIALOG_JUNCTION_PROPS(), and DIALOG_SHEET_PROPERTIES::TransferDataToWindow().

◆ SetReadOnly()

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

Definition at line 104 of file color_swatch.h.

104 { m_readOnly = aReadOnly; }
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:132

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 108 of file color_swatch.h.

108 { m_readOnlyCallback = aCallback; }
std::function< void()> m_readOnlyCallback
Definition: color_swatch.h:133

References m_readOnlyCallback.

Referenced by APPEARANCE_CONTROLS::rebuildLayers().

◆ SetSwatchBackground()

void COLOR_SWATCH::SetSwatchBackground ( KIGFX::COLOR4D  aBackground)

Set the swatch background color.

Definition at line 252 of file color_swatch.cpp.

253 {
254  m_background = aBackground;
256  m_swatch->SetBitmap( bm );
257 }
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
Make a simple color swatch bitmap.
wxSize m_checkerboardSize
Definition: color_swatch.h:128
KIGFX::COLOR4D m_background
Definition: color_swatch.h:122
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:129
KIGFX::COLOR4D m_color
Definition: color_swatch.h:121

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

Referenced by LAYER_WIDGET::OnRenderSwatchChanged(), PANEL_EESCHEMA_EDITING_OPTIONS::TransferDataToWindow(), and DIALOG_SHEET_PROPERTIES::TransferDataToWindow().

◆ SetSwatchColor()

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

Set the current swatch color directly.

Definition at line 234 of file color_swatch.cpp.

235 {
236  m_color = aColor;
237 
239  m_swatch->SetBitmap( bm );
240 
241  if( aSendEvent )
242  sendSwatchChangeEvent( *this );
243 }
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
Make a simple color swatch bitmap.
wxSize m_checkerboardSize
Definition: color_swatch.h:128
KIGFX::COLOR4D m_background
Definition: color_swatch.h:122
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:129
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
Definition: color_swatch.h:121

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

Referenced by DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS(), DIALOG_JUNCTION_PROPS::resetDefaults(), DIALOG_EDIT_LINE_STYLE::resetDefaults(), PANEL_COLOR_SETTINGS::ResetPanel(), DIALOG_JUNCTION_PROPS::TransferDataToWindow(), PANEL_EESCHEMA_EDITING_OPTIONS::TransferDataToWindow(), DIALOG_EDIT_LINE_STYLE::TransferDataToWindow(), and DIALOG_SHEET_PROPERTIES::TransferDataToWindow().

◆ setupEvents()

void COLOR_SWATCH::setupEvents ( )
private

Definition at line 178 of file color_swatch.cpp.

179 {
180  wxWindow* topLevelParent = GetParent();
181 
182  while( topLevelParent && !topLevelParent->IsTopLevel() )
183  topLevelParent = topLevelParent->GetParent();
184 
185  if( topLevelParent && dynamic_cast<DIALOG_SHIM*>( topLevelParent ) )
186  {
187  m_swatch->Bind( wxEVT_LEFT_DOWN,
188  [this] ( wxMouseEvent& aEvt )
189  {
191  } );
192  }
193  else
194  {
195  // forward click to any other listeners, since we don't want them
196  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
197 
198  // bind the events that trigger the dialog
199  m_swatch->Bind( wxEVT_LEFT_DCLICK,
200  [this] ( wxMouseEvent& aEvt )
201  {
203  } );
204  }
205 
206  m_swatch->Bind( wxEVT_MIDDLE_DOWN,
207  [this] ( wxMouseEvent& aEvt )
208  {
210  } );
211 
212  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
213 }
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.

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

Referenced by COLOR_SWATCH().

Member Data Documentation

◆ m_background

KIGFX::COLOR4D COLOR_SWATCH::m_background
private

Definition at line 122 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 123 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 132 of file color_swatch.h.

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

◆ m_readOnlyCallback

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

Definition at line 133 of file color_swatch.h.

Referenced by GetNewSwatchColor(), and SetReadOnlyCallback().

◆ m_size

wxSize COLOR_SWATCH::m_size
private

◆ m_swatch

wxStaticBitmap* COLOR_SWATCH::m_swatch
private

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