1/**************************************************************************/
2/* color_mode.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "color_mode.h"
32
33#include "core/math/color.h"
34#include "scene/gui/slider.h"
35#include "thirdparty/misc/ok_color.h"
36
37ColorMode::ColorMode(ColorPicker *p_color_picker) {
38 color_picker = p_color_picker;
39}
40
41String ColorModeRGB::get_slider_label(int idx) const {
42 ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
43 return labels[idx];
44}
45
46float ColorModeRGB::get_slider_max(int idx) const {
47 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
48 Color color = color_picker->get_pick_color();
49 return next_power_of_2(MAX(255, color.components[idx] * 255.0)) - 1;
50}
51
52float ColorModeRGB::get_slider_value(int idx) const {
53 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider value.");
54 return color_picker->get_pick_color().components[idx] * 255;
55}
56
57Color ColorModeRGB::get_color() const {
58 Vector<float> values = color_picker->get_active_slider_values();
59 Color color;
60 for (int i = 0; i < 4; i++) {
61 color.components[i] = values[i] / 255.0;
62 }
63 return color;
64}
65
66void ColorModeRGB::slider_draw(int p_which) {
67 Vector<Vector2> pos;
68 pos.resize(4);
69 Vector<Color> col;
70 col.resize(4);
71 HSlider *slider = color_picker->get_slider(p_which);
72 Size2 size = slider->get_size();
73 Color left_color;
74 Color right_color;
75 Color color = color_picker->get_pick_color();
76 const real_t margin = 16 * color_picker->theme_cache.base_scale;
77
78 if (p_which == ColorPicker::SLIDER_COUNT) {
79 slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
80
81 left_color = color;
82 left_color.a = 0;
83 right_color = color;
84 right_color.a = 1;
85 } else {
86 left_color = Color(
87 p_which == 0 ? 0 : color.r,
88 p_which == 1 ? 0 : color.g,
89 p_which == 2 ? 0 : color.b);
90 right_color = Color(
91 p_which == 0 ? 1 : color.r,
92 p_which == 1 ? 1 : color.g,
93 p_which == 2 ? 1 : color.b);
94 }
95
96 col.set(0, left_color);
97 col.set(1, right_color);
98 col.set(2, right_color);
99 col.set(3, left_color);
100 pos.set(0, Vector2(0, 0));
101 pos.set(1, Vector2(size.x, 0));
102 pos.set(2, Vector2(size.x, margin));
103 pos.set(3, Vector2(0, margin));
104
105 slider->draw_polygon(pos, col);
106}
107
108void ColorModeHSV::_value_changed() {
109 Vector<float> values = color_picker->get_active_slider_values();
110
111 if (values[1] > 0 || values[0] != cached_hue) {
112 cached_hue = values[0];
113 }
114 if (values[2] > 0 || values[1] != cached_saturation) {
115 cached_saturation = values[1];
116 }
117}
118
119String ColorModeHSV::get_slider_label(int idx) const {
120 ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
121 return labels[idx];
122}
123
124float ColorModeHSV::get_slider_max(int idx) const {
125 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
126 return slider_max[idx];
127}
128
129float ColorModeHSV::get_slider_value(int idx) const {
130 switch (idx) {
131 case 0: {
132 if (color_picker->get_pick_color().get_s() > 0) {
133 return color_picker->get_pick_color().get_h() * 360.0;
134 } else {
135 return cached_hue;
136 }
137 }
138 case 1: {
139 if (color_picker->get_pick_color().get_v() > 0) {
140 return color_picker->get_pick_color().get_s() * 100.0;
141 } else {
142 return cached_saturation;
143 }
144 }
145 case 2:
146 return color_picker->get_pick_color().get_v() * 100.0;
147 case 3:
148 return Math::round(color_picker->get_pick_color().components[3] * 255.0);
149 default:
150 ERR_FAIL_V_MSG(0, "Couldn't get slider value.");
151 }
152}
153
154Color ColorModeHSV::get_color() const {
155 Vector<float> values = color_picker->get_active_slider_values();
156 Color color;
157 color.set_hsv(values[0] / 360.0, values[1] / 100.0, values[2] / 100.0, values[3] / 255.0);
158 return color;
159}
160
161void ColorModeHSV::slider_draw(int p_which) {
162 Vector<Vector2> pos;
163 pos.resize(4);
164 Vector<Color> col;
165 col.resize(4);
166 HSlider *slider = color_picker->get_slider(p_which);
167 Size2 size = slider->get_size();
168 Color left_color;
169 Color right_color;
170 Color color = color_picker->get_pick_color();
171 const real_t margin = 16 * color_picker->theme_cache.base_scale;
172
173 if (p_which == ColorPicker::SLIDER_COUNT) {
174 slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
175
176 left_color = color;
177 left_color.a = 0;
178 right_color = color;
179 right_color.a = 1;
180 } else if (p_which == 0) {
181 float v = color.get_v();
182 left_color = Color(v, v, v);
183 right_color = left_color;
184 } else {
185 Color s_col;
186 Color v_col;
187 s_col.set_hsv(color.get_h(), 0, color.get_v());
188 left_color = (p_which == 1) ? s_col : Color(0, 0, 0);
189
190 float s_col_hue = (Math::is_zero_approx(color.get_s())) ? cached_hue / 360.0 : color.get_h();
191 s_col.set_hsv(s_col_hue, 1, color.get_v());
192 v_col.set_hsv(color.get_h(), color.get_s(), 1);
193 right_color = (p_which == 1) ? s_col : v_col;
194 }
195 col.set(0, left_color);
196 col.set(1, right_color);
197 col.set(2, right_color);
198 col.set(3, left_color);
199 pos.set(0, Vector2(0, 0));
200 pos.set(1, Vector2(size.x, 0));
201 pos.set(2, Vector2(size.x, margin));
202 pos.set(3, Vector2(0, margin));
203
204 slider->draw_polygon(pos, col);
205
206 if (p_which == 0) { // H
207 Ref<Texture2D> hue = color_picker->theme_cache.color_hue;
208 slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_v(), color.get_s()));
209 }
210}
211
212String ColorModeRAW::get_slider_label(int idx) const {
213 ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
214 return labels[idx];
215}
216
217float ColorModeRAW::get_slider_max(int idx) const {
218 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
219 return slider_max[idx];
220}
221
222float ColorModeRAW::get_slider_value(int idx) const {
223 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider value.");
224 return color_picker->get_pick_color().components[idx];
225}
226
227Color ColorModeRAW::get_color() const {
228 Vector<float> values = color_picker->get_active_slider_values();
229 Color color;
230 for (int i = 0; i < 4; i++) {
231 color.components[i] = values[i];
232 }
233 return color;
234}
235
236void ColorModeRAW::slider_draw(int p_which) {
237 Vector<Vector2> pos;
238 pos.resize(4);
239 Vector<Color> col;
240 col.resize(4);
241 HSlider *slider = color_picker->get_slider(p_which);
242 Size2 size = slider->get_size();
243 Color left_color;
244 Color right_color;
245 Color color = color_picker->get_pick_color();
246 const real_t margin = 16 * color_picker->theme_cache.base_scale;
247
248 if (p_which == ColorPicker::SLIDER_COUNT) {
249 slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
250
251 left_color = color;
252 left_color.a = 0;
253 right_color = color;
254 right_color.a = 1;
255
256 col.set(0, left_color);
257 col.set(1, right_color);
258 col.set(2, right_color);
259 col.set(3, left_color);
260 pos.set(0, Vector2(0, 0));
261 pos.set(1, Vector2(size.x, 0));
262 pos.set(2, Vector2(size.x, margin));
263 pos.set(3, Vector2(0, margin));
264
265 slider->draw_polygon(pos, col);
266 }
267}
268
269bool ColorModeRAW::apply_theme() const {
270 for (int i = 0; i < 4; i++) {
271 HSlider *slider = color_picker->get_slider(i);
272 slider->remove_theme_icon_override("grabber");
273 slider->remove_theme_icon_override("grabber_highlight");
274 slider->remove_theme_style_override("slider");
275 slider->remove_theme_constant_override("grabber_offset");
276 }
277
278 return true;
279}
280
281void ColorModeOKHSL::_value_changed() {
282 Vector<float> values = color_picker->get_active_slider_values();
283
284 if (values[1] > 0 || values[0] != cached_hue) {
285 cached_hue = values[0];
286 }
287 if (values[2] > 0 || values[1] != cached_saturation) {
288 cached_saturation = values[1];
289 }
290}
291
292String ColorModeOKHSL::get_slider_label(int idx) const {
293 ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
294 return labels[idx];
295}
296
297float ColorModeOKHSL::get_slider_max(int idx) const {
298 ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
299 return slider_max[idx];
300}
301
302float ColorModeOKHSL::get_slider_value(int idx) const {
303 switch (idx) {
304 case 0: {
305 if (color_picker->get_pick_color().get_ok_hsl_s() > 0) {
306 return color_picker->get_pick_color().get_ok_hsl_h() * 360.0;
307 } else {
308 return cached_hue;
309 }
310 }
311 case 1: {
312 if (color_picker->get_pick_color().get_ok_hsl_l() > 0) {
313 return color_picker->get_pick_color().get_ok_hsl_s() * 100.0;
314 } else {
315 return cached_saturation;
316 }
317 }
318 case 2:
319 return color_picker->get_pick_color().get_ok_hsl_l() * 100.0;
320 case 3:
321 return Math::round(color_picker->get_pick_color().components[3] * 255.0);
322 default:
323 ERR_FAIL_V_MSG(0, "Couldn't get slider value.");
324 }
325}
326
327Color ColorModeOKHSL::get_color() const {
328 Vector<float> values = color_picker->get_active_slider_values();
329 Color color;
330 color.set_ok_hsl(values[0] / 360.0, values[1] / 100.0, values[2] / 100.0, values[3] / 255.0);
331 return color;
332}
333
334void ColorModeOKHSL::slider_draw(int p_which) {
335 HSlider *slider = color_picker->get_slider(p_which);
336 Size2 size = slider->get_size();
337 const real_t margin = 16 * color_picker->theme_cache.base_scale;
338
339 Vector<Vector2> pos;
340 Vector<Color> col;
341 Color left_color;
342 Color right_color;
343 Color color = color_picker->get_pick_color();
344
345 if (p_which == 2) { // L
346 pos.resize(6);
347 col.resize(6);
348 left_color = Color(0, 0, 0);
349 Color middle_color;
350 float slider_hue = (Math::is_zero_approx(color.get_ok_hsl_s())) ? cached_hue / 360.0 : color.get_ok_hsl_h();
351 float slider_sat = (Math::is_zero_approx(color.get_ok_hsl_l())) ? cached_saturation / 100.0 : color.get_ok_hsl_s();
352
353 middle_color.set_ok_hsl(slider_hue, slider_sat, 0.5);
354 right_color.set_ok_hsl(slider_hue, slider_sat, 1);
355
356 col.set(0, left_color);
357 col.set(1, middle_color);
358 col.set(2, right_color);
359 col.set(3, right_color);
360 col.set(4, middle_color);
361 col.set(5, left_color);
362 pos.set(0, Vector2(0, 0));
363 pos.set(1, Vector2(size.x * 0.5, 0));
364 pos.set(2, Vector2(size.x, 0));
365 pos.set(3, Vector2(size.x, margin));
366 pos.set(4, Vector2(size.x * 0.5, margin));
367 pos.set(5, Vector2(0, margin));
368 } else {
369 pos.resize(4);
370 col.resize(4);
371
372 if (p_which == ColorPicker::SLIDER_COUNT) {
373 slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
374
375 left_color = color;
376 left_color.a = 0;
377 right_color = color;
378 right_color.a = 1;
379 } else if (p_which == 0) {
380 float l = color.get_ok_hsl_l();
381 left_color = Color(l, l, l);
382 right_color = left_color;
383 } else {
384 left_color.set_ok_hsl(color.get_ok_hsl_h(), 0, color.get_ok_hsl_l());
385 float s_col_hue = (Math::is_zero_approx(color.get_ok_hsl_s())) ? cached_hue / 360.0 : color.get_ok_hsl_h();
386 right_color.set_ok_hsl(s_col_hue, 1, color.get_ok_hsl_l());
387 }
388
389 col.set(0, left_color);
390 col.set(1, right_color);
391 col.set(2, right_color);
392 col.set(3, left_color);
393 pos.set(0, Vector2(0, 0));
394 pos.set(1, Vector2(size.x, 0));
395 pos.set(2, Vector2(size.x, margin));
396 pos.set(3, Vector2(0, margin));
397 }
398
399 slider->draw_polygon(pos, col);
400
401 if (p_which == 0) { // H
402 Ref<Texture2D> hue = color_picker->theme_cache.color_okhsl_hue;
403 slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_ok_hsl_l() * 2.0, color.get_ok_hsl_s()));
404 return;
405 }
406}
407