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,
38 const wxSize& aSize, const wxSize& aCheckerboardSize,
39 const COLOR4D& aCheckerboardBackground,
40 const std::vector<int>& aMargins )
41{
42 wxBitmap bitmap( aSize );
43 wxMemoryDC iconDC;
44
45 iconDC.SelectObject( bitmap );
46
47 RenderToDC( &iconDC, aColor, aBackground, aSize, aCheckerboardSize, aCheckerboardBackground, aMargins );
48
49 return bitmap;
50}
51
52
54{
56 ToPhys( m_size ), ToPhys( m_checkerboardSize ),
58
59 bitmap.SetScaleFactor( GetDPIScaleFactor() );
60 return bitmap;
61}
62
63
64void COLOR_SWATCH::RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground,
65 const wxRect& aRect, const wxSize& aCheckerboardSize,
66 const KIGFX::COLOR4D& aCheckerboardBackground, const std::vector<int>& aMargins )
67{
68 wxColor fg = aColor.ToColour();
69
70 wxBrush brush;
71 brush.SetStyle( wxBRUSHSTYLE_SOLID );
72
73 aDC->SetPen( *wxTRANSPARENT_PEN );
74
75 // Draw a checkerboard
76 COLOR4D white;
77 COLOR4D black;
78 bool rowCycle;
79
80 if( aColor == COLOR4D::UNSPECIFIED )
81 {
82 if( aCheckerboardBackground.GetBrightness() > 0.4 )
83 {
84 white = COLOR4D::WHITE;
85 black = white.Darkened( 0.15 );
86 rowCycle = true;
87 }
88 else
89 {
90 black = COLOR4D::BLACK;
91 white = black.Brightened( 0.15 );
92 rowCycle = false;
93 }
94 }
95 else
96 {
97 if( aBackground.GetBrightness() > 0.4 )
98 {
99 white = aBackground;
100 black = white.Darkened( 0.15 );
101 rowCycle = true;
102 }
103 else
104 {
105 black = COLOR4D::BLACK;
106 white = black.Brightened( 0.15 );
107 rowCycle = false;
108 }
109 }
110
111 for( int x = aRect.GetLeft(); x <= aRect.GetRight(); x += aCheckerboardSize.x )
112 {
113 bool colCycle = rowCycle;
114
115 for( int y = aRect.GetTop(); y <= aRect.GetBottom(); y += aCheckerboardSize.y )
116 {
117 wxColor bg = colCycle ? black.ToColour() : white.ToColour();
118
119 // Blend fg bg with the checkerboard
120 unsigned char r = wxColor::AlphaBlend( fg.Red(), bg.Red(), aColor.a );
121 unsigned char g = wxColor::AlphaBlend( fg.Green(), bg.Green(), aColor.a );
122 unsigned char b = wxColor::AlphaBlend( fg.Blue(), bg.Blue(), aColor.a );
123
124 brush.SetColour( r, g, b );
125
126 aDC->SetBrush( brush );
127 aDC->DrawRectangle( x, y, aCheckerboardSize.x, aCheckerboardSize.y );
128
129 colCycle = !colCycle;
130 }
131
132 rowCycle = !rowCycle;
133 }
134
135 aDC->SetBrush( *wxWHITE_BRUSH );
136
137 if( aMargins[0] )
138 aDC->DrawRectangle( 0, 0, aMargins[0], aRect.GetHeight() );
139
140 if( aMargins[1] )
141 aDC->DrawRectangle( 0, 0, aRect.GetWidth(), aMargins[1] );
142
143 if( aMargins[2] )
144 aDC->DrawRectangle( aRect.GetWidth() - aMargins[2], 0, aMargins[2], aRect.GetHeight() );
145
146 if( aMargins[3] )
147 aDC->DrawRectangle( 0, aRect.GetHeight() - aMargins[3], aRect.GetWidth(), aMargins[3] );
148}
149
150
151COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID,
152 const COLOR4D& aBackground, const COLOR4D& aDefault,
153 SWATCH_SIZE aSwatchSize, bool aTriggerWithSingleClick ) :
154 wxPanel( aParent, aID ),
155 m_color( aColor ),
156 m_background( aBackground ),
157 m_default( aDefault ),
158 m_userColors( nullptr ),
159 m_readOnly( false ),
160 m_supportsOpacity( true )
161{
162 wxASSERT_MSG( aSwatchSize != SWATCH_EXPAND,
163 wxS( "SWATCH_EXPAND not supported in COLOR_SWATCH" ) );
164
165 switch( aSwatchSize )
166 {
167 case SWATCH_MEDIUM: m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
168 case SWATCH_SMALL: m_size = ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
169 case SWATCH_LARGE: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
170 case SWATCH_EXPAND: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
171 }
172
173 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
174 m_checkerboardBg = aParent->GetBackgroundColour();
175
176 auto sizer = new wxBoxSizer( wxHORIZONTAL );
177 SetSizer( sizer );
178
179 m_swatch = new wxStaticBitmap( this, aID, makeBitmap() );
180
181 sizer->Add( m_swatch, 0, 0 );
182
183 setupEvents( aTriggerWithSingleClick );
184}
185
186
187COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, wxWindowID aID, const wxPoint& aPos,
188 const wxSize& aSize, long aStyle ) :
189 wxPanel( aParent, aID, aPos, aSize, aStyle ),
190 m_userColors( nullptr ),
191 m_readOnly( false ),
192 m_supportsOpacity( true )
193{
194 if( aSize == wxDefaultSize )
195 m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU );
196 else
197 m_size = aSize;
198
199 m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
200 m_checkerboardBg = aParent->GetBackgroundColour();
201
202#ifdef __WXMAC__
203 // Adjust for border
204 m_size.x -= 2;
205 m_size.y -= 2;
206#endif
207
208 SetSize( m_size );
209
210 auto sizer = new wxBoxSizer( wxHORIZONTAL );
211 SetSizer( sizer );
212
213 m_swatch = new wxStaticBitmap( this, aID, makeBitmap() );
214
215 sizer->Add( m_swatch, 0, 0 );
216
217 setupEvents( false );
218}
219
220
221void COLOR_SWATCH::setupEvents( bool aTriggerWithSingleClick )
222{
223 if( dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
224 {
225 m_swatch->Bind( wxEVT_LEFT_DOWN,
226 [this] ( wxMouseEvent& aEvt )
227 {
229 } );
230 }
231 else
232 {
233 // forward click to any other listeners, since we don't want them
234 m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
235
236 // bind the events that trigger the dialog
237 m_swatch->Bind( wxEVT_LEFT_DCLICK,
238 [this] ( wxMouseEvent& aEvt )
239 {
241 } );
242
243 if( aTriggerWithSingleClick )
244 {
245 m_swatch->Bind( wxEVT_LEFT_UP,
246 [this] ( wxMouseEvent& aEvt )
247 {
249 } );
250 }
251 }
252
253 m_swatch->Bind( wxEVT_MIDDLE_DOWN,
254 [this] ( wxMouseEvent& aEvt )
255 {
257 } );
258
259 m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
260}
261
262
263void COLOR_SWATCH::rePostEvent( wxEvent& aEvent )
264{
265 wxPostEvent( this, aEvent );
266}
267
268
269static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
270{
271 wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED, aSender.GetId() );
272
273 // use this class as the object (alternative might be to
274 // set a custom event class but that's more work)
275 changeEvt.SetEventObject( &aSender );
276
277 wxPostEvent( &aSender, changeEvt );
278}
279
280
281void COLOR_SWATCH::SetSwatchColor( const COLOR4D& aColor, bool aSendEvent )
282{
283 m_color = aColor;
284
285 m_swatch->SetBitmap( makeBitmap() );
286
287 if( aSendEvent )
288 sendSwatchChangeEvent( *this );
289}
290
291
293{
294 m_default = aColor;
295}
296
297
299{
300 m_background = aBackground;
301
302 m_swatch->SetBitmap( makeBitmap() );
303}
304
305
307{
308 return m_color;
309}
310
311
313{
314 if( m_readOnly )
315 {
318
319 return;
320 }
321
322 DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, m_supportsOpacity,
324
325 if( dialog.ShowModal() == wxID_OK )
326 {
327 COLOR4D newColor = dialog.GetColor();
328
330 {
331 m_color = newColor;
332
333 m_swatch->SetBitmap( makeBitmap() );
334
335 sendSwatchChangeEvent( *this );
336 }
337 }
338}
339
340
342{
343 wxWindow* parent = GetParent();
344 m_checkerboardBg = parent ? parent->GetBackgroundColour() : wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
345
346 if( m_swatch )
347 m_swatch->SetBitmap( makeBitmap() );
348}
static const COLOR4D WHITE
Definition color4d.h:401
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
static const COLOR4D BLACK
Definition color4d.h:402
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:104
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition color4d.h:282
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition color4d.h:333
double a
Alpha component.
Definition color4d.h:395
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition color4d.h:268
wxColour ToColour() const
Definition color4d.cpp:220
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