Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow picking similar colours using OKHSL. #59786

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ Comment: Tangent Space Normal Maps implementation
Copyright: 2011, Morten S. Mikkelsen
License: Zlib

Files: ./thirdparty/misc/ok_color.h
./thirdparty/misc/ok_color_shader.h
Comment: OK Lab color space
fire marked this conversation as resolved.
Show resolved Hide resolved
Copyright: 2021, Björn Ottosson
License: Expat

Files: ./thirdparty/noise/FastNoiseLite.h
Comment: FastNoise Lite
Copyright: 2020, Jordan Peck and contributors
Expand Down
61 changes: 61 additions & 0 deletions core/math/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "core/string/print_string.h"
#include "core/templates/rb_map.h"

#include "thirdparty/misc/ok_color.h"

uint32_t Color::to_argb32() const {
uint32_t c = (uint8_t)Math::round(a * 255);
c <<= 8;
Expand Down Expand Up @@ -240,6 +242,20 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
}
}

void Color::set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
ok_color::HSL hsl;
hsl.h = p_h;
hsl.s = p_s;
hsl.l = p_l;
ok_color new_ok_color;
ok_color::RGB rgb = new_ok_color.okhsl_to_srgb(hsl);
Color c = Color(rgb.r, rgb.g, rgb.b, p_alpha).clamp();
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}

bool Color::is_equal_approx(const Color &p_color) const {
return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a);
}
Expand Down Expand Up @@ -568,3 +584,48 @@ Color Color::operator-() const {
1.0f - b,
1.0f - a);
}

Color Color::from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
Color c;
c.set_ok_hsl(p_h, p_s, p_l, p_alpha);
return c;
}

float Color::get_ok_hsl_h() const {
ok_color::RGB rgb;
rgb.r = r;
rgb.g = g;
rgb.b = b;
ok_color new_ok_color;
ok_color::HSL ok_hsl = new_ok_color.srgb_to_okhsl(rgb);
if (Math::is_nan(ok_hsl.h)) {
return 0.0f;
}
return CLAMP(ok_hsl.h, 0.0f, 1.0f);
}

float Color::get_ok_hsl_s() const {
ok_color::RGB rgb;
rgb.r = r;
rgb.g = g;
rgb.b = b;
ok_color new_ok_color;
ok_color::HSL ok_hsl = new_ok_color.srgb_to_okhsl(rgb);
if (Math::is_nan(ok_hsl.s)) {
return 0.0f;
}
return CLAMP(ok_hsl.s, 0.0f, 1.0f);
}

float Color::get_ok_hsl_l() const {
ok_color::RGB rgb;
rgb.r = r;
rgb.g = g;
rgb.b = b;
ok_color new_ok_color;
ok_color::HSL ok_hsl = new_ok_color.srgb_to_okhsl(rgb);
if (Math::is_nan(ok_hsl.l)) {
return 0.0f;
}
return CLAMP(ok_hsl.l, 0.0f, 1.0f);
}
8 changes: 8 additions & 0 deletions core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ struct _NO_DISCARD_ Color {
float get_s() const;
float get_v() const;
void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);
float get_ok_hsl_h() const;
float get_ok_hsl_s() const;
float get_ok_hsl_l() const;
void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0);

_FORCE_INLINE_ float &operator[](int p_idx) {
return components[p_idx];
Expand Down Expand Up @@ -195,6 +199,7 @@ struct _NO_DISCARD_ Color {
static Color get_named_color(int p_idx);
static Color from_string(const String &p_string, const Color &p_default);
static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);
static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0);
static Color from_rgbe9995(uint32_t p_rgbe);

_FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys
Expand All @@ -213,6 +218,9 @@ struct _NO_DISCARD_ Color {
_FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v()); }
_FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v()); }
_FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v); }
_FORCE_INLINE_ void set_ok_hsl_h(float p_h) { set_ok_hsl(p_h, get_ok_hsl_s(), get_ok_hsl_l()); }
_FORCE_INLINE_ void set_ok_hsl_s(float p_s) { set_ok_hsl(get_ok_hsl_h(), p_s, get_ok_hsl_l()); }
_FORCE_INLINE_ void set_ok_hsl_l(float p_l) { set_ok_hsl(get_ok_hsl_h(), get_ok_hsl_s(), p_l); }

