neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
color_dialog.hpp
Go to the documentation of this file.
1// color_dialog.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2015, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (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 <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
23#include <neogfx/gfx/color.hpp>
29
30namespace neogfx
31{
32 class color_dialog : public dialog
33 {
35 public:
36 define_event(SelectionChanged, selection_changed)
37 protected:
38 define_event(ColorSpaceChanged, color_space_changed)
39 public:
40 enum mode_e
41 {
42 ModeHSV,
43 ModeRGB
44 };
45 enum channel_e
46 {
47 ChannelHue,
48 ChannelSaturation,
49 ChannelValue,
50 ChannelRed,
51 ChannelGreen,
52 ChannelBlue,
53 ChannelAlpha
54 };
55 typedef std::array<optional_color, 24> custom_color_list;
56 private:
57 typedef std::variant<std::monostate, color, hsv_color> representations;
58 typedef std::optional<custom_color_list::iterator> optional_custom_color_list_iterator;
59 class color_box : public framed_widget<>
60 {
61 typedef framed_widget<> base_type;
62 public:
63 color_box(color_dialog& aOwner, const optional_color& aColor, const optional_custom_color_list_iterator& aCustomColor = optional_custom_color_list_iterator());
64 public:
65 virtual size minimum_size(optional_size const& aAvailableSpace = optional_size{}) const;
66 virtual size maximum_size(optional_size const& aAvailableSpace = optional_size{}) const;
67 public:
68 virtual void paint(i_graphics_context& aGc) const;
69 public:
70 virtual void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers);
71 private:
72 color_dialog& iOwner;
73 optional_color iColor;
74 optional_custom_color_list_iterator iCustomColor;
75 };
76 class x_picker : public framed_widget<>
77 {
78 typedef framed_widget<> base_type;
79 private:
80 class cursor_widget : public image_widget
81 {
82 public:
83 enum type_e
84 {
85 LeftCursor,
86 RightCursor
87 };
88 public:
89 cursor_widget(x_picker& aParent, type_e aType);
90 public:
91 void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
92 void mouse_button_released(mouse_button aButton, const point& aPosition) override;
93 void mouse_moved(const point& aPosition, key_modifiers_e aKeyModifiers) override;
94 private:
95 x_picker& iParent;
96 optional_point iDragOffset;
97 };
98 public:
99 x_picker(color_dialog& aOwner);
100 public:
101 scalar cursor_width() const;
102 public:
103 size minimum_size(optional_size const& aAvailableSpace = optional_size{}) const override;
104 size maximum_size(optional_size const& aAvailableSpace = optional_size{}) const override;
105 public:
106 void moved() override;
107 void resized() override;
108 public:
109 void paint(i_graphics_context& aGc) const override;
110 public:
111 void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
112 void mouse_button_released(mouse_button aButton, const point& aPosition) override;
113 void mouse_moved(const point& aPosition, key_modifiers_e aKeyModifiers) override;
114 public:
115 neogfx::mouse_cursor mouse_cursor() const override;
116 public:
118 private:
119 void select(const point& aPosition);
120 representations color_at_position(const point& aCursorPos) const;
121 void update_cursors();
122 point current_cursor_position() const;
123 private:
124 color_dialog& iOwner;
125 sink iSink;
126 bool iTracking;
127 cursor_widget iLeftCursor;
128 cursor_widget iRightCursor;
129 };
130 class yz_picker : public framed_scrollable_widget
131 {
132 private:
133 static scalar constexpr CURSOR_RADIUS = 4.0;
134 static scalar constexpr CURSOR_THICKNESS = 1.5;
135 public:
136 yz_picker(color_dialog& aOwner);
137 public:
138 void set_image(image&& aImage);
139 void clear_image();
140 public:
141 void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
142 void mouse_button_released(mouse_button aButton, const point& aPosition) override;
143 void mouse_moved(const point& aPosition, key_modifiers_e aKeyModifiers) override;
144 public:
145 neogfx::mouse_cursor mouse_cursor() const override;
146 private:
147 void select(const point& aPosition);
148 representations color_at_position(const point& aCursorPos) const;
149 point current_cursor_position() const;
150 void update_texture();
151 void animate();
152 private:
153 color_dialog& iOwner;
154 vertical_layout iLayout;
155 std::optional<image> iImage;
156 mutable std::array<std::array<avec4u8, 256>, 256> iPixels;
157 image_widget iCanvas;
158 mutable texture iTexture;
159 bool iTracking;
160 optional_point iCursorPosition;
161 widget_timer iAnimationTimer;
162 };
163 class color_selection : public framed_widget<>
164 {
165 typedef framed_widget<> base_type;
166 public:
167 color_selection(color_dialog& aOwner);
168 public:
169 size minimum_size(optional_size const& aAvailableSpace = optional_size{}) const override;
170 size maximum_size(optional_size const& aAvailableSpace = optional_size{}) const override;
171 public:
172 void paint(i_graphics_context& aGc) const override;
173 private:
174 color_dialog& iOwner;
175 };
176 public:
177 color_dialog(const color& aCurrentColor = color::Black);
178 color_dialog(i_widget& aParent, const color& aCurrentColor = color::Black);
180 public:
184 void select_color(const color& aColor);
185 const custom_color_list& custom_colors() const;
186 void set_custom_colors(const custom_color_list& aCustomColors);
187 protected:
188 void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
189 private:
190 void init();
191 static custom_color_list& previous_custom_colors();
192 scalar color_space_coefficient() const;
193 void change_color_space(color_space aColorSpace);
194 mode_e current_mode() const;
195 channel_e current_channel() const;
196 void set_current_channel(channel_e aChannel);
197 hsv_color selected_color_as_hsv(bool aChangeRepresentation) const;
198 vec4 selected_color_in_color_space() const;
199 void select_color(const representations& aColor, const i_widget& aUpdatingWidget);
200 void select_color_in_color_space(const vec4& aColor, const i_widget& aUpdatingWidget);
201 custom_color_list::iterator current_custom_color() const;
202 void set_current_custom_color(custom_color_list::iterator aCustomColor);
203 void update_widgets(const i_widget& aUpdatingWidget);
204 private:
205 sink iSink;
206 channel_e iCurrentChannel;
207 color iCurrentColor;
208 mutable representations iSelectedColor;
209 custom_color_list iCustomColors;
210 custom_color_list::iterator iCurrentCustomColor;
211 bool iUpdatingWidgets;
212 bool iScreenPickerActive;
213 vertical_layout iLayout;
214 horizontal_layout iLayout2;
215 vertical_layout iLeftLayout;
216 vertical_layout iRightLayout;
217 horizontal_layout iRightTopLayout;
218 horizontal_layout iRightBottomLayout;
219 color_selection iColorSelection;
220 push_button iScreenPicker;
221 horizontal_spacer iSpacer0;
222 grid_layout iChannelLayout;
223 group_box iBasicColorsGroup;
224 grid_layout iBasicColorsGrid;
225 vertical_spacer iSpacer;
226 group_box iCustomColorsGroup;
227 grid_layout iCustomColorsGrid;
228 horizontal_spacer iSpacer2;
229 yz_picker iYZPicker;
230 x_picker iXPicker;
231 horizontal_layout iModelLayout;
232 horizontal_spacer iSpacer3;
233 std::optional<color_space> iColorSpace;
234 drop_list iColorSpaceSelector;
235 std::pair<radio_button, double_spin_box> iH;
236 std::pair<radio_button, double_spin_box> iS;
237 std::pair<radio_button, double_spin_box> iV;
238 std::pair<radio_button, double_spin_box> iR;
239 std::pair<radio_button, double_spin_box> iG;
240 std::pair<radio_button, double_spin_box> iB;
241 std::pair<radio_button, double_spin_box> iA;
242 line_edit iRgb;
243 push_button iAddToCustomColors;
244 };
245}
const custom_color_list & custom_colors() const
void set_custom_colors(const custom_color_list &aCustomColors)
color_dialog(i_widget &aParent, const color &aCurrentColor=color::Black)
color current_color() const
hsv_color selected_color_as_hsv() const
void mouse_button_pressed(mouse_button aButton, const point &aPosition, key_modifiers_e aKeyModifiers) override
color_dialog(const color &aCurrentColor=color::Black)
void select_color(const color &aColor)
color selected_color() const
dimension effective_frame_width() const
static const sRGB_color Black
Definition color.hpp:403
mouse_button
Definition i_mouse.hpp:31
double scalar
Definition numerical.hpp:63
#define meta_object(...)
Definition i_object.hpp:28
#define define_event(name, declName,...)
Definition event.hpp:200