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 {
78 };
79 
81 {
82 public:
92  DIALOG_COLOR_PICKER( wxWindow* aParent, const KIGFX::COLOR4D& aCurrentColor,
93  bool aAllowOpacityControl, CUSTOM_COLORS_LIST* aUserColors = nullptr,
94  const KIGFX::COLOR4D& aDefaultColor = KIGFX::COLOR4D::UNSPECIFIED );
96 
98 
99 private:
100  /* When the dialog is created, the mouse cursor can be on the RGB or HSV palette selector
101  * Because this dialog is created by clicking double clicking on a widget, the left mouse
102  * button is down, thus creating a not wanted mouse event inside this dialog
103  * m_allowMouseEvents is first set to false, and then set to true on the first left mouse
104  * clicking inside this dialog to prevent not wanted mouse drag event
105  */
106  bool m_allowMouseEvents;
112 
114  std::vector<KIGFX::COLOR4D> m_Color4DList;
116 
121  wxPoint* m_selectedCursor;
122 
123  double m_hue;
124  double m_sat;
125  double m_val;
126 
127  wxBitmap* m_bitmapRGB;
128  wxBitmap* m_bitmapHSV;
129 
130  std::vector<wxStaticBitmap*> m_colorSwatches;
131 
132  void SetEditVals( CHANGED_COLOR aChanged, bool aCheckTransparency );
133  void drawAll();
134 
135  void createHSVBitmap();
136  void drawHSVPalette();
137  void createRGBBitmap();
138  void drawRGBPalette();
139 
141  void updatePreview( wxStaticBitmap* aStaticBitmap, KIGFX::COLOR4D& aColor4D );
142 
144  void OnChangeBrightness( wxScrollEvent& event ) override;
145 
147  void OnChangeAlpha( wxScrollEvent& event ) override;
148 
150  void OnChangeEditRed( wxSpinEvent& event ) override;
151  void OnChangeEditGreen( wxSpinEvent& event ) override;
152  void OnChangeEditBlue( wxSpinEvent& event ) override;
153  void OnChangeEditHue( wxSpinEvent& event ) override;
154  void OnChangeEditSat( wxSpinEvent& event ) override;
155 
157  void onRGBMouseClick( wxMouseEvent& event ) override;
158  void onRGBMouseDrag( wxMouseEvent& event ) override;
159  void onHSVMouseClick( wxMouseEvent& event ) override;
160  void onHSVMouseDrag( wxMouseEvent& event ) override;
161 
162  void OnColorValueText( wxCommandEvent& event ) override;
163 
165  void OnResetButton( wxCommandEvent& aEvent ) override;
166 
175  bool setHSvaluesFromCursor( wxPoint aMouseCursor );
176 
178  void buttColorClick( wxMouseEvent& event );
179 
181  void colorDClick( wxMouseEvent& event );
182 
184  bool TransferDataToWindow() override;
185 
191  void initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColors );
192 
193  // convert double value 0 ... 1 to int 0 ... aValMax
194  int normalizeToInt( double aValue, int aValMax = 255 )
195  {
196  return ( aValue * aValMax ) + 0.5;
197  }
198 
199 };
200 
201 #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:364
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
bool setHSvaluesFromCursor(wxPoint aMouseCursor)
Manage the Hue and Saturation settings when the mouse cursor is at aMouseCursor.
void OnChangeBrightness(wxScrollEvent &event) override
Event handler from wxSlider: alpha (transparency) control.
KIGFX::COLOR4D m_newColor4D
the current color4d
void onHSVMouseClick(wxMouseEvent &event) override
wxPoint m_cursorBitmapHSV
the cursor on the HSV bitmap palette.
double b
Blue component.
Definition: color4d.h:365
void SetEditVals(CHANGED_COLOR aChanged, bool aCheckTransparency)
double a
Alpha component.
Definition: color4d.h:366
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:369
void updatePreview(wxStaticBitmap *aStaticBitmap, KIGFX::COLOR4D &aColor4D)
Event handler from wxSlider: brightness (value) control.
KIGFX::COLOR4D GetColor()
Class representing a simple color swatch, of the kind used to set layer colors.
Definition: color_swatch.h:57
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:363
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.