From ff6102430e463d2d72fd99aaddda5f76fbb53fd5 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 27 Oct 2024 12:58:33 -0400 Subject: [PATCH] LibGfx: Add Color::from_linear_srgb() This will be usefully on its own later on when supporting this color space directly, but it also allows us to do some factorization in the current codebase. --- Userland/Libraries/LibGfx/Color.cpp | 17 +++++++++++------ Userland/Libraries/LibGfx/Color.h | 15 ++------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index b2653162d35..bebbd47cdd1 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -396,13 +396,8 @@ Vector Color::tints(u32 steps, float max) const return tints; } -Color Color::from_xyz50(float x, float y, float z, float alpha) +Color Color::from_linear_srgb(float red, float green, float blue, float alpha) { - // See commit description for these values - float red = 3.13397926 * x - 1.61689519 * y - 0.49070587 * z; - float green = -0.97840009 * x + 1.91589112 * y + 0.03339256 * z; - float blue = 0.07200357 * x - 0.22897505 * y + 1.40517398 * z; - auto linear_to_srgb = [](float c) { return c >= 0.0031308f ? 1.055f * pow(c, 0.4166666f) - 0.055f : 12.92f * c; }; @@ -418,6 +413,16 @@ Color Color::from_xyz50(float x, float y, float z, float alpha) clamp(lroundf(alpha * 255.f), 0, 255)); } +Color Color::from_xyz50(float x, float y, float z, float alpha) +{ + // See commit description for these values + float red = 3.13397926 * x - 1.61689519 * y - 0.49070587 * z; + float green = -0.97840009 * x + 1.91589112 * y + 0.03339256 * z; + float blue = 0.07200357 * x - 0.22897505 * y + 1.40517398 * z; + + return from_linear_srgb(red, green, blue, alpha); +} + Color Color::from_lab(float L, float a, float b, float alpha) { // Third edition of "Colorimetry" by the CIE diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index f02aa8c2d4e..df2c4c01cde 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -184,14 +184,11 @@ public: static Color from_lab(float L, float a, float b, float alpha = 1.0f); static Color from_xyz50(float x, float y, float z, float alpha = 1.0f); + static Color from_linear_srgb(float x, float y, float z, float alpha = 1.0f); // https://bottosson.github.io/posts/oklab/ static constexpr Color from_oklab(float L, float a, float b, float alpha = 1.0f) { - auto linear_to_srgb = [](float c) { - return c >= 0.0031308f ? 1.055f * pow(c, 0.4166666f) - 0.055f : 12.92f * c; - }; - float l = L + 0.3963377774f * a + 0.2158037573f * b; float m = L - 0.1055613458f * a - 0.0638541728f * b; float s = L - 0.0894841775f * a - 1.2914855480f * b; @@ -204,15 +201,7 @@ public: float green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s; float blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s; - red = linear_to_srgb(red) * 255.f; - green = linear_to_srgb(green) * 255.f; - blue = linear_to_srgb(blue) * 255.f; - - return Color( - clamp(lroundf(red), 0, 255), - clamp(lroundf(green), 0, 255), - clamp(lroundf(blue), 0, 255), - clamp(lroundf(alpha * 255.f), 0, 255)); + return from_linear_srgb(red, green, blue, alpha); } // https://bottosson.github.io/posts/oklab/