Browse Source

ColorPicker: Improve the accuracy of hue slider in OKHSL mode

LuoZhihao 8 months ago
parent
commit
9eeeadb037

+ 0 - 3
doc/classes/ColorPicker.xml

@@ -168,9 +168,6 @@
 		<theme_item name="color_hue" data_type="icon" type="Texture2D">
 			Custom texture for the hue selection slider on the right.
 		</theme_item>
-		<theme_item name="color_okhsl_hue" data_type="icon" type="Texture2D">
-			Custom texture for the H slider in the OKHSL color mode.
-		</theme_item>
 		<theme_item name="expanded_arrow" data_type="icon" type="Texture2D">
 			The icon for color preset drop down menu when expanded.
 		</theme_item>

+ 25 - 3
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<Texture2D> 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<Gradient> 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);
 	}
 }

+ 3 - 0
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<GradientTexture2D> hue_texture = nullptr;
 
 	virtual String get_name() const override { return "OKHSL"; }
 

+ 1 - 1
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");

+ 0 - 1
scene/gui/color_picker.h

@@ -264,7 +264,6 @@ private:
 		Ref<Texture2D> overbright_indicator;
 		Ref<Texture2D> picker_cursor;
 		Ref<Texture2D> color_hue;
-		Ref<Texture2D> color_okhsl_hue;
 
 		/* Mode buttons */
 		Ref<StyleBox> mode_button_normal;

+ 0 - 27
scene/theme/default_theme.cpp

@@ -1081,33 +1081,6 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
 		theme->set_icon("color_hue", "ColorPicker", hue_texture);
 	}
 
-	{
-		const int precision = 7;
-
-		Ref<Gradient> 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<GradientTexture2D> 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"]);