KiCad PCB EDA Suite
Loading...
Searching...
No Matches
color_swatch.h
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 (C) 2017-2024 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#ifndef COLOR_SWATCH__H_
25#define COLOR_SWATCH__H_
26
27#include <wx/bitmap.h>
28#include <wx/gdicmn.h>
29#include <wx/panel.h>
30#include <wx/statbmp.h>
31#include <wx/window.h>
32
33#include <functional>
34
35#include <gal/color4d.h>
37
39{
44};
45
46
47const static wxSize SWATCH_SIZE_SMALL_DU( 8, 6 );
48const static wxSize SWATCH_SIZE_MEDIUM_DU( 24, 10 );
49const static wxSize SWATCH_SIZE_LARGE_DU( 24, 16 );
50const static wxSize CHECKERBOARD_SIZE_DU( 3, 3 );
51
52
56class COLOR_SWATCH: public wxPanel
57{
58public:
59
67 COLOR_SWATCH( wxWindow* aParent, const KIGFX::COLOR4D& aColor, int aID,
68 const KIGFX::COLOR4D& aBackground, const KIGFX::COLOR4D& aDefault,
69 SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick = false );
70
74 COLOR_SWATCH( wxWindow *aParent, wxWindowID aId, const wxPoint &aPos = wxDefaultPosition,
75 const wxSize &aSize = wxDefaultSize, long aStyle = 0 );
76
80 void SetSwatchColor( const KIGFX::COLOR4D& aColor, bool aSendEvent );
81
85 void SetDefaultColor( const KIGFX::COLOR4D& aColor );
86
90 void SetSwatchBackground( const KIGFX::COLOR4D& aBackground );
91
95 void SetUserColors( CUSTOM_COLORS_LIST* aUserColors ) { m_userColors = aUserColors; }
96
101
106 void SetWindowID( wxWindowID aId )
107 {
108 SetId( aId );
109 m_swatch->SetId( aId );
110 }
111
117 void GetNewSwatchColor();
118
119 void SetReadOnly( bool aReadOnly = true ) { m_readOnly = aReadOnly; }
120 bool IsReadOnly() const { return m_readOnly; }
121
122 void SetSupportsOpacity( bool aSupportsOpacity ) { m_supportsOpacity = aSupportsOpacity; }
123
125 void SetReadOnlyCallback( std::function<void()> aCallback ) { m_readOnlyCallback = aCallback; }
126
128 void OnDarkModeToggle();
129
130 static wxBitmap MakeBitmap( const KIGFX::COLOR4D& aColor, const KIGFX::COLOR4D& aBackground,
131 const wxSize& aSize, const wxSize& aCheckerboardSize,
132 const KIGFX::COLOR4D& aCheckerboardBackground );
133
134 static void RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor,
135 const KIGFX::COLOR4D& aBackground, const wxRect& aRect,
136 const wxSize& aCheckerboardSize,
137 const KIGFX::COLOR4D& aCheckerboardBackground );
138
139private:
140 void setupEvents( bool aTriggerWithSingleClick );
141
142 wxBitmap makeBitmap();
143
147 void rePostEvent( wxEvent& aEvent );
148
153
154 wxStaticBitmap* m_swatch;
155
156 wxSize m_size;
159
162 std::function<void()> m_readOnlyCallback;
163
166};
167
168
172wxDECLARE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent);
173
174#endif // COLOR_SWATCH__H_
A simple color swatch of the kind used to set layer colors.
Definition: color_swatch.h:57
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
wxSize m_checkerboardSize
Definition: color_swatch.h:157
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:158
KIGFX::COLOR4D m_default
Definition: color_swatch.h:151
bool m_supportsOpacity
If opacity is not supported the color chooser dialog will be displayed without it.
Definition: color_swatch.h:165
wxStaticBitmap * m_swatch
Definition: color_swatch.h:154
void OnDarkModeToggle()
Respond to a change in the OS's DarkMode setting.
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
void SetSupportsOpacity(bool aSupportsOpacity)
Definition: color_swatch.h:122
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:161
KIGFX::COLOR4D GetSwatchColor() const
wxBitmap makeBitmap()
void SetReadOnlyCallback(std::function< void()> aCallback)
Registers a handler for when the user tries to interact with a read-only swatch.
Definition: color_swatch.h:125
bool IsReadOnly() const
Definition: color_swatch.h:120
KIGFX::COLOR4D m_color
Definition: color_swatch.h:149
KIGFX::COLOR4D m_background
Definition: color_swatch.h:150
static void RenderToDC(wxDC *aDC, const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxRect &aRect, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
void setupEvents(bool aTriggerWithSingleClick)
void SetReadOnly(bool aReadOnly=true)
Definition: color_swatch.h:119
std::function< void()> m_readOnlyCallback
Definition: color_swatch.h:162
void SetUserColors(CUSTOM_COLORS_LIST *aUserColors)
Fetch a reference to the user colors list.
Definition: color_swatch.h:95
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
Definition: color_swatch.h:152
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
void SetWindowID(wxWindowID aId)
Updates the window ID of this control and its children.
Definition: color_swatch.h:106
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
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
Definition: color_swatch.h:39
@ SWATCH_MEDIUM
Definition: color_swatch.h:41
@ SWATCH_LARGE
Definition: color_swatch.h:42
@ SWATCH_EXPAND
Definition: color_swatch.h:43
@ SWATCH_SMALL
Definition: color_swatch.h:40
wxDECLARE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
Event signaling a swatch has changed color.
std::vector< CUSTOM_COLOR_ITEM > CUSTOM_COLORS_LIST