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=KIGFX::COLOR4D::UNSPECIFIED)
 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 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 = KIGFX::COLOR4D::UNSPECIFIED 
)

Construct a COLOR_SWATCH.

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

Definition at line 67 of file color_swatch.cpp.

68  :
69  wxPanel( aParent, aID ),
70  m_color( aColor ),
71  m_background( aBackground ),
72  m_default( aDefault )
73 {
74  m_size = ConvertDialogToPixels( PALETTE_SWATCH_SIZE_DU );
75 
76  auto sizer = new wxBoxSizer( wxHORIZONTAL );
77  SetSizer( sizer );
78 
79  wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( aColor, aBackground, m_size );
80  m_swatch = new wxStaticBitmap( this, aID, bitmap );
81 
82  sizer->Add( m_swatch, 0, 0 );
83 
84  setupEvents();
85 }
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:97
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:94
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:100
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:91

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

89  :
90  wxPanel( aParent, aID, aPos, aSize, aStyle )
91 {
92  if( aSize == wxDefaultSize )
93  m_size = ConvertDialogToPixels( DIALOG_SWATCH_SIZE_DU );
94  else
95  m_size = aSize;
96 
97  SetSize( m_size );
98 
99  auto sizer = new wxBoxSizer( wxHORIZONTAL );
100  SetSizer( sizer );
101 
104  m_swatch = new wxStaticBitmap( this, aID, bitmap );
105 
106  sizer->Add( m_swatch, 0, 0 );
107 
108  setupEvents();
109 }
void setupEvents()
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:308
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:100
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 177 of file color_swatch.cpp.

178 {
179  COLOR4D newColor = COLOR4D::UNSPECIFIED;
180 
181  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true, nullptr, m_default );
182 
183  if( dialog.ShowModal() == wxID_OK )
184  newColor = dialog.GetColor();
185 
186  if( newColor != COLOR4D::UNSPECIFIED )
187  {
188  m_color = newColor;
189 
190  wxBitmap bm = MakeBitmap( newColor, m_background, m_size );
191  m_swatch->SetBitmap( bm );
192 
193  sendSwatchChangeEvent( *this );
194  }
195 }
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:97
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:94
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:100
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:91
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40

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

COLOR4D COLOR_SWATCH::GetSwatchColor ( ) const
Returns
the current swatch color

Definition at line 171 of file color_swatch.cpp.

172 {
173  return m_color;
174 }
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:91

References m_color.

Referenced by PCB_LAYER_WIDGET::SyncLayerAlphaIndicators(), and DIALOG_SCH_SHEET_PROPS::TransferDataFromWindow().

◆ 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  brush.SetColour( aColor.ToColour() );
60  iconDC.SetBrush( brush );
61  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
62 
63  return bitmap;
64 }
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:248

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

134 {
135  wxPostEvent( this, aEvt );
136 }

Referenced by setupEvents().

◆ SetSwatchBackground()

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

Set the swatch background color.

Definition at line 163 of file color_swatch.cpp.

164 {
165  m_background = aBackground;
166  wxBitmap bm = MakeBitmap( m_color, m_background, m_size );
167  m_swatch->SetBitmap( bm );
168 }
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:94
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:100
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:91

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

Referenced by LAYER_WIDGET::OnRenderSwatchChanged(), 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 151 of file color_swatch.cpp.

152 {
153  m_color = aColor;
154 
155  wxBitmap bm = MakeBitmap( m_color, m_background, m_size );
156  m_swatch->SetBitmap( bm );
157 
158  if( sendEvent )
159  sendSwatchChangeEvent( *this );
160 }
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:94
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:100
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:91

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

Referenced by DIALOG_SCH_SHEET_PROPS::TransferDataToWindow().

◆ setupEvents()

void COLOR_SWATCH::setupEvents ( )
private

Definition at line 112 of file color_swatch.cpp.

113 {
114  // forward click to any other listeners, since we don't want them
115  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
116  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
117 
118  // bind the events that trigger the dialog
119  m_swatch->Bind( wxEVT_LEFT_DCLICK,
120  [this] ( wxMouseEvent& aEvt )
121  {
123  } );
124 
125  m_swatch->Bind( wxEVT_MIDDLE_DOWN,
126  [this] ( wxMouseEvent& aEvt )
127  {
129  } );
130 }
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:100
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 94 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 91 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 97 of file color_swatch.h.

Referenced by GetNewSwatchColor().

◆ 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 100 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: