mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-22 09:18:55 +00:00
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:
parent
47992f7b39
commit
ff6102430e
Notes:
github-actions[bot]
2024-11-09 14:16:37 +00:00
Author: https://github.com/LucasChollet
Commit: ff6102430e
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2029
Reviewed-by: https://github.com/AtkinsSJ
Reviewed-by: https://github.com/gmta
Reviewed-by: https://github.com/tcl3
2 changed files with 13 additions and 19 deletions
|
@ -396,13 +396,8 @@ Vector<Color> Color::tints(u32 steps, float max) const
|
||||||
return tints;
|
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) {
|
auto linear_to_srgb = [](float c) {
|
||||||
return c >= 0.0031308f ? 1.055f * pow(c, 0.4166666f) - 0.055f : 12.92f * 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));
|
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)
|
Color Color::from_lab(float L, float a, float b, float alpha)
|
||||||
{
|
{
|
||||||
// Third edition of "Colorimetry" by the CIE
|
// Third edition of "Colorimetry" by the CIE
|
||||||
|
|
|
@ -184,14 +184,11 @@ public:
|
||||||
|
|
||||||
static Color from_lab(float L, float a, float b, float alpha = 1.0f);
|
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_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/
|
// https://bottosson.github.io/posts/oklab/
|
||||||
static constexpr Color from_oklab(float L, float a, float b, float alpha = 1.0f)
|
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 l = L + 0.3963377774f * a + 0.2158037573f * b;
|
||||||
float m = L - 0.1055613458f * a - 0.0638541728f * b;
|
float m = L - 0.1055613458f * a - 0.0638541728f * b;
|
||||||
float s = L - 0.0894841775f * a - 1.2914855480f * 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 green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
|
||||||
float blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
|
float blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
|
||||||
|
|
||||||
red = linear_to_srgb(red) * 255.f;
|
return from_linear_srgb(red, green, blue, alpha);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://bottosson.github.io/posts/oklab/
|
// https://bottosson.github.io/posts/oklab/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue