Allow picking similar colours using OKHSL.
This commit is contained in:
parent
36bd26dc75
commit
1b776a6e7a
|
@ -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
|
||||
Copyright: 2021, Björn Ottosson
|
||||
License: Expat
|
||||
|
||||
Files: ./thirdparty/noise/FastNoiseLite.h
|
||||
Comment: FastNoise Lite
|
||||
Copyright: 2020, Jordan Peck and contributors
|
||||
|
|
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
|
@ -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
|
||||
|
@ -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() {}
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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" />
|
||||
|
|
|
@ -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">
|
||||
|
|
|
@ -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);
|
||||
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);
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include "color_picker.h"
|
||||
|
||||
#include "core/input/input.h"
|
||||
#include "core/math/color.h"
|
||||
#include "core/os/keyboard.h"
|
||||
#include "core/os/os.h"
|
||||
#include "scene/main/window.h"
|
||||
|
@ -39,6 +40,9 @@
|
|||
#include "editor/editor_settings.h"
|
||||
#endif
|
||||
|
||||
#include "thirdparty/misc/ok_color.h"
|
||||
#include "thirdparty/misc/ok_color_shader.h"
|
||||
|
||||
List<Color> ColorPicker::preset_cache;
|
||||
|
||||
void ColorPicker::_notification(int p_what) {
|
||||
|
@ -102,6 +106,7 @@ void ColorPicker::_notification(int p_what) {
|
|||
|
||||
Ref<Shader> ColorPicker::wheel_shader;
|
||||
Ref<Shader> ColorPicker::circle_shader;
|
||||
Ref<Shader> ColorPicker::circle_ok_color_shader;
|
||||
|
||||
void ColorPicker::init_shaders() {
|
||||
wheel_shader.instantiate();
|
||||
|
@ -152,11 +157,36 @@ void fragment() {
|
|||
|
||||
COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
|
||||
})");
|
||||
|
||||
circle_ok_color_shader.instantiate();
|
||||
circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
|
||||
// ColorPicker ok color hsv circle shader.
|
||||
|
||||
uniform float v = 1.0;
|
||||
|
||||
void fragment() {
|
||||
float x = UV.x - 0.5;
|
||||
float y = UV.y - 0.5;
|
||||
x += 0.001;
|
||||
y += 0.001;
|
||||
float b = float(sqrt(x * x + y * y) < 0.5);
|
||||
x -= 0.002;
|
||||
float b2 = float(sqrt(x * x + y * y) < 0.5);
|
||||
y -= 0.002;
|
||||
float b3 = float(sqrt(x * x + y * y) < 0.5);
|
||||
x += 0.002;
|
||||
float b4 = float(sqrt(x * x + y * y) < 0.5);
|
||||
float s = sqrt(x * x + y * y);
|
||||
float h = atan(y, x) / (2.0*M_PI);
|
||||
vec3 col = okhsl_to_srgb(vec3(h, s, v));
|
||||
COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
|
||||
})");
|
||||
}
|
||||
|
||||
void ColorPicker::finish_shaders() {
|
||||
wheel_shader.unref();
|
||||
circle_shader.unref();
|
||||
circle_ok_color_shader.unref();
|
||||
}
|
||||
|
||||
void ColorPicker::set_focus_on_line_edit() {
|
||||
|
@ -166,8 +196,12 @@ void ColorPicker::set_focus_on_line_edit() {
|
|||
void ColorPicker::_update_controls() {
|
||||
const char *rgb[3] = { "R", "G", "B" };
|
||||
const char *hsv[3] = { "H", "S", "V" };
|
||||
|
||||
if (hsv_mode_enabled) {
|
||||
const char *hsl[3] = { "H", "S", "L" };
|
||||
if (hsv_mode_enabled && picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
labels[i]->set_text(hsl[i]);
|
||||
}
|
||||
} else if (hsv_mode_enabled && picker_type != SHAPE_OKHSL_CIRCLE) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
labels[i]->set_text(hsv[i]);
|
||||
}
|
||||
|
@ -176,14 +210,23 @@ void ColorPicker::_update_controls() {
|
|||
labels[i]->set_text(rgb[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
btn_hsv->set_text(RTR("OKHSL"));
|
||||
} else {
|
||||
btn_hsv->set_text(RTR("HSV"));
|
||||
}
|
||||
if (hsv_mode_enabled) {
|
||||
set_raw_mode(false);
|
||||
set_hsv_mode(true);
|
||||
btn_raw->set_disabled(true);
|
||||
} else if (raw_mode_enabled) {
|
||||
set_raw_mode(true);
|
||||
set_hsv_mode(false);
|
||||
btn_raw->set_disabled(false);
|
||||
btn_hsv->set_disabled(true);
|
||||
} else {
|
||||
set_raw_mode(false);
|
||||
set_hsv_mode(false);
|
||||
btn_raw->set_disabled(false);
|
||||
btn_hsv->set_disabled(false);
|
||||
}
|
||||
|
@ -236,8 +279,15 @@ void ColorPicker::_update_controls() {
|
|||
wheel_edit->show();
|
||||
w_edit->show();
|
||||
uv_edit->hide();
|
||||
|
||||
wheel->set_material(circle_mat);
|
||||
circle_mat->set_shader(circle_shader);
|
||||
break;
|
||||
case SHAPE_OKHSL_CIRCLE:
|
||||
wheel_edit->show();
|
||||
w_edit->show();
|
||||
uv_edit->hide();
|
||||
wheel->set_material(circle_mat);
|
||||
circle_mat->set_shader(circle_ok_color_shader);
|
||||
break;
|
||||
default: {
|
||||
}
|
||||
|
@ -246,11 +296,17 @@ void ColorPicker::_update_controls() {
|
|||
|
||||
void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
|
||||
color = p_color;
|
||||
if (color != last_hsv) {
|
||||
h = color.get_h();
|
||||
s = color.get_s();
|
||||
v = color.get_v();
|
||||
last_hsv = color;
|
||||
if (color != last_color) {
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
h = color.get_ok_hsl_h();
|
||||
s = color.get_ok_hsl_s();
|
||||
v = color.get_ok_hsl_l();
|
||||
} else {
|
||||
h = color.get_h();
|
||||
s = color.get_s();
|
||||
v = color.get_v();
|
||||
}
|
||||
last_color = color;
|
||||
}
|
||||
|
||||
if (!is_inside_tree()) {
|
||||
|
@ -301,10 +357,13 @@ void ColorPicker::_value_changed(double) {
|
|||
h = scroll[0]->get_value() / 360.0;
|
||||
s = scroll[1]->get_value() / 100.0;
|
||||
v = scroll[2]->get_value() / 100.0;
|
||||
color.set_hsv(h, s, v, scroll[3]->get_value() / 255.0);
|
||||
|
||||
last_hsv = color;
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_ok_hsl(h, s, v, Math::round(scroll[3]->get_value() / 255.0));
|
||||
} else {
|
||||
color.set_hsv(h, s, v, Math::round(scroll[3]->get_value() / 255.0));
|
||||
}
|
||||
|
||||
last_color = color;
|
||||
} else {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0);
|
||||
|
@ -342,7 +401,6 @@ void ColorPicker::_update_color(bool p_update_sliders) {
|
|||
for (int i = 0; i < 4; i++) {
|
||||
scroll[i]->set_step(1.0);
|
||||
}
|
||||
|
||||
scroll[0]->set_max(359);
|
||||
scroll[0]->set_value(h * 360.0);
|
||||
scroll[1]->set_max(100);
|
||||
|
@ -350,7 +408,7 @@ void ColorPicker::_update_color(bool p_update_sliders) {
|
|||
scroll[2]->set_max(100);
|
||||
scroll[2]->set_value(v * 100.0);
|
||||
scroll[3]->set_max(255);
|
||||
scroll[3]->set_value(color.components[3] * 255.0);
|
||||
scroll[3]->set_value(Math::round(color.components[3] * 255.0));
|
||||
} else {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (raw_mode_enabled) {
|
||||
|
@ -362,7 +420,7 @@ void ColorPicker::_update_color(bool p_update_sliders) {
|
|||
scroll[i]->set_value(color.components[i]);
|
||||
} else {
|
||||
scroll[i]->set_step(1);
|
||||
const float byte_value = color.components[i] * 255.0;
|
||||
const float byte_value = Math::round(color.components[i] * 255.0);
|
||||
scroll[i]->set_max(next_power_of_2(MAX(255, byte_value)) - 1);
|
||||
scroll[i]->set_value(byte_value);
|
||||
}
|
||||
|
@ -426,7 +484,6 @@ Color ColorPicker::get_pick_color() const {
|
|||
void ColorPicker::set_picker_shape(PickerShapeType p_picker_type) {
|
||||
ERR_FAIL_INDEX(p_picker_type, SHAPE_MAX);
|
||||
picker_type = p_picker_type;
|
||||
|
||||
_update_controls();
|
||||
_update_color();
|
||||
}
|
||||
|
@ -702,7 +759,7 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
|
|||
Ref<Texture2D> cursor = get_theme_icon(SNAME("picker_cursor"), SNAME("ColorPicker"));
|
||||
int x;
|
||||
int y;
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
x = center.x + (center.x * Math::cos(h * Math_TAU) * s) - (cursor->get_width() / 2);
|
||||
y = center.y + (center.y * Math::sin(h * Math_TAU) * s) - (cursor->get_height() / 2);
|
||||
} else {
|
||||
|
@ -735,6 +792,25 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
|
|||
Color col;
|
||||
col.set_hsv(h, 1, 1);
|
||||
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
||||
} else if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
Vector<Point2> points;
|
||||
Vector<Color> colors;
|
||||
Color col;
|
||||
col.set_ok_hsl(h, s, 1);
|
||||
points.resize(4);
|
||||
colors.resize(4);
|
||||
points.set(0, Vector2());
|
||||
points.set(1, Vector2(c->get_size().x, 0));
|
||||
points.set(2, c->get_size());
|
||||
points.set(3, Vector2(0, c->get_size().y));
|
||||
colors.set(0, col);
|
||||
colors.set(1, col);
|
||||
colors.set(2, Color(0, 0, 0));
|
||||
colors.set(3, Color(0, 0, 0));
|
||||
c->draw_polygon(points, colors);
|
||||
int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1);
|
||||
col.set_ok_hsl(h, 1, v);
|
||||
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
||||
} else if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
Vector<Point2> points;
|
||||
Vector<Color> colors;
|
||||
|
@ -757,7 +833,7 @@ void ColorPicker::_hsv_draw(int p_which, Control *c) {
|
|||
}
|
||||
} else if (p_which == 2) {
|
||||
c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
circle_mat->set_shader_param("v", v);
|
||||
}
|
||||
}
|
||||
|
@ -793,10 +869,19 @@ void ColorPicker::_slider_draw(int p_which) {
|
|||
}
|
||||
Color s_col;
|
||||
Color v_col;
|
||||
s_col.set_hsv(h, 0, v);
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
s_col.set_ok_hsl(h, 0, v);
|
||||
} else {
|
||||
s_col.set_hsv(h, 0, v);
|
||||
}
|
||||
left_color = (p_which == 1) ? s_col : Color(0, 0, 0);
|
||||
s_col.set_hsv(h, 1, v);
|
||||
v_col.set_hsv(h, s, 1);
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
s_col.set_ok_hsl(h, 1, v);
|
||||
v_col.set_ok_hsl(h, s, 1);
|
||||
} else {
|
||||
s_col.set_hsv(h, 1, v);
|
||||
v_col.set_hsv(h, s, 1);
|
||||
}
|
||||
right_color = (p_which == 1) ? s_col : v_col;
|
||||
} else {
|
||||
left_color = Color(
|
||||
|
@ -828,9 +913,8 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
|
|||
if (bev.is_valid()) {
|
||||
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
||||
Vector2 center = c->get_size() / 2.0;
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
real_t dist = center.distance_to(bev->get_position());
|
||||
|
||||
if (dist <= center.x) {
|
||||
real_t rad = center.angle_to_point(bev->get_position());
|
||||
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
||||
|
@ -867,8 +951,13 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
|
|||
}
|
||||
}
|
||||
changing_color = true;
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
last_hsv = color;
|
||||
if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_ok_hsl(h, s, v, color.a);
|
||||
} else if (picker_type != SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
}
|
||||
last_color = color;
|
||||
|
||||
set_pick_color(color);
|
||||
_update_color();
|
||||
if (!deferred_mode_enabled) {
|
||||
|
@ -892,7 +981,7 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
|
|||
}
|
||||
|
||||
Vector2 center = c->get_size() / 2.0;
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
real_t dist = center.distance_to(mev->get_position());
|
||||
real_t rad = center.angle_to_point(mev->get_position());
|
||||
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
||||
|
@ -913,9 +1002,12 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
|
|||
v = 1.0 - (y - corner_y) / real_size.y;
|
||||
}
|
||||
}
|
||||
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
last_hsv = color;
|
||||
if (picker_type != SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
} else if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_ok_hsl(h, s, v, color.a);
|
||||
}
|
||||
last_color = color;
|
||||
set_pick_color(color);
|
||||
_update_color();
|
||||
if (!deferred_mode_enabled) {
|
||||
|
@ -931,7 +1023,7 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
|
|||
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
||||
changing_color = true;
|
||||
float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
v = 1.0 - (y / w_edit->get_size().height);
|
||||
} else {
|
||||
h = y / w_edit->get_size().height;
|
||||
|
@ -939,8 +1031,12 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
|
|||
} else {
|
||||
changing_color = false;
|
||||
}
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
last_hsv = color;
|
||||
if (picker_type != SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
} else if (picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_ok_hsl(h, s, v, color.a);
|
||||
}
|
||||
last_color = color;
|
||||
set_pick_color(color);
|
||||
_update_color();
|
||||
if (!deferred_mode_enabled) {
|
||||
|
@ -957,13 +1053,17 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
|
|||
return;
|
||||
}
|
||||
float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
|
||||
if (picker_type == SHAPE_VHS_CIRCLE) {
|
||||
if (picker_type == SHAPE_VHS_CIRCLE || picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
v = 1.0 - (y / w_edit->get_size().height);
|
||||
} else {
|
||||
h = y / w_edit->get_size().height;
|
||||
}
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
last_hsv = color;
|
||||
if (hsv_mode_enabled && picker_type != SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_hsv(h, s, v, color.a);
|
||||
} else if (hsv_mode_enabled && picker_type == SHAPE_OKHSL_CIRCLE) {
|
||||
color.set_ok_hsl(h, s, v, color.a);
|
||||
}
|
||||
last_color = color;
|
||||
set_pick_color(color);
|
||||
_update_color();
|
||||
if (!deferred_mode_enabled) {
|
||||
|
@ -1128,7 +1228,7 @@ void ColorPicker::_bind_methods() {
|
|||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hsv_mode"), "set_hsv_mode", "is_hsv_mode");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "raw_mode"), "set_raw_mode", "is_raw_mode");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle"), "set_picker_shape", "get_picker_shape");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle"), "set_picker_shape", "get_picker_shape");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_enabled"), "set_presets_enabled", "are_presets_enabled");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
|
||||
|
||||
|
@ -1139,6 +1239,7 @@ void ColorPicker::_bind_methods() {
|
|||
BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE);
|
||||
BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL);
|
||||
BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE);
|
||||
BIND_ENUM_CONSTANT(SHAPE_OKHSL_CIRCLE);
|
||||
}
|
||||
|
||||
ColorPicker::ColorPicker() :
|
||||
|
|
|
@ -68,6 +68,7 @@ public:
|
|||
SHAPE_HSV_RECTANGLE,
|
||||
SHAPE_HSV_WHEEL,
|
||||
SHAPE_VHS_CIRCLE,
|
||||
SHAPE_OKHSL_CIRCLE,
|
||||
|
||||
SHAPE_MAX
|
||||
};
|
||||
|
@ -75,6 +76,7 @@ public:
|
|||
private:
|
||||
static Ref<Shader> wheel_shader;
|
||||
static Ref<Shader> circle_shader;
|
||||
static Ref<Shader> circle_ok_color_shader;
|
||||
static List<Color> preset_cache;
|
||||
|
||||
Control *screen = nullptr;
|
||||
|
@ -124,7 +126,7 @@ private:
|
|||
float h = 0.0;
|
||||
float s = 0.0;
|
||||
float v = 0.0;
|
||||
Color last_hsv;
|
||||
Color last_color;
|
||||
|
||||
void _html_submitted(const String &p_html);
|
||||
void _value_changed(double);
|
||||
|
@ -161,6 +163,8 @@ public:
|
|||
void set_edit_alpha(bool p_show);
|
||||
bool is_editing_alpha() const;
|
||||
|
||||
int get_preset_size();
|
||||
|
||||
void _set_pick_color(const Color &p_color, bool p_update_sliders);
|
||||
void set_pick_color(const Color &p_color);
|
||||
Color get_pick_color() const;
|
||||
|
|
|
@ -432,6 +432,15 @@ Collection of single-file libraries used in Godot components.
|
|||
* Upstream: https://github.com/Auburn/FastNoiseLite
|
||||
* Version: git (6be3d6bf7fb408de341285f9ee8a29b67fd953f1, 2022) + custom changes
|
||||
* License: MIT
|
||||
- `ok_color.h`
|
||||
* Upstream: https://github.com/bottosson/bottosson.github.io/blob/master/misc/ok_color.h
|
||||
* Version: git (d69831edb90ffdcd08b7e64da3c5405acd48ad2c, 2022)
|
||||
* License: MIT
|
||||
* Modifications: License included in header.
|
||||
- `ok_color_shader.h`
|
||||
* https://www.shadertoy.com/view/7sK3D1
|
||||
* Version: 2021-09-13
|
||||
* License: MIT
|
||||
- `pcg.{cpp,h}`
|
||||
* Upstream: http://www.pcg-random.org
|
||||
* Version: minimal C implementation, http://www.pcg-random.org/download.html
|
||||
|
|
|
@ -0,0 +1,688 @@
|
|||
// Copyright(c) 2021 Björn Ottosson
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files(the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and /or sell copies
|
||||
// of the Software, and to permit persons to whom the Software is furnished to do
|
||||
// so, subject to the following conditions :
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#ifndef OK_COLOR_H
|
||||
#define OK_COLOR_H
|
||||
|
||||
#include <cmath>
|
||||
#include <cfloat>
|
||||
|
||||
class ok_color
|
||||
{
|
||||
public:
|
||||
|
||||
struct Lab { float L; float a; float b; };
|
||||
struct RGB { float r; float g; float b; };
|
||||
struct HSV { float h; float s; float v; };
|
||||
struct HSL { float h; float s; float l; };
|
||||
struct LC { float L; float C; };
|
||||
|
||||
// Alternative representation of (L_cusp, C_cusp)
|
||||
// Encoded so S = C_cusp/L_cusp and T = C_cusp/(1-L_cusp)
|
||||
// The maximum value for C in the triangle is then found as fmin(S*L, T*(1-L)), for a given L
|
||||
struct ST { float S; float T; };
|
||||
|
||||
static constexpr float pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062f;
|
||||
|
||||
float clamp(float x, float min, float max)
|
||||
{
|
||||
if (x < min)
|
||||
return min;
|
||||
if (x > max)
|
||||
return max;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
float sgn(float x)
|
||||
{
|
||||
return (float)(0.f < x) - (float)(x < 0.f);
|
||||
}
|
||||
|
||||
float srgb_transfer_function(float a)
|
||||
{
|
||||
return .0031308f >= a ? 12.92f * a : 1.055f * powf(a, .4166666666666667f) - .055f;
|
||||
}
|
||||
|
||||
float srgb_transfer_function_inv(float a)
|
||||
{
|
||||
return .04045f < a ? powf((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
|
||||
}
|
||||
|
||||
Lab linear_srgb_to_oklab(RGB c)
|
||||
{
|
||||
float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
|
||||
float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
|
||||
float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
|
||||
|
||||
float l_ = cbrtf(l);
|
||||
float m_ = cbrtf(m);
|
||||
float s_ = cbrtf(s);
|
||||
|
||||
return {
|
||||
0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
|
||||
1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
|
||||
0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_,
|
||||
};
|
||||
}
|
||||
|
||||
RGB oklab_to_linear_srgb(Lab c)
|
||||
{
|
||||
float l_ = c.L + 0.3963377774f * c.a + 0.2158037573f * c.b;
|
||||
float m_ = c.L - 0.1055613458f * c.a - 0.0638541728f * c.b;
|
||||
float s_ = c.L - 0.0894841775f * c.a - 1.2914855480f * c.b;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
return {
|
||||
+4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
|
||||
-1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
|
||||
-0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s,
|
||||
};
|
||||
}
|
||||
|
||||
// Finds the maximum saturation possible for a given hue that fits in sRGB
|
||||
// Saturation here is defined as S = C/L
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
float compute_max_saturation(float a, float b)
|
||||
{
|
||||
// Max saturation will be when one of r, g or b goes below zero.
|
||||
|
||||
// Select different coefficients depending on which component goes below zero first
|
||||
float k0, k1, k2, k3, k4, wl, wm, ws;
|
||||
|
||||
if (-1.88170328f * a - 0.80936493f * b > 1)
|
||||
{
|
||||
// Red component
|
||||
k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
|
||||
wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
|
||||
}
|
||||
else if (1.81444104f * a - 1.19445276f * b > 1)
|
||||
{
|
||||
// Green component
|
||||
k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
|
||||
wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Blue component
|
||||
k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
|
||||
wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
|
||||
}
|
||||
|
||||
// Approximate max saturation using a polynomial:
|
||||
float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
|
||||
|
||||
// Do one step Halley's method to get closer
|
||||
// this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
|
||||
// this should be sufficient for most applications, otherwise do two/three steps
|
||||
|
||||
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
||||
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
||||
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
||||
|
||||
{
|
||||
float l_ = 1.f + S * k_l;
|
||||
float m_ = 1.f + S * k_m;
|
||||
float s_ = 1.f + S * k_s;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
float l_dS = 3.f * k_l * l_ * l_;
|
||||
float m_dS = 3.f * k_m * m_ * m_;
|
||||
float s_dS = 3.f * k_s * s_ * s_;
|
||||
|
||||
float l_dS2 = 6.f * k_l * k_l * l_;
|
||||
float m_dS2 = 6.f * k_m * k_m * m_;
|
||||
float s_dS2 = 6.f * k_s * k_s * s_;
|
||||
|
||||
float f = wl * l + wm * m + ws * s;
|
||||
float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
|
||||
float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
|
||||
|
||||
S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
|
||||
}
|
||||
|
||||
return S;
|
||||
}
|
||||
|
||||
// finds L_cusp and C_cusp for a given hue
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
LC find_cusp(float a, float b)
|
||||
{
|
||||
// First, find the maximum saturation (saturation S = C/L)
|
||||
float S_cusp = compute_max_saturation(a, b);
|
||||
|
||||
// Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
|
||||
RGB rgb_at_max = oklab_to_linear_srgb({ 1, S_cusp * a, S_cusp * b });
|
||||
float L_cusp = cbrtf(1.f / fmax(fmax(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
|
||||
float C_cusp = L_cusp * S_cusp;
|
||||
|
||||
return { L_cusp , C_cusp };
|
||||
}
|
||||
|
||||
// Finds intersection of the line defined by
|
||||
// L = L0 * (1 - t) + t * L1;
|
||||
// C = t * C1;
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
float find_gamut_intersection(float a, float b, float L1, float C1, float L0, LC cusp)
|
||||
{
|
||||
// Find the intersection for upper and lower half seprately
|
||||
float t;
|
||||
if (((L1 - L0) * cusp.C - (cusp.L - L0) * C1) <= 0.f)
|
||||
{
|
||||
// Lower half
|
||||
|
||||
t = cusp.C * L0 / (C1 * cusp.L + cusp.C * (L0 - L1));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Upper half
|
||||
|
||||
// First intersect with triangle
|
||||
t = cusp.C * (L0 - 1.f) / (C1 * (cusp.L - 1.f) + cusp.C * (L0 - L1));
|
||||
|
||||
// Then one step Halley's method
|
||||
{
|
||||
float dL = L1 - L0;
|
||||
float dC = C1;
|
||||
|
||||
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
||||
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
||||
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
||||
|
||||
float l_dt = dL + dC * k_l;
|
||||
float m_dt = dL + dC * k_m;
|
||||
float s_dt = dL + dC * k_s;
|
||||
|
||||
|
||||
// If higher accuracy is required, 2 or 3 iterations of the following block can be used:
|
||||
{
|
||||
float L = L0 * (1.f - t) + t * L1;
|
||||
float C = t * C1;
|
||||
|
||||
float l_ = L + C * k_l;
|
||||
float m_ = L + C * k_m;
|
||||
float s_ = L + C * k_s;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
float ldt = 3 * l_dt * l_ * l_;
|
||||
float mdt = 3 * m_dt * m_ * m_;
|
||||
float sdt = 3 * s_dt * s_ * s_;
|
||||
|
||||
float ldt2 = 6 * l_dt * l_dt * l_;
|
||||
float mdt2 = 6 * m_dt * m_dt * m_;
|
||||
float sdt2 = 6 * s_dt * s_dt * s_;
|
||||
|
||||
float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1;
|
||||
float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
|
||||
float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
|
||||
|
||||
float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
|
||||
float t_r = -r * u_r;
|
||||
|
||||
float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1;
|
||||
float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
|
||||
float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
|
||||
|
||||
float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
|
||||
float t_g = -g * u_g;
|
||||
|
||||
b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1;
|
||||
float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
|
||||
float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
|
||||
|
||||
float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
|
||||
float t_b = -b * u_b;
|
||||
|
||||
t_r = u_r >= 0.f ? t_r : FLT_MAX;
|
||||
t_g = u_g >= 0.f ? t_g : FLT_MAX;
|
||||
t_b = u_b >= 0.f ? t_b : FLT_MAX;
|
||||
|
||||
t += fmin(t_r, fmin(t_g, t_b));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
float find_gamut_intersection(float a, float b, float L1, float C1, float L0)
|
||||
{
|
||||
// Find the cusp of the gamut triangle
|
||||
LC cusp = find_cusp(a, b);
|
||||
|
||||
return find_gamut_intersection(a, b, L1, C1, L0, cusp);
|
||||
}
|
||||
|
||||
RGB gamut_clip_preserve_chroma(RGB rgb)
|
||||
{
|
||||
if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
|
||||
return rgb;
|
||||
|
||||
Lab lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.L;
|
||||
float eps = 0.00001f;
|
||||
float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
float L0 = clamp(L, 0, 1);
|
||||
|
||||
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1 - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
|
||||
}
|
||||
|
||||
RGB gamut_clip_project_to_0_5(RGB rgb)
|
||||
{
|
||||
if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
|
||||
return rgb;
|
||||
|
||||
Lab lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.L;
|
||||
float eps = 0.00001f;
|
||||
float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
float L0 = 0.5;
|
||||
|
||||
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1 - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
|
||||
}
|
||||
|
||||
RGB gamut_clip_project_to_L_cusp(RGB rgb)
|
||||
{
|
||||
if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
|
||||
return rgb;
|
||||
|
||||
Lab lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.L;
|
||||
float eps = 0.00001f;
|
||||
float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
||||
LC cusp = find_cusp(a_, b_);
|
||||
|
||||
float L0 = cusp.L;
|
||||
|
||||
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
||||
|
||||
float L_clipped = L0 * (1 - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
|
||||
}
|
||||
|
||||
RGB gamut_clip_adaptive_L0_0_5(RGB rgb, float alpha = 0.05f)
|
||||
{
|
||||
if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
|
||||
return rgb;
|
||||
|
||||
Lab lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.L;
|
||||
float eps = 0.00001f;
|
||||
float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
float Ld = L - 0.5f;
|
||||
float e1 = 0.5f + fabs(Ld) + alpha * C;
|
||||
float L0 = 0.5f * (1.f + sgn(Ld) * (e1 - sqrtf(e1 * e1 - 2.f * fabs(Ld))));
|
||||
|
||||
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
|
||||
}
|
||||
|
||||
RGB gamut_clip_adaptive_L0_L_cusp(RGB rgb, float alpha = 0.05f)
|
||||
{
|
||||
if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
|
||||
return rgb;
|
||||
|
||||
Lab lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.L;
|
||||
float eps = 0.00001f;
|
||||
float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
||||
LC cusp = find_cusp(a_, b_);
|
||||
|
||||
float Ld = L - cusp.L;
|
||||
float k = 2.f * (Ld > 0 ? 1.f - cusp.L : cusp.L);
|
||||
|
||||
float e1 = 0.5f * k + fabs(Ld) + alpha * C / k;
|
||||
float L0 = cusp.L + 0.5f * (sgn(Ld) * (e1 - sqrtf(e1 * e1 - 2.f * k * fabs(Ld))));
|
||||
|
||||
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
|
||||
}
|
||||
|
||||
float toe(float x)
|
||||
{
|
||||
constexpr float k_1 = 0.206f;
|
||||
constexpr float k_2 = 0.03f;
|
||||
constexpr float k_3 = (1.f + k_1) / (1.f + k_2);
|
||||
return 0.5f * (k_3 * x - k_1 + sqrtf((k_3 * x - k_1) * (k_3 * x - k_1) + 4 * k_2 * k_3 * x));
|
||||
}
|
||||
|
||||
float toe_inv(float x)
|
||||
{
|
||||
constexpr float k_1 = 0.206f;
|
||||
constexpr float k_2 = 0.03f;
|
||||
constexpr float k_3 = (1.f + k_1) / (1.f + k_2);
|
||||
return (x * x + k_1 * x) / (k_3 * (x + k_2));
|
||||
}
|
||||
|
||||
ST to_ST(LC cusp)
|
||||
{
|
||||
float L = cusp.L;
|
||||
float C = cusp.C;
|
||||
return { C / L, C / (1 - L) };
|
||||
}
|
||||
|
||||
// Returns a smooth approximation of the location of the cusp
|
||||
// This polynomial was created by an optimization process
|
||||
// It has been designed so that S_mid < S_max and T_mid < T_max
|
||||
ST get_ST_mid(float a_, float b_)
|
||||
{
|
||||
float S = 0.11516993f + 1.f / (
|
||||
+7.44778970f + 4.15901240f * b_
|
||||
+ a_ * (-2.19557347f + 1.75198401f * b_
|
||||
+ a_ * (-2.13704948f - 10.02301043f * b_
|
||||
+ a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
|
||||
)))
|
||||
);
|
||||
|
||||
float T = 0.11239642f + 1.f / (
|
||||
+1.61320320f - 0.68124379f * b_
|
||||
+ a_ * (+0.40370612f + 0.90148123f * b_
|
||||
+ a_ * (-0.27087943f + 0.61223990f * b_
|
||||
+ a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
|
||||
)))
|
||||
);
|
||||
|
||||
return { S, T };
|
||||
}
|
||||
|
||||
struct Cs { float C_0; float C_mid; float C_max; };
|
||||
Cs get_Cs(float L, float a_, float b_)
|
||||
{
|
||||
LC cusp = find_cusp(a_, b_);
|
||||
|
||||
float C_max = find_gamut_intersection(a_, b_, L, 1, L, cusp);
|
||||
ST ST_max = to_ST(cusp);
|
||||
|
||||
// Scale factor to compensate for the curved part of gamut shape:
|
||||
float k = C_max / fmin((L * ST_max.S), (1 - L) * ST_max.T);
|
||||
|
||||
float C_mid;
|
||||
{
|
||||
ST ST_mid = get_ST_mid(a_, b_);
|
||||
|
||||
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
||||
float C_a = L * ST_mid.S;
|
||||
float C_b = (1.f - L) * ST_mid.T;
|
||||
C_mid = 0.9f * k * sqrtf(sqrtf(1.f / (1.f / (C_a * C_a * C_a * C_a) + 1.f / (C_b * C_b * C_b * C_b))));
|
||||
}
|
||||
|
||||
float C_0;
|
||||
{
|
||||
// for C_0, the shape is independent of hue, so ST are constant. Values picked to roughly be the average values of ST.
|
||||
float C_a = L * 0.4f;
|
||||
float C_b = (1.f - L) * 0.8f;
|
||||
|
||||
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
||||
C_0 = sqrtf(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
|
||||
}
|
||||
|
||||
return { C_0, C_mid, C_max };
|
||||
}
|
||||
|
||||
RGB okhsl_to_srgb(HSL hsl)
|
||||
{
|
||||
float h = hsl.h;
|
||||
float s = hsl.s;
|
||||
float l = hsl.l;
|
||||
|
||||
if (l == 1.0f)
|
||||
{
|
||||
return { 1.f, 1.f, 1.f };
|
||||
}
|
||||
|
||||
else if (l == 0.f)
|
||||
{
|
||||
return { 0.f, 0.f, 0.f };
|
||||
}
|
||||
|
||||
float a_ = cosf(2.f * pi * h);
|
||||
float b_ = sinf(2.f * pi * h);
|
||||
float L = toe_inv(l);
|
||||
|
||||
Cs cs = get_Cs(L, a_, b_);
|
||||
float C_0 = cs.C_0;
|
||||
float C_mid = cs.C_mid;
|
||||
float C_max = cs.C_max;
|
||||
|
||||
float mid = 0.8f;
|
||||
float mid_inv = 1.25f;
|
||||
|
||||
float C, t, k_0, k_1, k_2;
|
||||
|
||||
if (s < mid)
|
||||
{
|
||||
t = mid_inv * s;
|
||||
|
||||
k_1 = mid * C_0;
|
||||
k_2 = (1.f - k_1 / C_mid);
|
||||
|
||||
C = t * k_1 / (1.f - k_2 * t);
|
||||
}
|
||||
else
|
||||
{
|
||||
t = (s - mid)/ (1 - mid);
|
||||
|
||||
k_0 = C_mid;
|
||||
k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
||||
k_2 = (1.f - (k_1) / (C_max - C_mid));
|
||||
|
||||
C = k_0 + t * k_1 / (1.f - k_2 * t);
|
||||
}
|
||||
|
||||
RGB rgb = oklab_to_linear_srgb({ L, C * a_, C * b_ });
|
||||
return {
|
||||
srgb_transfer_function(rgb.r),
|
||||
srgb_transfer_function(rgb.g),
|
||||
srgb_transfer_function(rgb.b),
|
||||
};
|
||||
}
|
||||
|
||||
HSL srgb_to_okhsl(RGB rgb)
|
||||
{
|
||||
Lab lab = linear_srgb_to_oklab({
|
||||
srgb_transfer_function_inv(rgb.r),
|
||||
srgb_transfer_function_inv(rgb.g),
|
||||
srgb_transfer_function_inv(rgb.b)
|
||||
});
|
||||
|
||||
float C = sqrtf(lab.a * lab.a + lab.b * lab.b);
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
float L = lab.L;
|
||||
float h = 0.5f + 0.5f * atan2f(-lab.b, -lab.a) / pi;
|
||||
|
||||
Cs cs = get_Cs(L, a_, b_);
|
||||
float C_0 = cs.C_0;
|
||||
float C_mid = cs.C_mid;
|
||||
float C_max = cs.C_max;
|
||||
|
||||
// Inverse of the interpolation in okhsl_to_srgb:
|
||||
|
||||
float mid = 0.8f;
|
||||
float mid_inv = 1.25f;
|
||||
|
||||
float s;
|
||||
if (C < C_mid)
|
||||
{
|
||||
float k_1 = mid * C_0;
|
||||
float k_2 = (1.f - k_1 / C_mid);
|
||||
|
||||
float t = C / (k_1 + k_2 * C);
|
||||
s = t * mid;
|
||||
}
|
||||
else
|
||||
{
|
||||
float k_0 = C_mid;
|
||||
float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
||||
float k_2 = (1.f - (k_1) / (C_max - C_mid));
|
||||
|
||||
float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
|
||||
s = mid + (1.f - mid) * t;
|
||||
}
|
||||
|
||||
float l = toe(L);
|
||||
return { h, s, l };
|
||||
}
|
||||
|
||||
|
||||
RGB okhsv_to_srgb(HSV hsv)
|
||||
{
|
||||
float h = hsv.h;
|
||||
float s = hsv.s;
|
||||
float v = hsv.v;
|
||||
|
||||
float a_ = cosf(2.f * pi * h);
|
||||
float b_ = sinf(2.f * pi * h);
|
||||
|
||||
LC cusp = find_cusp(a_, b_);
|
||||
ST ST_max = to_ST(cusp);
|
||||
float S_max = ST_max.S;
|
||||
float T_max = ST_max.T;
|
||||
float S_0 = 0.5f;
|
||||
float k = 1 - S_0 / S_max;
|
||||
|
||||
// first we compute L and V as if the gamut is a perfect triangle:
|
||||
|
||||
// L, C when v==1:
|
||||
float L_v = 1 - s * S_0 / (S_0 + T_max - T_max * k * s);
|
||||
float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
|
||||
|
||||
float L = v * L_v;
|
||||
float C = v * C_v;
|
||||
|
||||
// then we compensate for both toe and the curved top part of the triangle:
|
||||
float L_vt = toe_inv(L_v);
|
||||
float C_vt = C_v * L_vt / L_v;
|
||||
|
||||
float L_new = toe_inv(L);
|
||||
C = C * L_new / L;
|
||||
L = L_new;
|
||||
|
||||
RGB rgb_scale = oklab_to_linear_srgb({ L_vt, a_ * C_vt, b_ * C_vt });
|
||||
float scale_L = cbrtf(1.f / fmax(fmax(rgb_scale.r, rgb_scale.g), fmax(rgb_scale.b, 0.f)));
|
||||
|
||||
L = L * scale_L;
|
||||
C = C * scale_L;
|
||||
|
||||
RGB rgb = oklab_to_linear_srgb({ L, C * a_, C * b_ });
|
||||
return {
|
||||
srgb_transfer_function(rgb.r),
|
||||
srgb_transfer_function(rgb.g),
|
||||
srgb_transfer_function(rgb.b),
|
||||
};
|
||||
}
|
||||
|
||||
HSV srgb_to_okhsv(RGB rgb)
|
||||
{
|
||||
Lab lab = linear_srgb_to_oklab({
|
||||
srgb_transfer_function_inv(rgb.r),
|
||||
srgb_transfer_function_inv(rgb.g),
|
||||
srgb_transfer_function_inv(rgb.b)
|
||||
});
|
||||
|
||||
float C = sqrtf(lab.a * lab.a + lab.b * lab.b);
|
||||
float a_ = lab.a / C;
|
||||
float b_ = lab.b / C;
|
||||
|
||||
float L = lab.L;
|
||||
float h = 0.5f + 0.5f * atan2f(-lab.b, -lab.a) / pi;
|
||||
|
||||
LC cusp = find_cusp(a_, b_);
|
||||
ST ST_max = to_ST(cusp);
|
||||
float S_max = ST_max.S;
|
||||
float T_max = ST_max.T;
|
||||
float S_0 = 0.5f;
|
||||
float k = 1 - S_0 / S_max;
|
||||
|
||||
// first we find L_v, C_v, L_vt and C_vt
|
||||
|
||||
float t = T_max / (C + L * T_max);
|
||||
float L_v = t * L;
|
||||
float C_v = t * C;
|
||||
|
||||
float L_vt = toe_inv(L_v);
|
||||
float C_vt = C_v * L_vt / L_v;
|
||||
|
||||
// we can then use these to invert the step that compensates for the toe and the curved top part of the triangle:
|
||||
RGB rgb_scale = oklab_to_linear_srgb({ L_vt, a_ * C_vt, b_ * C_vt });
|
||||
float scale_L = cbrtf(1.f / fmax(fmax(rgb_scale.r, rgb_scale.g), fmax(rgb_scale.b, 0.f)));
|
||||
|
||||
L = L / scale_L;
|
||||
C = C / scale_L;
|
||||
|
||||
C = C * toe(L) / L;
|
||||
L = toe(L);
|
||||
|
||||
// we can now compute v and s:
|
||||
|
||||
float v = L / L_v;
|
||||
float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
|
||||
|
||||
return { h, s, v };
|
||||
}
|
||||
|
||||
};
|
||||
#endif // OK_COLOR_H
|
|
@ -0,0 +1,663 @@
|
|||
// Copyright(c) 2021 Björn Ottosson
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files(the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and /or sell copies
|
||||
// of the Software, and to permit persons to whom the Software is furnished to do
|
||||
// so, subject to the following conditions :
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#ifndef OK_COLOR_SHADER_H
|
||||
#define OK_COLOR_SHADER_H
|
||||
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
static String OK_COLOR_SHADER = R"(shader_type canvas_item;
|
||||
|
||||
const float M_PI = 3.1415926535897932384626433832795;
|
||||
|
||||
float cbrt( float x )
|
||||
{
|
||||
return sign(x)*pow(abs(x),1.0f/3.0f);
|
||||
}
|
||||
|
||||
float srgb_transfer_function(float a)
|
||||
{
|
||||
return .0031308f >= a ? 12.92f * a : 1.055f * pow(a, .4166666666666667f) - .055f;
|
||||
}
|
||||
|
||||
float srgb_transfer_function_inv(float a)
|
||||
{
|
||||
return .04045f < a ? pow((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
|
||||
}
|
||||
|
||||
vec3 linear_srgb_to_oklab(vec3 c)
|
||||
{
|
||||
float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
|
||||
float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
|
||||
float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
|
||||
|
||||
float l_ = cbrt(l);
|
||||
float m_ = cbrt(m);
|
||||
float s_ = cbrt(s);
|
||||
|
||||
return vec3(
|
||||
0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
|
||||
1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
|
||||
0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_
|
||||
);
|
||||
}
|
||||
|
||||
vec3 oklab_to_linear_srgb(vec3 c)
|
||||
{
|
||||
float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
|
||||
float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
|
||||
float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
return vec3(
|
||||
+4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
|
||||
-1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
|
||||
-0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s
|
||||
);
|
||||
}
|
||||
|
||||
// Finds the maximum saturation possible for a given hue that fits in sRGB
|
||||
// Saturation here is defined as S = C/L
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
float compute_max_saturation(float a, float b)
|
||||
{
|
||||
// Max saturation will be when one of r, g or b goes below zero.
|
||||
|
||||
// Select different coefficients depending on which component goes below zero first
|
||||
float k0, k1, k2, k3, k4, wl, wm, ws;
|
||||
|
||||
if (-1.88170328f * a - 0.80936493f * b > 1.f)
|
||||
{
|
||||
// Red component
|
||||
k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
|
||||
wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
|
||||
}
|
||||
else if (1.81444104f * a - 1.19445276f * b > 1.f)
|
||||
{
|
||||
// Green component
|
||||
k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
|
||||
wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Blue component
|
||||
k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
|
||||
wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
|
||||
}
|
||||
|
||||
// Approximate max saturation using a polynomial:
|
||||
float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
|
||||
|
||||
// Do one step Halley's method to get closer
|
||||
// this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
|
||||
// this should be sufficient for most applications, otherwise do two/three steps
|
||||
|
||||
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
||||
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
||||
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
||||
|
||||
{
|
||||
float l_ = 1.f + S * k_l;
|
||||
float m_ = 1.f + S * k_m;
|
||||
float s_ = 1.f + S * k_s;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
float l_dS = 3.f * k_l * l_ * l_;
|
||||
float m_dS = 3.f * k_m * m_ * m_;
|
||||
float s_dS = 3.f * k_s * s_ * s_;
|
||||
|
||||
float l_dS2 = 6.f * k_l * k_l * l_;
|
||||
float m_dS2 = 6.f * k_m * k_m * m_;
|
||||
float s_dS2 = 6.f * k_s * k_s * s_;
|
||||
|
||||
float f = wl * l + wm * m + ws * s;
|
||||
float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
|
||||
float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
|
||||
|
||||
S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
|
||||
}
|
||||
|
||||
return S;
|
||||
}
|
||||
|
||||
// finds L_cusp and C_cusp for a given hue
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
vec2 find_cusp(float a, float b)
|
||||
{
|
||||
// First, find the maximum saturation (saturation S = C/L)
|
||||
float S_cusp = compute_max_saturation(a, b);
|
||||
|
||||
// Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
|
||||
vec3 rgb_at_max = oklab_to_linear_srgb(vec3( 1, S_cusp * a, S_cusp * b ));
|
||||
float L_cusp = cbrt(1.f / max(max(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
|
||||
float C_cusp = L_cusp * S_cusp;
|
||||
|
||||
return vec2( L_cusp , C_cusp );
|
||||
} )"
|
||||
R"(// Finds intersection of the line defined by
|
||||
// L = L0 * (1 - t) + t * L1;
|
||||
// C = t * C1;
|
||||
// a and b must be normalized so a^2 + b^2 == 1
|
||||
float find_gamut_intersection(float a, float b, float L1, float C1, float L0, vec2 cusp)
|
||||
{
|
||||
// Find the intersection for upper and lower half seprately
|
||||
float t;
|
||||
if (((L1 - L0) * cusp.y - (cusp.x - L0) * C1) <= 0.f)
|
||||
{
|
||||
// Lower half
|
||||
|
||||
t = cusp.y * L0 / (C1 * cusp.x + cusp.y * (L0 - L1));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Upper half
|
||||
|
||||
// First intersect with triangle
|
||||
t = cusp.y * (L0 - 1.f) / (C1 * (cusp.x - 1.f) + cusp.y * (L0 - L1));
|
||||
|
||||
// Then one step Halley's method
|
||||
{
|
||||
float dL = L1 - L0;
|
||||
float dC = C1;
|
||||
|
||||
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
||||
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
||||
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
||||
|
||||
float l_dt = dL + dC * k_l;
|
||||
float m_dt = dL + dC * k_m;
|
||||
float s_dt = dL + dC * k_s;
|
||||
|
||||
|
||||
// If higher accuracy is required, 2 or 3 iterations of the following block can be used:
|
||||
{
|
||||
float L = L0 * (1.f - t) + t * L1;
|
||||
float C = t * C1;
|
||||
|
||||
float l_ = L + C * k_l;
|
||||
float m_ = L + C * k_m;
|
||||
float s_ = L + C * k_s;
|
||||
|
||||
float l = l_ * l_ * l_;
|
||||
float m = m_ * m_ * m_;
|
||||
float s = s_ * s_ * s_;
|
||||
|
||||
float ldt = 3.f * l_dt * l_ * l_;
|
||||
float mdt = 3.f * m_dt * m_ * m_;
|
||||
float sdt = 3.f * s_dt * s_ * s_;
|
||||
|
||||
float ldt2 = 6.f * l_dt * l_dt * l_;
|
||||
float mdt2 = 6.f * m_dt * m_dt * m_;
|
||||
float sdt2 = 6.f * s_dt * s_dt * s_;
|
||||
|
||||
float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1.f;
|
||||
float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
|
||||
float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
|
||||
|
||||
float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
|
||||
float t_r = -r * u_r;
|
||||
|
||||
float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1.f;
|
||||
float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
|
||||
float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
|
||||
|
||||
float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
|
||||
float t_g = -g * u_g;
|
||||
|
||||
float b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1.f;
|
||||
float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
|
||||
float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
|
||||
|
||||
float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
|
||||
float t_b = -b * u_b;
|
||||
|
||||
t_r = u_r >= 0.f ? t_r : 10000.f;
|
||||
t_g = u_g >= 0.f ? t_g : 10000.f;
|
||||
t_b = u_b >= 0.f ? t_b : 10000.f;
|
||||
|
||||
t += min(t_r, min(t_g, t_b));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
float find_gamut_intersection_5(float a, float b, float L1, float C1, float L0)
|
||||
{
|
||||
// Find the cusp of the gamut triangle
|
||||
vec2 cusp = find_cusp(a, b);
|
||||
|
||||
return find_gamut_intersection(a, b, L1, C1, L0, cusp);
|
||||
})"
|
||||
R"(
|
||||
|
||||
vec3 gamut_clip_preserve_chroma(vec3 rgb)
|
||||
{
|
||||
if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
|
||||
return rgb;
|
||||
|
||||
vec3 lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.x;
|
||||
float eps = 0.00001f;
|
||||
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
float L0 = clamp(L, 0.f, 1.f);
|
||||
|
||||
float t = find_gamut_intersection_5(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
||||
}
|
||||
|
||||
vec3 gamut_clip_project_to_0_5(vec3 rgb)
|
||||
{
|
||||
if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
|
||||
return rgb;
|
||||
|
||||
vec3 lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.x;
|
||||
float eps = 0.00001f;
|
||||
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
float L0 = 0.5;
|
||||
|
||||
float t = find_gamut_intersection_5(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
||||
}
|
||||
|
||||
vec3 gamut_clip_project_to_L_cusp(vec3 rgb)
|
||||
{
|
||||
if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
|
||||
return rgb;
|
||||
|
||||
vec3 lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.x;
|
||||
float eps = 0.00001f;
|
||||
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
||||
vec2 cusp = find_cusp(a_, b_);
|
||||
|
||||
float L0 = cusp.x;
|
||||
|
||||
float t = find_gamut_intersection_5(a_, b_, L, C, L0);
|
||||
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
||||
}
|
||||
|
||||
vec3 gamut_clip_adaptive_L0_0_5(vec3 rgb, float alpha)
|
||||
{
|
||||
if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
|
||||
return rgb;
|
||||
|
||||
vec3 lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.x;
|
||||
float eps = 0.00001f;
|
||||
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
float Ld = L - 0.5f;
|
||||
float e1 = 0.5f + abs(Ld) + alpha * C;
|
||||
float L0 = 0.5f * (1.f + sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * abs(Ld))));
|
||||
|
||||
float t = find_gamut_intersection_5(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
||||
}
|
||||
|
||||
vec3 gamut_clip_adaptive_L0_L_cusp(vec3 rgb, float alpha)
|
||||
{
|
||||
if (rgb.r < 1.f && rgb.g < 1.f && rgb.b < 1.f && rgb.r > 0.f && rgb.g > 0.f && rgb.b > 0.f)
|
||||
return rgb;
|
||||
|
||||
vec3 lab = linear_srgb_to_oklab(rgb);
|
||||
|
||||
float L = lab.x;
|
||||
float eps = 0.00001f;
|
||||
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
||||
vec2 cusp = find_cusp(a_, b_);
|
||||
|
||||
float Ld = L - cusp.x;
|
||||
float k = 2.f * (Ld > 0.f ? 1.f - cusp.x : cusp.x);
|
||||
|
||||
float e1 = 0.5f * k + abs(Ld) + alpha * C / k;
|
||||
float L0 = cusp.x + 0.5f * (sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * k * abs(Ld))));
|
||||
|
||||
float t = find_gamut_intersection_5(a_, b_, L, C, L0);
|
||||
float L_clipped = L0 * (1.f - t) + t * L;
|
||||
float C_clipped = t * C;
|
||||
|
||||
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
||||
}
|
||||
|
||||
float toe(float x)
|
||||
{
|
||||
float k_1 = 0.206f;
|
||||
float k_2 = 0.03f;
|
||||
float k_3 = (1.f + k_1) / (1.f + k_2);
|
||||
return 0.5f * (k_3 * x - k_1 + sqrt((k_3 * x - k_1) * (k_3 * x - k_1) + 4.f * k_2 * k_3 * x));
|
||||
}
|
||||
|
||||
float toe_inv(float x)
|
||||
{
|
||||
float k_1 = 0.206f;
|
||||
float k_2 = 0.03f;
|
||||
float k_3 = (1.f + k_1) / (1.f + k_2);
|
||||
return (x * x + k_1 * x) / (k_3 * (x + k_2));
|
||||
}
|
||||
)"
|
||||
R"(vec2 to_ST(vec2 cusp)
|
||||
{
|
||||
float L = cusp.x;
|
||||
float C = cusp.y;
|
||||
return vec2( C / L, C / (1.f - L) );
|
||||
}
|
||||
|
||||
// Returns a smooth approximation of the location of the cusp
|
||||
// This polynomial was created by an optimization process
|
||||
// It has been designed so that S_mid < S_max and T_mid < T_max
|
||||
vec2 get_ST_mid(float a_, float b_)
|
||||
{
|
||||
float S = 0.11516993f + 1.f / (
|
||||
+7.44778970f + 4.15901240f * b_
|
||||
+ a_ * (-2.19557347f + 1.75198401f * b_
|
||||
+ a_ * (-2.13704948f - 10.02301043f * b_
|
||||
+ a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
|
||||
)))
|
||||
);
|
||||
|
||||
float T = 0.11239642f + 1.f / (
|
||||
+1.61320320f - 0.68124379f * b_
|
||||
+ a_ * (+0.40370612f + 0.90148123f * b_
|
||||
+ a_ * (-0.27087943f + 0.61223990f * b_
|
||||
+ a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
|
||||
)))
|
||||
);
|
||||
|
||||
return vec2( S, T );
|
||||
}
|
||||
|
||||
vec3 get_Cs(float L, float a_, float b_)
|
||||
{
|
||||
vec2 cusp = find_cusp(a_, b_);
|
||||
|
||||
float C_max = find_gamut_intersection(a_, b_, L, 1.f, L, cusp);
|
||||
vec2 ST_max = to_ST(cusp);
|
||||
|
||||
// Scale factor to compensate for the curved part of gamut shape:
|
||||
float k = C_max / min((L * ST_max.x), (1.f - L) * ST_max.y);
|
||||
|
||||
float C_mid;
|
||||
{
|
||||
vec2 ST_mid = get_ST_mid(a_, b_);
|
||||
|
||||
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
||||
float C_a = L * ST_mid.x;
|
||||
float C_b = (1.f - L) * ST_mid.y;
|
||||
C_mid = 0.9f * k * sqrt(sqrt(1.f / (1.f / (C_a * C_a * C_a * C_a) + 1.f / (C_b * C_b * C_b * C_b))));
|
||||
}
|
||||
|
||||
float C_0;
|
||||
{
|
||||
// for C_0, the shape is independent of hue, so vec2 are constant. Values picked to roughly be the average values of vec2.
|
||||
float C_a = L * 0.4f;
|
||||
float C_b = (1.f - L) * 0.8f;
|
||||
|
||||
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
||||
C_0 = sqrt(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
|
||||
}
|
||||
|
||||
return vec3( C_0, C_mid, C_max );
|
||||
}
|
||||
|
||||
vec3 okhsl_to_srgb(vec3 hsl)
|
||||
{
|
||||
float h = hsl.x;
|
||||
float s = hsl.y;
|
||||
float l = hsl.z;
|
||||
|
||||
if (l == 1.0f)
|
||||
{
|
||||
return vec3( 1.f, 1.f, 1.f );
|
||||
}
|
||||
|
||||
else if (l == 0.f)
|
||||
{
|
||||
return vec3( 0.f, 0.f, 0.f );
|
||||
}
|
||||
|
||||
float a_ = cos(2.f * M_PI * h);
|
||||
float b_ = sin(2.f * M_PI * h);
|
||||
float L = toe_inv(l);
|
||||
|
||||
vec3 cs = get_Cs(L, a_, b_);
|
||||
float C_0 = cs.x;
|
||||
float C_mid = cs.y;
|
||||
float C_max = cs.z;
|
||||
|
||||
float mid = 0.8f;
|
||||
float mid_inv = 1.25f;
|
||||
|
||||
float C, t, k_0, k_1, k_2;
|
||||
|
||||
if (s < mid)
|
||||
{
|
||||
t = mid_inv * s;
|
||||
|
||||
k_1 = mid * C_0;
|
||||
k_2 = (1.f - k_1 / C_mid);
|
||||
|
||||
C = t * k_1 / (1.f - k_2 * t);
|
||||
}
|
||||
else
|
||||
{
|
||||
t = (s - mid)/ (1.f - mid);
|
||||
|
||||
k_0 = C_mid;
|
||||
k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
||||
k_2 = (1.f - (k_1) / (C_max - C_mid));
|
||||
|
||||
C = k_0 + t * k_1 / (1.f - k_2 * t);
|
||||
}
|
||||
|
||||
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
||||
return vec3(
|
||||
srgb_transfer_function(rgb.r),
|
||||
srgb_transfer_function(rgb.g),
|
||||
srgb_transfer_function(rgb.b)
|
||||
);
|
||||
}
|
||||
|
||||
vec3 srgb_to_okhsl(vec3 rgb)
|
||||
{
|
||||
vec3 lab = linear_srgb_to_oklab(vec3(
|
||||
srgb_transfer_function_inv(rgb.r),
|
||||
srgb_transfer_function_inv(rgb.g),
|
||||
srgb_transfer_function_inv(rgb.b)
|
||||
));
|
||||
|
||||
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
float L = lab.x;
|
||||
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
||||
|
||||
vec3 cs = get_Cs(L, a_, b_);
|
||||
float C_0 = cs.x;
|
||||
float C_mid = cs.y;
|
||||
float C_max = cs.z;
|
||||
|
||||
// Inverse of the interpolation in okhsl_to_srgb:
|
||||
|
||||
float mid = 0.8f;
|
||||
float mid_inv = 1.25f;
|
||||
|
||||
float s;
|
||||
if (C < C_mid)
|
||||
{
|
||||
float k_1 = mid * C_0;
|
||||
float k_2 = (1.f - k_1 / C_mid);
|
||||
|
||||
float t = C / (k_1 + k_2 * C);
|
||||
s = t * mid;
|
||||
}
|
||||
else
|
||||
{
|
||||
float k_0 = C_mid;
|
||||
float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
||||
float k_2 = (1.f - (k_1) / (C_max - C_mid));
|
||||
|
||||
float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
|
||||
s = mid + (1.f - mid) * t;
|
||||
}
|
||||
|
||||
float l = toe(L);
|
||||
return vec3( h, s, l );
|
||||
}
|
||||
|
||||
|
||||
vec3 okhsv_to_srgb(vec3 hsv)
|
||||
{
|
||||
float h = hsv.x;
|
||||
float s = hsv.y;
|
||||
float v = hsv.z;
|
||||
|
||||
float a_ = cos(2.f * M_PI * h);
|
||||
float b_ = sin(2.f * M_PI * h);
|
||||
|
||||
vec2 cusp = find_cusp(a_, b_);
|
||||
vec2 ST_max = to_ST(cusp);
|
||||
float S_max = ST_max.x;
|
||||
float T_max = ST_max.y;
|
||||
float S_0 = 0.5f;
|
||||
float k = 1.f- S_0 / S_max;
|
||||
|
||||
// first we compute L and V as if the gamut is a perfect triangle:
|
||||
|
||||
// L, C when v==1:
|
||||
float L_v = 1.f - s * S_0 / (S_0 + T_max - T_max * k * s);
|
||||
float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
|
||||
|
||||
float L = v * L_v;
|
||||
float C = v * C_v;
|
||||
|
||||
// then we compensate for both toe and the curved top part of the triangle:
|
||||
float L_vt = toe_inv(L_v);
|
||||
float C_vt = C_v * L_vt / L_v;
|
||||
|
||||
float L_new = toe_inv(L);
|
||||
C = C * L_new / L;
|
||||
L = L_new;
|
||||
|
||||
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
||||
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
||||
|
||||
L = L * scale_L;
|
||||
C = C * scale_L;
|
||||
|
||||
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
||||
return vec3(
|
||||
srgb_transfer_function(rgb.r),
|
||||
srgb_transfer_function(rgb.g),
|
||||
srgb_transfer_function(rgb.b)
|
||||
);
|
||||
}
|
||||
)"
|
||||
R"(
|
||||
vec3 srgb_to_okhsv(vec3 rgb)
|
||||
{
|
||||
vec3 lab = linear_srgb_to_oklab(vec3(
|
||||
srgb_transfer_function_inv(rgb.r),
|
||||
srgb_transfer_function_inv(rgb.g),
|
||||
srgb_transfer_function_inv(rgb.b)
|
||||
));
|
||||
|
||||
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
||||
float a_ = lab.y / C;
|
||||
float b_ = lab.z / C;
|
||||
|
||||
float L = lab.x;
|
||||
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
||||
|
||||
vec2 cusp = find_cusp(a_, b_);
|
||||
vec2 ST_max = to_ST(cusp);
|
||||
float S_max = ST_max.x;
|
||||
float T_max = ST_max.y;
|
||||
float S_0 = 0.5f;
|
||||
float k = 1.f - S_0 / S_max;
|
||||
|
||||
// first we find L_v, C_v, L_vt and C_vt
|
||||
|
||||
float t = T_max / (C + L * T_max);
|
||||
float L_v = t * L;
|
||||
float C_v = t * C;
|
||||
|
||||
float L_vt = toe_inv(L_v);
|
||||
float C_vt = C_v * L_vt / L_v;
|
||||
|
||||
// we can then use these to invert the step that compensates for the toe and the curved top part of the triangle:
|
||||
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
||||
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
||||
|
||||
L = L / scale_L;
|
||||
C = C / scale_L;
|
||||
|
||||
C = C * toe(L) / L;
|
||||
L = toe(L);
|
||||
|
||||
// we can now compute v and s:
|
||||
|
||||
float v = L / L_v;
|
||||
float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
|
||||
|
||||
return vec3 (h, s, v );
|
||||
})";
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue