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, bool aForDialog)
 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 sendEvent)
 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...
 

Static Public Member Functions

static wxBitmap MakeBitmap (KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
 Make a simple color swatch bitmap. More...
 

Private Member Functions

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

Private Attributes

KIGFX::COLOR4D m_color
 

The current colour of the swatch

More...
 
KIGFX::COLOR4D m_background
 

The background colour to show the swatch over

More...
 
KIGFX::COLOR4D m_default
 

The default color for the swatch

More...
 
wxStaticBitmap * m_swatch
 

Handle of the actual swatch shown

More...
 
wxSize m_size
 

Detailed Description

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

Definition at line 38 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,
bool  aForDialog 
)

Construct a COLOR_SWATCH.

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

Definition at line 73 of file color_swatch.cpp.

74  :
75  wxPanel( aParent, aID ),
76  m_color( aColor ),
77  m_background( aBackground ),
78  m_default( aDefault )
79 {
80  if( aForDialog )
81  m_size = ConvertDialogToPixels( DIALOG_SWATCH_SIZE_DU );
82  else
83  m_size = ConvertDialogToPixels( PALETTE_SWATCH_SIZE_DU );
84 
85  auto sizer = new wxBoxSizer( wxHORIZONTAL );
86  SetSizer( sizer );
87 
88  wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( aColor, aBackground, m_size );
89  m_swatch = new wxStaticBitmap( this, aID, bitmap );
90 
91  sizer->Add( m_swatch, 0, 0 );
92 
93  setupEvents();
94 }
void setupEvents()
static const wxSize PALETTE_SWATCH_SIZE_DU(8, 6)
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:102
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:99
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
Make a simple color swatch bitmap.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:96
static const wxSize DIALOG_SWATCH_SIZE_DU(24, 10)

References DIALOG_SWATCH_SIZE_DU(), m_size, m_swatch, MakeBitmap(), PALETTE_SWATCH_SIZE_DU(), and setupEvents().

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

98  :
99  wxPanel( aParent, aID, aPos, aSize, aStyle )
100 {
101  if( aSize == wxDefaultSize )
102  m_size = ConvertDialogToPixels( DIALOG_SWATCH_SIZE_DU );
103  else
104  m_size = aSize;
105 
106  SetSize( m_size );
107 
108  auto sizer = new wxBoxSizer( wxHORIZONTAL );
109  SetSizer( sizer );
110 
113  m_swatch = new wxStaticBitmap( this, aID, bitmap );
114 
115  sizer->Add( m_swatch, 0, 0 );
116 
117  setupEvents();
118 }
void setupEvents()
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:372
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
Make a simple color swatch bitmap.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
static const wxSize DIALOG_SWATCH_SIZE_DU(24, 10)

References DIALOG_SWATCH_SIZE_DU(), m_size, m_swatch, MakeBitmap(), setupEvents(), and KIGFX::COLOR4D::UNSPECIFIED.

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

211 {
212  COLOR4D newColor = COLOR4D::UNSPECIFIED;
213 
214  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true, nullptr, m_default );
215 
216  if( dialog.ShowModal() == wxID_OK )
217  newColor = dialog.GetColor();
218 
219  if( newColor != COLOR4D::UNSPECIFIED || m_default == COLOR4D::UNSPECIFIED )
220  {
221  m_color = newColor;
222 
223  wxBitmap bm = MakeBitmap( newColor, m_background, m_size );
224  m_swatch->SetBitmap( bm );
225 
226  sendSwatchChangeEvent( *this );
227  }
228 }
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:102
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:99
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
Make a simple color swatch bitmap.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:96
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99

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

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

◆ GetSwatchColor()

◆ MakeBitmap()

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

Make a simple color swatch bitmap.

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

Definition at line 46 of file color_swatch.cpp.

47 {
48  wxBitmap bitmap( aSize );
49  wxBrush brush;
50  wxMemoryDC iconDC;
51 
52  iconDC.SelectObject( bitmap );
53 
54  brush.SetStyle( wxBRUSHSTYLE_SOLID );
55  brush.SetColour( aBackground.WithAlpha(1.0).ToColour() );
56  iconDC.SetBrush( brush );
57  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
58 
59  if( aColor == COLOR4D::UNSPECIFIED )
60  {
61  aColor = aBackground.Inverted();
62  brush.SetStyle( wxBRUSHSTYLE_BDIAGONAL_HATCH );
63  }
64 
65  brush.SetColour( aColor.ToColour() );
66  iconDC.SetBrush( brush );
67  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
68 
69  return bitmap;
70 }
COLOR4D Inverted() const
Function Inverted Returns an inverted color, alpha remains the same.
Definition: color4d.h:319
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:307

