colorutils: Add some more utils

Add oklab<>srgb-linear and hsl<>rgb conversions.
This commit is contained in:
Matthias Clasen 2024-05-30 16:36:36 -04:00
parent 0c307f7563
commit f790441505
2 changed files with 64 additions and 15 deletions

View File

@ -229,6 +229,32 @@ gtk_rgb_to_hsv (float r, float g, float b,
*v = b;
}
void
gtk_rgb_to_hsl (float red, float green, float blue,
float *hue, float *saturation, float *lightness)
{
GdkHSLA hsla;
_gdk_hsla_init_from_rgba (&hsla, &(GdkRGBA) { red, green, blue, 1.0 });
*hue = hsla.hue;
*saturation = hsla.saturation;
*lightness = hsla.lightness;
}
void
gtk_hsl_to_rgb (float hue, float saturation, float lightness,
float *red, float *green, float *blue)
{
GdkRGBA rgba;
_gdk_rgba_init_from_hsla (&rgba, &(GdkHSLA) { hue, saturation, lightness, 1.0 });
*red = rgba.red;
*green = rgba.green;
*blue = rgba.blue;
}
void
gtk_rgb_to_hwb (float red, float green, float blue,
float *hue, float *white, float *black)
@ -320,8 +346,8 @@ unapply_gamma (float v)
}
void
gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue)
gtk_oklab_to_linear_srgb (float L, float a, float b,
float *red, float *green, float *blue)
{
float l = L + 0.3963377774f * a + 0.2158037573f * b;
float m = L - 0.1055613458f * a - 0.0638541728f * b;
@ -331,24 +357,27 @@ gtk_oklab_to_rgb (float L, float a, float b,
m = powf (m, 3);
s = powf (s, 3);
float linear_red = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
float linear_green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
float linear_blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
*red = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
*green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
*blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
}
void
gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue)
{
float linear_red, linear_green, linear_blue;
gtk_oklab_to_linear_srgb (L, a, b, &linear_red, &linear_green, &linear_blue);
gtk_linear_srgb_to_rgb (linear_red, linear_green, linear_blue, red, green, blue);
}
void
gtk_rgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
gtk_linear_srgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
{
float linear_red, linear_green, linear_blue;
gtk_rgb_to_linear_srgb (red, green, blue, &linear_red, &linear_green, &linear_blue);
float l = 0.4122214708f * linear_red + 0.5363325363f * linear_green + 0.0514459929f * linear_blue;
float m = 0.2119034982f * linear_red + 0.6806995451f * linear_green + 0.1073969566f * linear_blue;
float s = 0.0883024619f * linear_red + 0.2817188376f * linear_green + 0.6299787005f * linear_blue;
float l = 0.4122214708f * red + 0.5363325363f * green + 0.0514459929f * blue;
float m = 0.2119034982f * red + 0.6806995451f * green + 0.1073969566f * blue;
float s = 0.0883024619f * red + 0.2817188376f * green + 0.6299787005f * blue;
l = cbrtf (l);
m = cbrtf (m);
@ -359,6 +388,15 @@ gtk_rgb_to_oklab (float red, float green, float blue,
*b = 0.0259040371f*l + 0.7827717662f*m - 0.8086757660f*s;
}
void
gtk_rgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
{
float linear_red, linear_green, linear_blue;
gtk_rgb_to_linear_srgb (red, green, blue, &linear_red, &linear_green, &linear_blue);
gtk_linear_srgb_to_oklab (linear_red, linear_green, linear_blue, L, a, b);
}
void
gtk_rgb_to_linear_srgb (float red, float green, float blue,
float *linear_red, float *linear_green, float *linear_blue)

View File

@ -21,7 +21,12 @@
G_BEGIN_DECLS
void gtk_rgb_to_hwb (float red, float green, float blue,
void gtk_rgb_to_hsl (float red, float green, float blue,
float *hue, float *saturation, float *lightness);
void gtk_hsl_to_rgb (float hue, float saturation, float lightness,
float *red, float *green, float *blue);
void gtk_rgb_to_hwb (float red, float green, float blue,
float *hue, float *white, float *black);
void gtk_hwb_to_rgb (float hue, float white, float black,
float *red, float *green, float *blue);
@ -31,6 +36,11 @@ void gtk_oklab_to_oklch (float L, float a, float b,
void gtk_oklch_to_oklab (float L, float C, float H,
float *L2, float *a, float *b);
void gtk_oklab_to_linear_srgb (float L, float a, float b,
float *red, float *green, float *blue);
void gtk_linear_srgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b);
void gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue);
void gtk_rgb_to_oklab (float red, float green, float blue,
@ -41,5 +51,6 @@ void gtk_rgb_to_linear_srgb (float red, float green, float blue
void gtk_linear_srgb_to_rgb (float linear_red, float linear_green, float linear_blue,
float *red, float *green, float *blue);
G_END_DECLS