1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-15 13:51:40 +00:00

Merge pull request #99461 from beicause/color-picker-fix-68286

ColorPicker: fix OKHSL circle in HSV mode
This commit is contained in:
Thaddeus Crews
2024-11-22 14:54:09 -06:00
2 changed files with 42 additions and 21 deletions

View File

@@ -217,14 +217,14 @@ void fragment() {
circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"( circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
// ColorPicker ok color hsv circle shader. // ColorPicker ok color hsv circle shader.
uniform float v = 1.0; uniform float ok_hsl_l = 1.0;
void fragment() { void fragment() {
float x = UV.x - 0.5; float x = UV.x - 0.5;
float y = UV.y - 0.5; float y = UV.y - 0.5;
float h = atan(y, x) / (2.0 * M_PI); float h = atan(y, x) / (2.0 * M_PI);
float s = sqrt(x * x + y * y) * 2.0; float s = sqrt(x * x + y * y) * 2.0;
vec3 col = okhsl_to_srgb(vec3(h, s, v)); vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
x += 0.001; x += 0.001;
y += 0.001; y += 0.001;
float b = float(sqrt(x * x + y * y) < 0.5); float b = float(sqrt(x * x + y * y) < 0.5);
@@ -387,10 +387,21 @@ void ColorPicker::_slider_value_changed() {
color = modes[current_mode]->get_color(); color = modes[current_mode]->get_color();
modes[current_mode]->_value_changed(); modes[current_mode]->_value_changed();
if (current_mode == MODE_HSV || current_mode == MODE_OKHSL) { if (current_mode == MODE_HSV) {
h = sliders[0]->get_value() / 360.0; h = sliders[0]->get_value() / 360.0;
s = sliders[1]->get_value() / 100.0; s = sliders[1]->get_value() / 100.0;
v = sliders[2]->get_value() / 100.0; v = sliders[2]->get_value() / 100.0;
ok_hsl_h = color.get_ok_hsl_h();
ok_hsl_s = color.get_ok_hsl_s();
ok_hsl_l = color.get_ok_hsl_l();
last_color = color;
} else if (current_mode == MODE_OKHSL) {
ok_hsl_h = sliders[0]->get_value() / 360.0;
ok_hsl_s = sliders[1]->get_value() / 100.0;
ok_hsl_l = sliders[2]->get_value() / 100.0;
h = color.get_h();
s = color.get_s();
v = color.get_v();
last_color = color; last_color = color;
} }
@@ -504,20 +515,17 @@ Vector<float> ColorPicker::get_active_slider_values() {
} }
void ColorPicker::_copy_color_to_hsv() { void ColorPicker::_copy_color_to_hsv() {
if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) { ok_hsl_h = color.get_ok_hsl_h();
h = color.get_ok_hsl_h(); ok_hsl_s = color.get_ok_hsl_s();
s = color.get_ok_hsl_s(); ok_hsl_l = color.get_ok_hsl_l();
v = color.get_ok_hsl_l(); h = color.get_h();
} else { s = color.get_s();
h = color.get_h(); v = color.get_v();
s = color.get_s();
v = color.get_v();
}
} }
void ColorPicker::_copy_hsv_to_color() { void ColorPicker::_copy_hsv_to_color() {
if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) { if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) {
color.set_ok_hsl(h, s, v, color.a); color.set_ok_hsl(ok_hsl_h, ok_hsl_s, ok_hsl_l, color.a);
} else { } else {
color.set_hsv(h, s, v, color.a); color.set_hsv(h, s, v, color.a);
} }
@@ -1201,8 +1209,8 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
int x; int x;
int y; int y;
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) { if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
x = center.x + (center.x * Math::cos(h * Math_TAU) * s) - (theme_cache.picker_cursor->get_width() / 2); x = center.x + (center.x * Math::cos((actual_shape == SHAPE_OKHSL_CIRCLE ? ok_hsl_h : h) * Math_TAU) * s) - (theme_cache.picker_cursor->get_width() / 2);
y = center.y + (center.y * Math::sin(h * Math_TAU) * s) - (theme_cache.picker_cursor->get_height() / 2); y = center.y + (center.y * Math::sin((actual_shape == SHAPE_OKHSL_CIRCLE ? ok_hsl_h : h) * Math_TAU) * s) - (theme_cache.picker_cursor->get_height() / 2);
} else { } else {
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0; real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0; real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
@@ -1238,11 +1246,11 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
Vector<Point2> points; Vector<Point2> points;
Vector<Color> colors; Vector<Color> colors;
Color col; Color col;
col.set_ok_hsl(h, s, 1); col.set_ok_hsl(ok_hsl_h, ok_hsl_s, 1);
Color col2; Color col2;
col2.set_ok_hsl(h, s, 0.5); col2.set_ok_hsl(ok_hsl_h, ok_hsl_s, 0.5);
Color col3; Color col3;
col3.set_ok_hsl(h, s, 0); col3.set_ok_hsl(ok_hsl_h, ok_hsl_s, 0);
points.resize(6); points.resize(6);
colors.resize(6); colors.resize(6);
points.set(0, Vector2(c->get_size().x, 0)); points.set(0, Vector2(c->get_size().x, 0));
@@ -1258,8 +1266,8 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
colors.set(4, col2); colors.set(4, col2);
colors.set(5, col); colors.set(5, col);
c->draw_polygon(points, colors); c->draw_polygon(points, colors);
int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1); int y = c->get_size().y - c->get_size().y * CLAMP(ok_hsl_l, 0, 1);
col.set_ok_hsl(h, 1, v); col.set_ok_hsl(ok_hsl_h, 1, ok_hsl_l);
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted()); c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
} else if (actual_shape == SHAPE_VHS_CIRCLE) { } else if (actual_shape == SHAPE_VHS_CIRCLE) {
Vector<Point2> points; Vector<Point2> points;
@@ -1283,8 +1291,10 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
} }
} else if (p_which == 2) { } else if (p_which == 2) {
c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1)); c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) { if (actual_shape == SHAPE_VHS_CIRCLE) {
circle_mat->set_shader_parameter("v", v); circle_mat->set_shader_parameter("v", v);
} else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
circle_mat->set_shader_parameter("ok_hsl_l", ok_hsl_l);
} }
} }
} }
@@ -1308,6 +1318,8 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
real_t rad = center.angle_to_point(bev->get_position()); real_t rad = center.angle_to_point(bev->get_position());
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU; h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
s = CLAMP(dist / center.x, 0, 1); s = CLAMP(dist / center.x, 0, 1);
ok_hsl_h = h;
ok_hsl_s = s;
} else { } else {
return; return;
} }
@@ -1375,6 +1387,8 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
real_t rad = center.angle_to_point(mev->get_position()); real_t rad = center.angle_to_point(mev->get_position());
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU; h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
s = CLAMP(dist / center.x, 0, 1); s = CLAMP(dist / center.x, 0, 1);
ok_hsl_h = h;
ok_hsl_s = s;
} else { } else {
if (spinning) { if (spinning) {
real_t rad = center.angle_to_point(mev->get_position()); real_t rad = center.angle_to_point(mev->get_position());
@@ -1412,6 +1426,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height); float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) { if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
v = 1.0 - (y / w_edit->get_size().height); v = 1.0 - (y / w_edit->get_size().height);
ok_hsl_l = v;
} else { } else {
h = y / w_edit->get_size().height; h = y / w_edit->get_size().height;
} }
@@ -1440,6 +1455,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height); float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) { if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
v = 1.0 - (y / w_edit->get_size().height); v = 1.0 - (y / w_edit->get_size().height);
ok_hsl_l = v;
} else { } else {
h = y / w_edit->get_size().height; h = y / w_edit->get_size().height;
} }

View File

@@ -211,6 +211,11 @@ private:
float h = 0.0; float h = 0.0;
float s = 0.0; float s = 0.0;
float v = 0.0; float v = 0.0;
float ok_hsl_h = 0.0;
float ok_hsl_s = 0.0;
float ok_hsl_l = 0.0;
Color last_color; Color last_color;
struct ThemeCache { struct ThemeCache {