KiCad PCB EDA Suite
Loading...
Searching...
No Matches
color_swatch.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <kiplatform/ui.h>
26#include <wx/dcmemory.h>
27
28#include <dpi_scaling_common.h>
30#include <memory>
31
32wxDEFINE_EVENT( COLOR_SWATCH_CHANGED, wxCommandEvent );
33
34using KIGFX::COLOR4D;
35
36
37wxBitmap COLOR_SWATCH::MakeBitmap( const COLOR4D& aColor, const COLOR4D& aBackground, const wxSize& aSize,
38 const wxSize& aCheckerboardSize, const COLOR4D& aCheckerboardBackground,
39 const std::vector<int>& aMargins )
40{
41 wxBitmap bitmap( aSize );
42 wxMemoryDC iconDC;
43
44 iconDC.SelectObject( bitmap );
45
46 RenderToDC( &iconDC, aColor, aBackground, aSize, aCheckerboardSize, aCheckerboardBackground, aMargins );
47
48 return bitmap;
49}
50
51
53{
54 wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( m_color, m_background, ToPhys( m_size ),
56
57 bitmap.SetScaleFactor( GetDPIScaleFactor() );
58 return bitmap;
59}
60
61
62void COLOR_SWATCH::RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground,
63 const wxRect& aRect, const wxSize& aCheckerboardSize,
64 const KIGFX::COLOR4D& aCheckerboardBackground, const std::vector<int>& aMargins )
65{
66 wxColor fg = aColor.m_text.has_value() ? COLOR4D::UNSPECIFIED.ToColour() : aColor.ToColour();
67
68 wxBrush brush;
69 brush.SetStyle( wxBRUSHSTYLE_SOLID );
70
71 aDC->SetPen( *wxTRANSPARENT_PEN );
72
73 // Draw a checkerboard
74 COLOR4D white;
75 COLOR4D black;
76 bool rowCycle;
77
78 if( aColor.m_text.has_value() || aColor == COLOR4D::UNSPECIFIED )
79 {
80 if( aCheckerboardBackground.GetBrightness() > 0.4 )
81 {
82 white = COLOR4D::WHITE;
83 black = white.Darkened( 0.15 );
84 rowCycle = true;
85 }
86 else
87 {
88 black = COLOR4D::BLACK;
89 white = black.Brightened( 0.15 );
90 rowCycle = false;
91 }
92 }
93 else
94 {
95 if( aBackground.GetBrightness() > 0.4 )
96 {
97 white = aBackground;
98 black = white.Darkened( 0.15 );
99 rowCycle = true;
100 }
101 else
102 {
103 black = COLOR4D::BLACK;
104 white = black.Brightened( 0.15 );
105 rowCycle = false;
106 }
107 }
108
109 for( int x = aRect.GetLeft(); x <= aRect.GetRight(); x += aCheckerboardSize.x )
110 {
111 bool colCycle = rowCycle;
112
113 for( int y = aRect.GetTop(); y <= aRect.GetBottom(); y += aCheckerboardSize.y )
114 {
115 wxColor bg = colCycle ? black.ToColour() : white.ToColour();
116
117 // Blend fg bg with the checkerboard
118 double alpha = (double)fg.Alpha() / 255.0;
119 unsigned char r = wxColor::AlphaBlend( fg.Red(), bg.Red(), alpha );
120 unsigned char g = wxColor::AlphaBlend( fg.Green(), bg.Green(), alpha );
121 unsigned char b = wxColor::AlphaBlend( fg.Blue(), bg.Blue(), alpha );
122
123 brush.SetColour( r, g, b );
124
125 aDC->SetBrush( brush );
126 aDC->DrawRectangle( x, y, aCheckerboardSize.x, aCheckerboardSize.y );
127
128 colCycle = !colCycle;
129 }
130
131 rowCycle = !rowCycle;
132 }
133
134 aDC->SetBrush( *wxWHITE_BRUSH );
135
136 if( aMargins[0] )
137 aDC->DrawRectangle( 0, 0, aMargins[0], aRect.GetHeight() );
138
139 if( aMargins[1] )
140 aDC->DrawRectangle( 0, 0, aRect.GetWidth(), aMargins[1] );
141
142 if( aMargins[2] )
143 aDC->DrawRectangle( aRect.GetWidth() - aMargins[2], 0, aMargins[2], aRect.GetHeight() );
144
145 if( aMargins[3] )
146 aDC->DrawRectangle( 0, aRect.GetHeight() - aMargins[3], aRect.GetWidth(), aMargins[3] );
147}
148
149
150COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID, const COLOR4D& aBackground,
151 const COLOR4D& aDefault, SWATCH_SIZE aSwatchSize, bool aTriggerWithSingleClick ) :
152 wxPanel( aParent, aID ),
153 m_color( aColor ),
154 m_background( aBackground ),
155 m_default( aDefault ),
156 m_userColors( nullptr ),
157 m_readOnly( false ),
158 m_supportsOpacity( true )
159{
160 wxASSERT_MSG( aSwatchSize != SWATCH_EXPAND,
161 wxS( "SWATCH_EXPAND not supported in COLOR_SWATCH" ) );
162
163 switch( aSwatchSize )
164 {
165 case SWATCH_MEDIUM: m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
166 case SWATCH_SMALL: m_size = ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
167 case SWATCH_LARGE: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
168 case SWATCH_EXPAND: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
169 }
170
171 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
172 m_checkerboardBg = aParent->GetBackgroundColour();
173
174 auto sizer = new wxBoxSizer( wxHORIZONTAL );
175 SetSizer( sizer );
176
177 m_swatch = new wxStaticBitmap( this, aID, makeBitmap() );
178
179 sizer->Add( m_swatch, 0, 0 );
180
181 setupEvents( aTriggerWithSingleClick );
182}
183
184
185COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, wxWindowID aID, const wxPoint& aPos, const wxSize& aSize,
186 long aStyle ) :
187 wxPanel( aParent, aID, aPos, aSize, aStyle ),
188 m_userColors( nullptr ),
189 m_readOnly( false ),
190 m_supportsOpacity( true )
191{
192 if( aSize == wxDefaultSize )
193 m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU );
194 else
195 m_size = aSize;
196
197 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
198 m_checkerboardBg = aParent->GetBackgroundColour();
199
200#ifdef __WXMAC__
201 // Adjust for border
202 m_size.x -= 2;
203 m_size.y -= 2;
204#endif
205
206 SetSize( m_size );
207
208 auto sizer = new wxBoxSizer( wxHORIZONTAL );
209 SetSizer( sizer );
210
211 m_swatch = new wxStaticBitmap( this, aID, makeBitmap() );
212
213 sizer->Add( m_swatch, 0, 0 );
214
215 setupEvents( false );
216}
217
218
219void COLOR_SWATCH::setupEvents( bool aTriggerWithSingleClick )
220{
221 if( dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
222 {
223 m_swatch->Bind( wxEVT_LEFT_DOWN,
224 [this] ( wxMouseEvent& aEvt )
225 {
227 } );
228 }
229 else
230 {
231 // forward click to any other listeners, since we don't want them
232 m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
233
234 // bind the events that trigger the dialog
235 m_swatch->Bind( wxEVT_LEFT_DCLICK,
236 [this] ( wxMouseEvent& aEvt )
237 {
239 } );
240
241 if( aTriggerWithSingleClick )
242 {
243 m_swatch->Bind( wxEVT_LEFT_UP,
244 [this] ( wxMouseEvent& aEvt )
245 {
247 } );
248 }
249 }
250
251 m_swatch->Bind( wxEVT_MIDDLE_DOWN,
252 [this] ( wxMouseEvent& aEvt )
253 {
255 } );
256
257 m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
258}
259
260
261void COLOR_SWATCH::rePostEvent( wxEvent& aEvent )
262{
263 wxPostEvent( this, aEvent );
264}
265
266
267static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
268{
269 wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED, aSender.GetId() );
270
271 // use this class as the object (alternative might be to
272 // set a custom event class but that's more work)
273 changeEvt.SetEventObject( &aSender );
274
275 wxPostEvent( &aSender, changeEvt );
276}
277
278
279void COLOR_SWATCH::SetSwatchColor( const COLOR4D& aColor, bool aSendEvent )
280{
281 m_color = aColor;
282
283 m_swatch->SetBitmap( makeBitmap() );
284
285 if( aSendEvent )
286 sendSwatchChangeEvent( *this );
287}
288
289
291{
292 m_default = aColor;
293}
294
295
297{
298 m_background = aBackground;
299
300 m_swatch->SetBitmap( makeBitmap() );
301}
302
303
305{
306 return m_color;
307}
308
309
311{
312 if( m_readOnly )
313 {
316
317 return;
318 }
319
320 DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, m_supportsOpacity, m_userColors, m_default );
321
322 if( dialog.ShowModal() == wxID_OK )
323 {
324 COLOR4D newColor = dialog.GetColor();
325
327 {
328 m_color = newColor;
329
330 m_swatch->SetBitmap( makeBitmap() );
331
332 sendSwatchChangeEvent( *this );
333 }
334 }
335}
336
337
339{
340 wxWindow* parent = GetParent();
341 m_checkerboardBg = parent ? parent->GetBackgroundColour() : wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
342
343 if( m_swatch )
344 m_swatch->SetBitmap( makeBitmap() );
345}
static const COLOR4D WHITE
Definition color4d.h:405
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
static const COLOR4D BLACK
Definition color4d.h:406
A simple color swatch of the kind used to set layer colors.
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
wxSize m_checkerboardSize
KIGFX::COLOR4D m_checkerboardBg
KIGFX::COLOR4D m_default
bool m_supportsOpacity
If opacity is not supported the color chooser dialog will be displayed without it.
wxStaticBitmap * m_swatch
static void RenderToDC(wxDC *aDC, const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxRect &aRect, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground, const std::vector< int > &aMargins={ 0, 0, 0, 0 })
void OnDarkModeToggle()
Respond to a change in the OS's DarkMode setting.
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground, const std::vector< int > &aMargins={ 0, 0, 0, 0 })
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
KIGFX::COLOR4D GetSwatchColor() const
wxBitmap makeBitmap()
COLOR_SWATCH(wxWindow *aParent, const KIGFX::COLOR4D &aColor, int aID, const KIGFX::COLOR4D &aBackground, const KIGFX::COLOR4D &aDefault, SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick=false)
Construct a COLOR_SWATCH.
KIGFX::COLOR4D m_color
KIGFX::COLOR4D m_background
void setupEvents(bool aTriggerWithSingleClick)
std::function< void()> m_readOnlyCallback
void SetDefaultColor(const KIGFX::COLOR4D &aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
void SetSwatchBackground(const KIGFX::COLOR4D &aBackground)
Set the swatch background color.
CUSTOM_COLORS_LIST * m_userColors
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
KIGFX::COLOR4D GetColor()
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:68
int ShowModal() override
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
std::optional< wxString > m_text
Definition color4d.h:399
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition color4d.h:283
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition color4d.h:334
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition color4d.h:269
wxColour ToColour() const
Definition color4d.cpp:225
wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
SWATCH_SIZE
@ SWATCH_MEDIUM
@ SWATCH_LARGE
@ SWATCH_EXPAND
@ SWATCH_SMALL