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...
 
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...
 
KIGFX::COLOR4D m_default
 

The default color for the swatch

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

83  :
84  wxPanel( aParent, aID ),
85  m_color( aColor ),
86  m_background( aBackground ),
87  m_default( aDefault )
88 {
89  auto sizer = new wxBoxSizer( wxHORIZONTAL );
90  SetSizer( sizer );
91 
92  auto swatch = makeColorSwatch( this, m_color, m_background, aID );
93  m_swatch = swatch.release(); // hold a handle
94 
95  sizer->Add( m_swatch, 0, 0 );
96 
97  // forward click to any other listeners, since we don't want them
98  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
99  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
100 
101  // bind the events that trigger the dialog
102  m_swatch->Bind( wxEVT_LEFT_DCLICK, [this] ( wxMouseEvent& aEvt ) {
104  } );
105 
106  m_swatch->Bind( wxEVT_MIDDLE_DOWN, [this] ( wxMouseEvent& aEvt ) {
108  } );
109 }
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:90
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:87
wxStaticBitmap * m_swatch
Handle of the actual swatch shown
Definition: color_swatch.h:93
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:84

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

159 {
160  COLOR4D newColor = COLOR4D::UNSPECIFIED;
161 
162  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true, nullptr, m_default );
163 
164  if( dialog.ShowModal() == wxID_OK )
165  newColor = dialog.GetColor();
166 
167  if( newColor != COLOR4D::UNSPECIFIED )
168  {
169  m_color = newColor;
170 
171  wxBitmap bm = MakeBitmap( newColor, m_background, ConvertDialogToPixels( SWATCH_SIZE_DU ) );
172  m_swatch->SetBitmap( bm );
173 
174  sendSwatchChangeEvent( *this );
175  }
176 }
static const wxSize SWATCH_SIZE_DU(8, 6)
KIGFX::COLOR4D m_default
The default color for the swatch
Definition: color_swatch.h:90
KIGFX::COLOR4D m_background
The background colour to show the swatch over
Definition: color_swatch.h:87
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:93
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:84
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_default, 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 152 of file color_swatch.cpp.

153 {
154  return m_color;
155 }
KIGFX::COLOR4D m_color
The current colour of the swatch
Definition: color_swatch.h:84

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

113 {
114  wxPostEvent( this, aEvt );
115 }

Referenced by COLOR_SWATCH().

◆ SetSwatchBackground()

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

Set the swatch background color.

Definition at line 144 of file color_swatch.cpp.

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

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

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

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

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

◆ m_default

KIGFX::COLOR4D COLOR_SWATCH::m_default
private

The default color for the swatch

Definition at line 90 of file color_swatch.h.

Referenced by GetNewSwatchColor().

◆ m_swatch

wxStaticBitmap* COLOR_SWATCH::m_swatch
private

Handle of the actual swatch shown

Definition at line 93 of file color_swatch.h.

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


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