_FORCE_INLINE_ Color() {}

Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,8 @@ static void _register_variant_builtin_methods() {
bind_static_method(Color, get_named_color, sarray("idx"), varray());
bind_static_method(Color, from_string, sarray("str", "default"), varray());
bind_static_method(Color, from_hsv, sarray("h", "s", "v", "alpha"), varray(1.0));
bind_static_method(Color, from_ok_hsl, sarray("h", "s", "l", "alpha"), varray(1.0));

bind_static_method(Color, from_rgbe9995, sarray("rgbe"), varray());

/* RID */
Expand Down
4 changes: 4 additions & 0 deletions core/variant/variant_setget.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,8 @@ SETGET_NUMBER_STRUCT_FUNC(Color, double, h, set_h, get_h)
SETGET_NUMBER_STRUCT_FUNC(Color, double, s, set_s, get_s)
SETGET_NUMBER_STRUCT_FUNC(Color, double, v, set_v, get_v)

SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_h, set_ok_hsl_h, get_ok_hsl_h)
SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_s, set_ok_hsl_s, get_ok_hsl_s)
SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_l, set_ok_hsl_l, get_ok_hsl_l)

#endif // VARIANT_SETGET_H
18 changes: 18 additions & 0 deletions doc/classes/Color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@
[/codeblocks]
</description>
</method>
<method name="from_ok_hsl" qualifiers="static">
<return type="Color" />
<argument index="0" name="h" type="float" />
<argument index="1" name="s" type="float" />
<argument index="2" name="l" type="float" />
<argument index="3" name="alpha" type="float" default="1.0" />
<description>
Constructs a color from an [url=https://bottosson.github.io/posts/colorpicker/]OK HSL profile[/url]. [code]h[/code] (hue), [code]s[/code] (saturation), and [code]v[/code] (value) are typically between 0 and 1.
[codeblocks]
[gdscript]
var c = Color.from_ok_hsl(0.58, 0.5, 0.79, 0.8)
[/gdscript]
[csharp]
var c = Color.FromOkHsl(0.58f, 0.5f, 0.79f, 0.8f);
[/csharp]
[/codeblocks]
</description>
</method>
<method name="from_rgbe9995" qualifiers="static">
<return type="Color" />
<argument index="0" name="rgbe" type="int" />
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/ColorPicker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<constant name="SHAPE_VHS_CIRCLE" value="2" enum="PickerShapeType">
HSV Color Model circle color space. Use Saturation as a radius.
</constant>
<constant name="SHAPE_OKHSL_CIRCLE" value="3" enum="PickerShapeType">
HSL OK Color Model circle color space.
</constant>
</constants>
<theme_items>
<theme_item name="h_width" data_type="constant" type="int" default="30">
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6109,8 +6109,8 @@ EditorNode::EditorNode() {
EDITOR_DEF("interface/inspector/resources_to_open_in_new_inspector", "Script,MeshLibrary");
EDITOR_DEF("interface/inspector/default_color_picker_mode", 0);
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_mode", PROPERTY_HINT_ENUM, "RGB,HSV,RAW", PROPERTY_USAGE_DEFAULT));
EDITOR_DEF("interface/inspector/default_color_picker_shape", (int32_t)ColorPicker::SHAPE_VHS_CIRCLE);
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle", PROPERTY_USAGE_DEFAULT));
EDITOR_DEF("interface/inspector/default_color_picker_shape", (int32_t)ColorPicker::SHAPE_OKHSL_CIRCLE);
fire marked this conversation as resolved.
Show resolved Hide resolved
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle", PROPERTY_USAGE_DEFAULT));

ED_SHORTCUT("canvas_item_editor/pan_view", TTR("Pan View"), Key::SPACE);

Expand Down
Loading