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.
This commit is contained in:
Lucas CHOLLET 2024-10-27 12:58:33 -04:00 committed by Andreas Kling
parent 47992f7b39
commit ff6102430e
Notes: github-actions[bot] 2024-11-09 14:16:37 +00:00
2 changed files with 13 additions and 19 deletions

View file

@ -396,13 +396,8 @@ Vector<Color> 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

View file

@ -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/