From 1c343cd54d9bdb684de2e793f2ab99bfe85ae0d2 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 24 Mar 2022 01:24:00 +0100 Subject: [PATCH] Expose Color's `to_linear()` and `to_srgb()` to scripting --- core/variant/variant_call.cpp | 2 ++ doc/classes/Color.xml | 13 +++++++++++++ tests/core/math/test_color.h | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index a02f9c58233..c11925fa8c4 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1656,6 +1656,8 @@ static void _register_variant_builtin_methods() { bind_method(Color, darkened, sarray("amount"), varray()); bind_method(Color, blend, sarray("over"), varray()); bind_method(Color, get_luminance, sarray(), varray()); + bind_method(Color, to_linear, sarray(), varray()); + bind_method(Color, to_srgb, sarray(), varray()); bind_method(Color, is_equal_approx, sarray("to"), varray()); diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml index e30d448a27f..5b48804d9d0 100644 --- a/doc/classes/Color.xml +++ b/doc/classes/Color.xml @@ -183,6 +183,7 @@ Returns the luminance of the color in the [code][0.0, 1.0][/code] range. This is useful when determining light or dark color. Colors with a luminance smaller than 0.5 can be generally considered dark. + [b]Note:[/b] [method get_luminance] relies on the colour being in the linear color space to return an accurate relative luminance value. If the color is in the sRGB color space, use [method to_linear] to convert it to the linear color space first. @@ -404,6 +405,12 @@ [/codeblocks] + + + + Returns the color converted to the linear color space. This assumes the original color is in the sRGB color space. See also [method to_srgb] which performs the opposite operation. + + @@ -436,6 +443,12 @@ [/codeblocks] + + + + Returns the color converted to the [url=https://en.wikipedia.org/wiki/SRGB]sRGB[/url] color space. This assumes the original color is in the linear color space. See also [method to_linear] which performs the opposite operation. + + diff --git a/tests/core/math/test_color.h b/tests/core/math/test_color.h index 702f17a9cfb..6f40f8ecc0e 100644 --- a/tests/core/math/test_color.h +++ b/tests/core/math/test_color.h @@ -144,6 +144,24 @@ TEST_CASE("[Color] Conversion methods") { "The string representation should match the expected value."); } +TEST_CASE("[Color] Linear <-> sRGB conversion") { + const Color color = Color(0.35, 0.5, 0.6, 0.7); + const Color color_linear = color.to_linear(); + const Color color_srgb = color.to_srgb(); + CHECK_MESSAGE( + color_linear.is_equal_approx(Color(0.100481, 0.214041, 0.318547, 0.7)), + "The color converted to linear color space should match the expected value."); + CHECK_MESSAGE( + color_srgb.is_equal_approx(Color(0.62621, 0.735357, 0.797738, 0.7)), + "The color converted to sRGB color space should match the expected value."); + CHECK_MESSAGE( + color_linear.to_srgb().is_equal_approx(Color(0.35, 0.5, 0.6, 0.7)), + "The linear color converted back to sRGB color space should match the expected value."); + CHECK_MESSAGE( + color_srgb.to_linear().is_equal_approx(Color(0.35, 0.5, 0.6, 0.7)), + "The sRGB color converted back to linear color space should match the expected value."); +} + TEST_CASE("[Color] Named colors") { CHECK_MESSAGE( Color::named("red").is_equal_approx(Color::hex(0xFF0000FF)),