From 9eeeadb037c680cfc1d3806fbcb8e6aeb70e9977 Mon Sep 17 00:00:00 2001 From: LuoZhihao Date: Sat, 4 Jan 2025 21:36:27 +0800 Subject: [PATCH] ColorPicker: Improve the accuracy of hue slider in OKHSL mode --- doc/classes/ColorPicker.xml | 3 --- scene/gui/color_mode.cpp | 28 +++++++++++++++++++++++++--- scene/gui/color_mode.h | 3 +++ scene/gui/color_picker.cpp | 2 +- scene/gui/color_picker.h | 1 - scene/theme/default_theme.cpp | 27 --------------------------- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/doc/classes/ColorPicker.xml b/doc/classes/ColorPicker.xml index a11284a8449..f57a94e67d2 100644 --- a/doc/classes/ColorPicker.xml +++ b/doc/classes/ColorPicker.xml @@ -168,9 +168,6 @@ Custom texture for the hue selection slider on the right. - - Custom texture for the H slider in the OKHSL color mode. - The icon for color preset drop down menu when expanded. diff --git a/scene/gui/color_mode.cpp b/scene/gui/color_mode.cpp index 43d571312e6..5f58a16d50f 100644 --- a/scene/gui/color_mode.cpp +++ b/scene/gui/color_mode.cpp @@ -32,6 +32,7 @@ #include "core/math/color.h" #include "scene/gui/slider.h" +#include "scene/resources/gradient_texture.h" ColorMode::ColorMode(ColorPicker *p_color_picker) { color_picker = p_color_picker; @@ -398,8 +399,29 @@ void ColorModeOKHSL::slider_draw(int p_which) { slider->draw_polygon(pos, col); if (p_which == 0) { // H - Ref hue = color_picker->theme_cache.color_okhsl_hue; - 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())); - return; + const int precision = 7; + + Ref hue_gradient; + hue_gradient.instantiate(); + PackedFloat32Array offsets; + offsets.resize(precision); + PackedColorArray colors; + colors.resize(precision); + + for (int i = 0; i < precision; i++) { + float h = i / float(precision - 1); + offsets.write[i] = h; + colors.write[i] = Color::from_ok_hsl(h, color.get_ok_hsl_s(), color.get_ok_hsl_l()); + } + hue_gradient->set_offsets(offsets); + hue_gradient->set_colors(colors); + hue_gradient->set_interpolation_color_space(Gradient::ColorSpace::GRADIENT_COLOR_SPACE_OKLAB); + if (hue_texture.is_null()) { + hue_texture.instantiate(); + hue_texture->set_width(800); + hue_texture->set_height(6); + } + hue_texture->set_gradient(hue_gradient); + slider->draw_texture_rect(hue_texture, Rect2(Vector2(), Vector2(size.x, margin)), false); } } diff --git a/scene/gui/color_mode.h b/scene/gui/color_mode.h index b762097dc30..c035fe6f65a 100644 --- a/scene/gui/color_mode.h +++ b/scene/gui/color_mode.h @@ -33,6 +33,8 @@ #include "scene/gui/color_picker.h" +class GradientTexture2D; + class ColorMode { public: ColorPicker *color_picker = nullptr; @@ -129,6 +131,7 @@ public: float slider_max[4] = { 359, 100, 100, 255 }; float cached_hue = 0.0; float cached_saturation = 0.0; + Ref hue_texture = nullptr; virtual String get_name() const override { return "OKHSL"; } diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index a57bfd26fa0..751d6922cdc 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -46,6 +46,7 @@ #include "scene/gui/texture_rect.h" #include "scene/resources/atlas_texture.h" #include "scene/resources/color_palette.h" +#include "scene/resources/gradient_texture.h" #include "scene/resources/image_texture.h" #include "scene/resources/style_box_flat.h" #include "scene/resources/style_box_texture.h" @@ -2152,7 +2153,6 @@ void ColorPicker::_bind_methods() { BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, overbright_indicator); BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor); BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_hue); - BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_okhsl_hue); BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_normal, "tab_unselected", "TabContainer"); BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_pressed, "tab_selected", "TabContainer"); diff --git a/scene/gui/color_picker.h b/scene/gui/color_picker.h index 829263fbf55..ea3ea017435 100644 --- a/scene/gui/color_picker.h +++ b/scene/gui/color_picker.h @@ -264,7 +264,6 @@ private: Ref overbright_indicator; Ref picker_cursor; Ref color_hue; - Ref color_okhsl_hue; /* Mode buttons */ Ref mode_button_normal; diff --git a/scene/theme/default_theme.cpp b/scene/theme/default_theme.cpp index c1eda4db948..464a40bf823 100644 --- a/scene/theme/default_theme.cpp +++ b/scene/theme/default_theme.cpp @@ -1081,33 +1081,6 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_icon("color_hue", "ColorPicker", hue_texture); } - { - const int precision = 7; - - Ref hue_gradient; - hue_gradient.instantiate(); - PackedFloat32Array offsets; - offsets.resize(precision); - PackedColorArray colors; - colors.resize(precision); - - for (int i = 0; i < precision; i++) { - float h = i / float(precision - 1); - offsets.write[i] = h; - colors.write[i] = Color::from_ok_hsl(h, 1, 0.5); - } - hue_gradient->set_offsets(offsets); - hue_gradient->set_colors(colors); - - Ref hue_texture; - hue_texture.instantiate(); - hue_texture->set_width(800); - hue_texture->set_height(6); - hue_texture->set_gradient(hue_gradient); - - theme->set_icon("color_okhsl_hue", "ColorPicker", hue_texture); - } - // ColorPickerButton theme->set_icon("bg", "ColorPickerButton", icons["mini_checkerboard"]);