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)
 Construct a COLOR_SWATCH. 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 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...
 
wxStaticBitmap * m_swatch
 

Handle of the actual swatch shown

More...
 

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

COLOR_SWATCH::COLOR_SWATCH ( wxWindow *  aParent,
KIGFX::COLOR4D  aColor,
int  aID,
KIGFX::COLOR4D  aBackground 
)

Construct a COLOR_SWATCH.

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

Definition at line 82 of file color_swatch.cpp.

82  :
83  wxPanel( aParent, aID ),
84  m_color( aColor ),
85  m_background( aBackground )
86 {
87  auto sizer = new wxBoxSizer( wxHORIZONTAL );
88  SetSizer( sizer );
89 
90  auto swatch = makeColorSwatch( this, m_color, m_background, aID );
91  m_swatch = swatch.release(); // hold a handle
92 
93  sizer->Add( m_swatch, 0, 0 );
94 
95  // forward click to any other listeners, since we don't want them
96  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
97  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
98 
99  // bind the events that trigger the dialog
100  m_swatch->Bind( wxEVT_LEFT_DCLICK, [this] ( wxMouseEvent& aEvt ) {
102  } );
103 
104  m_swatch->Bind( wxEVT_MIDDLE_DOWN, [this] ( wxMouseEvent& aEvt ) {
106  } );
107 }
void rePostEvent(wxEvent &aEvt)
Pass unwanted events on to listeners of this object.
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:86
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:89
static std::unique_ptr< wxStaticBitmap > makeColorSwatch(wxWindow *aParent, COLOR4D aColor, COLOR4D aBackground, int aID)
Function makeColorButton creates a wxStaticBitmap and assigns it a solid color and a control ID.
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:83

References GetNewSwatchColor(), m_background, m_color, m_swatch, makeColorSwatch(), and rePostEvent().

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

157 {
158  COLOR4D newColor = COLOR4D::UNSPECIFIED;
159 
160  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true );
161 
162  if( dialog.ShowModal() == wxID_OK )
163  newColor = dialog.GetColor();
164 
165  if( newColor != COLOR4D::UNSPECIFIED )
166  {
167  m_color = newColor;
168 
169  wxBitmap bm = MakeBitmap( newColor, m_background, ConvertDialogToPixels( SWATCH_SIZE_DU ) );
170  m_swatch->SetBitmap( bm );
171 
172  sendSwatchChangeEvent( *this );
173  }
174 }
static const wxSize SWATCH_SIZE_DU(8, 6)
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:86
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:89
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:83
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39

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

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

◆ GetSwatchColor()

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

Definition at line 150 of file color_swatch.cpp.

151 {
152  return m_color;
153 }
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:83

References m_color.

Referenced by PCB_LAYER_WIDGET::SyncLayerAlphaIndicators().

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

46 {
47  wxBitmap bitmap( aSize );
48  wxBrush brush;
49  wxMemoryDC iconDC;
50 
51  iconDC.SelectObject( bitmap );
52 
53  brush.SetStyle( wxBRUSHSTYLE_SOLID );
54  brush.SetColour( aBackground.WithAlpha(1.0).ToColour() );
55  iconDC.SetBrush( brush );
56  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
57 
58  brush.SetColour( aColor.ToColour() );
59  iconDC.SetBrush( brush );
60  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
61 
62  return bitmap;
63 }
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:247

References KIGFX::COLOR4D::WithAlpha().

Referenced by GetNewSwatchColor(), makeColorSwatch(), 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 110 of file color_swatch.cpp.

111 {
112  wxPostEvent( this, aEvt );
113 }

Referenced by COLOR_SWATCH().

◆ SetSwatchBackground()

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

Set the swatch background color.

Definition at line 142 of file color_swatch.cpp.

143 {
144  m_background = aBackground;
145  wxBitmap bm = MakeBitmap( m_color, m_background, ConvertDialogToPixels( SWATCH_SIZE_DU ) );
146  m_swatch->SetBitmap( bm );
147 }
static const wxSize SWATCH_SIZE_DU(8, 6)
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:86
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:89
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:83

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

Referenced by LAYER_WIDGET::OnRenderSwatchChanged().

◆ SetSwatchColor()

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

Set the current swatch color directly.

Definition at line 128 of file color_swatch.cpp.

129 {
130  m_color = aColor;
131 
132  wxBitmap bm = MakeBitmap( m_color, m_background, ConvertDialogToPixels( SWATCH_SIZE_DU ) );
133  m_swatch->SetBitmap( bm );
134 
135  if( sendEvent )
136  {
137  sendSwatchChangeEvent( *this );
138  }
139 }
static const wxSize SWATCH_SIZE_DU(8, 6)
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:86
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:89
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:83

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

Member Data Documentation

◆ m_background

KIGFX::COLOR4D COLOR_SWATCH::m_background
private

The background colour to show the swatch over

Definition at line 86 of file color_swatch.h.

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

◆ m_color

KIGFX::COLOR4D COLOR_SWATCH::m_color
private

The current colour of the swatch

Definition at line 83 of file color_swatch.h.

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

◆ m_swatch

wxStaticBitmap* COLOR_SWATCH::m_swatch
private

Handle of the actual swatch shown

Definition at line 89 of file color_swatch.h.

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


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