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