References KIGFX::COLOR4D::Inverted(), and KIGFX::COLOR4D::WithAlpha().

Referenced by COLOR_SWATCH(), GetNewSwatchColor(), SetSwatchBackground(), SetSwatchColor(), and ZONE_SETTINGS::SetupLayersList().

◆ rePostEvent()

void COLOR_SWATCH::rePostEvent ( wxEvent &  aEvt)
private

Pass unwanted events on to listeners of this object.

Definition at line 159 of file color_swatch.cpp.

160 {
161  wxPostEvent( this, aEvt );
162 }

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

191 {
192  m_default = aColor;
193 }
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:102

References m_default.

Referenced by DIALOG_SCH_SHEET_PROPS::TransferDataToWindow().

◆ SetSwatchBackground()

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

Set the swatch background color.

Definition at line 196 of file color_swatch.cpp.

197 {
198  m_background = aBackground;
199  wxBitmap bm = MakeBitmap( m_color, m_background, m_size );
200  m_swatch->SetBitmap( bm );
201 }
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:99
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
Make a simple color swatch bitmap.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:96

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

Referenced by LAYER_WIDGET::OnRenderSwatchChanged(), PCB_LAYER_WIDGET::SyncLayerColors(), PANEL_EESCHEMA_SETTINGS::TransferDataToWindow(), and DIALOG_SCH_SHEET_PROPS::TransferDataToWindow().

◆ SetSwatchColor()

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

Set the current swatch color directly.

Definition at line 177 of file color_swatch.cpp.

178 {
179  m_color = aColor;
180 
181  wxBitmap bm = MakeBitmap( m_color == COLOR4D::UNSPECIFIED ? m_default : m_color,
182  m_background, m_size );
183  m_swatch->SetBitmap( bm );
184 
185  if( sendEvent )
186  sendSwatchChangeEvent( *this );
187 }
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:102
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:99
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize)
Make a simple color swatch bitmap.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:96

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

Referenced by PANEL_COLOR_SETTINGS::ResetPanel(), PCB_LAYER_WIDGET::SyncLayerColors(), PANEL_EESCHEMA_SETTINGS::TransferDataToWindow(), and DIALOG_SCH_SHEET_PROPS::TransferDataToWindow().

◆ setupEvents()

void COLOR_SWATCH::setupEvents ( )
private

Definition at line 121 of file color_swatch.cpp.

122 {
123  wxWindow* topLevelParent = GetParent();
124 
125  while( topLevelParent && !topLevelParent->IsTopLevel() )
126  topLevelParent = topLevelParent->GetParent();
127 
128  if( topLevelParent && dynamic_cast<DIALOG_SHIM*>( topLevelParent ) )
129  {
130  m_swatch->Bind( wxEVT_LEFT_DOWN,
131  [this] ( wxMouseEvent& aEvt )
132  {
134  } );
135  }
136  else
137  {
138  // forward click to any other listeners, since we don't want them
139  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
140 
141  // bind the events that trigger the dialog
142  m_swatch->Bind( wxEVT_LEFT_DCLICK,
143  [this] ( wxMouseEvent& aEvt )
144  {
146  } );
147  }
148 
149  m_swatch->Bind( wxEVT_MIDDLE_DOWN,
150  [this] ( wxMouseEvent& aEvt )
151  {
153  } );
154 
155  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
156 }
void rePostEvent(wxEvent &aEvt)
Pass unwanted events on to listeners of this object.
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:105
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

The background colour to show the swatch over

Definition at line 99 of file color_swatch.h.

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

◆ m_color

KIGFX::COLOR4D COLOR_SWATCH::m_color
private

The current colour of the swatch

Definition at line 96 of file color_swatch.h.

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

◆ m_default

KIGFX::COLOR4D COLOR_SWATCH::m_default
private

The default color for the swatch

Definition at line 102 of file color_swatch.h.

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

◆ m_size

wxSize COLOR_SWATCH::m_size
private

◆ m_swatch

wxStaticBitmap* COLOR_SWATCH::m_swatch
private

Handle of the actual swatch shown

Definition at line 105 of file color_swatch.h.

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


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