Skip to content

Commit dac0b67

Browse files
committed
Merge pull request #100659 from clayjohn/color-srgb-precision
Increase precision in `linear_to_srgb()` and `srgb_to_linear()`
2 parents 315f51b + 46ce499 commit dac0b67

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

core/math/color.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ struct [[nodiscard]] Color {
187187

188188
_FORCE_INLINE_ Color srgb_to_linear() const {
189189
return Color(
190-
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
191-
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
192-
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
190+
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
191+
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
192+
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
193193
a);
194194
}
195195
_FORCE_INLINE_ Color linear_to_srgb() const {
196196
return Color(
197-
r < 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * Math::pow(r, 1.0f / 2.4f) - 0.055f,
198-
g < 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * Math::pow(g, 1.0f / 2.4f) - 0.055f,
199-
b < 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * Math::pow(b, 1.0f / 2.4f) - 0.055f, a);
197+
r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
198+
g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
199+
b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
200200
}
201201

202202
static Color hex(uint32_t p_hex);

tests/core/math/test_color.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ TEST_CASE("[Color] Linear <-> sRGB conversion") {
160160
CHECK_MESSAGE(
161161
color_srgb.srgb_to_linear().is_equal_approx(Color(0.35, 0.5, 0.6, 0.7)),
162162
"The sRGB color converted back to linear color space should match the expected value.");
163+
CHECK_MESSAGE(
164+
Color(1.0, 1.0, 1.0, 1.0).srgb_to_linear() == (Color(1.0, 1.0, 1.0, 1.0)),
165+
"White converted from sRGB to linear should remain white.");
166+
CHECK_MESSAGE(
167+
Color(1.0, 1.0, 1.0, 1.0).linear_to_srgb() == (Color(1.0, 1.0, 1.0, 1.0)),
168+
"White converted from linear to sRGB should remain white.");
163169
}
164170

165171
TEST_CASE("[Color] Named colors") {

0 commit comments

Comments
 (0)