KiCad PCB EDA Suite
dialog_color_picker.h
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software: you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation, either version 3 of the License, or (at your
13  * option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef DIALOG_COLOR_PICKER_H
25 #define DIALOG_COLOR_PICKER_H
26 
27 
28 #include <gal/color4d.h>
29 #include "../../common/dialogs/dialog_color_picker_base.h"
30 
31 class COLOR_SWATCH;
32 
33 
38 {
40  wxString m_ColorName;
41 
42  CUSTOM_COLOR_ITEM( double red, double green, double blue, const wxString& aName )
43  {
44  m_Color.r = red;
45  m_Color.g = green;
46  m_Color.b = blue;
47  m_ColorName = aName;
48  }
49 
50  CUSTOM_COLOR_ITEM( double red, double green, double blue, double alpha, const wxString& aName )
51  {
52  m_Color.r = red;
53  m_Color.g = green;
54  m_Color.b = blue;
55  m_Color.a = alpha;
56  m_ColorName = aName;
57  }
58 
59  CUSTOM_COLOR_ITEM( const KIGFX::COLOR4D& aColor, const wxString& aName )
60  : m_Color( aColor ), m_ColorName( aName)
61  {}
62 };
63 
64 
65 typedef std::vector<CUSTOM_COLOR_ITEM> CUSTOM_COLORS_LIST;
66 
67 
69 {
79 };
80 
82 {
83 public:
93  DIALOG_COLOR_PICKER( wxWindow* aParent, const KIGFX::COLOR4D& aCurrentColor,
94  bool aAllowOpacityControl, CUSTOM_COLORS_LIST* aUserColors = nullptr,
95  const KIGFX::COLOR4D& aDefaultColor = KIGFX::COLOR4D::UNSPECIFIED );
97 
99 
100 private:
101  /* When the dialog is created, the mouse cursor can be on the RGB or HSV palette selector
102  * Because this dialog is created by clicking double clicking on a widget, the left mouse
103  * button is down, thus creating a not wanted mouse event inside this dialog
104  * m_allowMouseEvents is first set to false, and then set to true on the first left mouse
105  * clicking inside this dialog to prevent not wanted mouse drag event
106  */
107  bool m_allowMouseEvents;
113 
115  std::vector<KIGFX::COLOR4D> m_Color4DList;
117 
122  wxPoint* m_selectedCursor;
123 
124  double m_hue;
125  double m_sat;
126  double m_val;
127 
128  wxBitmap* m_bitmapRGB;
129  wxBitmap* m_bitmapHSV;
130 
131  std::vector<wxStaticBitmap*> m_colorSwatches;
132 
133  void SetEditVals( CHANGED_COLOR aChanged, bool aCheckTransparency );
134  void drawAll();
135 
136  void createHSVBitmap();
137  void drawHSVPalette();
138  void createRGBBitmap();
139  void drawRGBPalette();
140 
142  void updatePreview( wxStaticBitmap* aStaticBitmap, KIGFX::COLOR4D& aColor4D );
143 
145  void OnChangeBrightness( wxScrollEvent& event ) override;
146 
148  void OnChangeAlpha( wxScrollEvent& event ) override;
149 
151  void OnChangeEditRed( wxSpinEvent& event ) override;
152  void OnChangeEditGreen( wxSpinEvent& event ) override;
153  void OnChangeEditBlue( wxSpinEvent& event ) override;
154  void OnChangeEditHue( wxSpinEvent& event ) override;
155  void OnChangeEditSat( wxSpinEvent& event ) override;
156 
158  void onRGBMouseClick( wxMouseEvent& event ) override;
159  void onRGBMouseDrag( wxMouseEvent& event ) override;
160  void onHSVMouseClick( wxMouseEvent& event ) override;
161  void onHSVMouseDrag( wxMouseEvent& event ) override;
162 
163  void OnColorValueText( wxCommandEvent& event ) override;
164 
166  void OnResetButton( wxCommandEvent& aEvent ) override;
167 
176  bool setHSvaluesFromCursor( const wxPoint& aMouseCursor );
177 
179  void buttColorClick( wxMouseEvent& event );
180 
182  void colorDClick( wxMouseEvent& event );
183 
185  bool TransferDataToWindow() override;
186 
192  void initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColors );
193 
194  // convert double value 0 ... 1 to int 0 ... aValMax
195  int normalizeToInt( double aValue, int aValMax = 255 )
196  {
197  return ( aValue * aValMax ) + 0.5;
198  }
199 
200 };
201 
202 #endif // #define DIALOG_COLOR_PICKER_H
double m_hue
the current hue, in degrees (0 ... 360)
bool m_allowOpacityCtrl
true to show the widget, false to keep alpha channel = 1.0
double m_sat
the current saturation (0 ... 1.0)
CUSTOM_COLOR_ITEM(double red, double green, double blue, double alpha, const wxString &aName)
void drawRGBPalette()
draws the RVB color space
CUSTOM_COLOR_ITEM(const KIGFX::COLOR4D &aColor, const wxString &aName)
void OnChangeEditGreen(wxSpinEvent &event) override
std::vector< KIGFX::COLOR4D > m_Color4DList
the list of color4d ordered by button ID, for predefined colors
CHANGED_COLOR
void OnChangeAlpha(wxScrollEvent &event) override
Event handlers from wxSpinControl.
int normalizeToInt(double aValue, int aValMax=255)
void onHSVMouseDrag(wxMouseEvent &event) override
void OnColorValueText(wxCommandEvent &event) override
Event handler for the reset button press.
double g
Green component.
Definition: color4d.h:378
void drawHSVPalette()
draws the HSV color circle
A class to handle a custom color (predefined color) for the color picker dialog.
std::vector< CUSTOM_COLOR_ITEM > CUSTOM_COLORS_LIST
void createHSVBitmap()
generate the bitmap that shows the HSV color circle
void OnChangeBrightness(wxScrollEvent &event) override
Event handler from wxSlider: alpha (transparency) control.
KIGFX::COLOR4D m_newColor4D
the current color4d
bool setHSvaluesFromCursor(const wxPoint &aMouseCursor)
Manage the Hue and Saturation settings when the mouse cursor is at aMouseCursor.
void onHSVMouseClick(wxMouseEvent &event) override
wxPoint m_cursorBitmapHSV
the cursor on the HSV bitmap palette.
double b
Blue component.
Definition: color4d.h:379
void SetEditVals(CHANGED_COLOR aChanged, bool aCheckTransparency)
double a
Alpha component.
Definition: color4d.h:380
void onRGBMouseClick(wxMouseEvent &event) override
KIGFX::COLOR4D m_previousColor4D
the initial color4d
void createRGBBitmap()
generate the bitmap that shows the RVB color space
wxBitmap * m_bitmapHSV
the basic HUV palette
void OnChangeEditHue(wxSpinEvent &event) override
Class DIALOG_COLOR_PICKER_BASE.
std::vector< wxStaticBitmap * > m_colorSwatches
list of defined colors buttons
void colorDClick(wxMouseEvent &event)
called when creating the dialog
void buttColorClick(wxMouseEvent &event)
Event handler for double click on color buttons.
void onRGBMouseDrag(wxMouseEvent &event) override
void OnResetButton(wxCommandEvent &aEvent) override
double m_val
the current value (0 ... 1.0)
KIGFX::COLOR4D m_defaultColor
The default color4d.
CUSTOM_COLOR_ITEM(double red, double green, double blue, const wxString &aName)
wxPoint m_cursorBitmapRed
the red cursor on the RGB bitmap palette.
wxPoint m_cursorBitmapBlue
the blue cursor on the RGB bitmap palette.
void OnChangeEditBlue(wxSpinEvent &event) override
bool TransferDataToWindow() override
void initDefinedColors(CUSTOM_COLORS_LIST *aPredefinedColors)
Create the bitmap buttons for each defined colors.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:383
void updatePreview(wxStaticBitmap *aStaticBitmap, KIGFX::COLOR4D &aColor4D)
Event handler from wxSlider: brightness (value) control.
KIGFX::COLOR4D GetColor()
A simple color swatch of the kind used to set layer colors.
Definition: color_swatch.h:56
KIGFX::COLOR4D m_Color
void OnChangeEditRed(wxSpinEvent &event) override
wxBitmap * m_bitmapRGB
the basic RGB palette
DIALOG_COLOR_PICKER(wxWindow *aParent, const KIGFX::COLOR4D &aCurrentColor, bool aAllowOpacityControl, CUSTOM_COLORS_LIST *aUserColors=nullptr, const KIGFX::COLOR4D &aDefaultColor=KIGFX::COLOR4D::UNSPECIFIED)
Dialog constructor.
double r
Red component.
Definition: color4d.h:377
void OnChangeEditSat(wxSpinEvent &event) override
mouse handlers, when clicking on a palette bitmap
wxPoint m_cursorBitmapGreen
the green cursor on the RGB bitmap palette.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
wxPoint * m_selectedCursor
the ref cursor to the selected cursor, if any, or null.