2024-03-05 23:18:51 +00:00
|
|
|
package color
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2024-03-06 18:23:49 +00:00
|
|
|
// LCH represents a color using lightness, chroma and hue.
|
2024-03-05 23:18:51 +00:00
|
|
|
func LCH(lightness Value, chroma Value, hue Value) Color {
|
2024-03-06 18:23:49 +00:00
|
|
|
lightness = min(max(lightness, 0), 1)
|
|
|
|
hue *= math.Pi / 180
|
|
|
|
a := math.Cos(hue)
|
|
|
|
b := math.Sin(hue)
|
2024-03-05 23:18:51 +00:00
|
|
|
|
2024-03-06 18:23:49 +00:00
|
|
|
if !inSRGB(lightness, a, b, chroma) {
|
|
|
|
chroma = findChromaInSRGB(chroma, lightness, a, b, 0.01)
|
|
|
|
}
|
|
|
|
|
|
|
|
a *= chroma
|
|
|
|
b *= chroma
|
|
|
|
|
|
|
|
r, g, b := oklabToLinearRGB(lightness, a, b)
|
2024-03-05 23:18:51 +00:00
|
|
|
|
2024-03-06 18:23:49 +00:00
|
|
|
r = sRGB(r)
|
|
|
|
g = sRGB(g)
|
|
|
|
b = sRGB(b)
|
2024-03-05 23:18:51 +00:00
|
|
|
|
|
|
|
return Color{r, g, b}
|
|
|
|
}
|
|
|
|
|
2024-03-06 18:23:49 +00:00
|
|
|
// findChromaInSRGB tries to find the closest chroma that can be represented in sRGB color space.
|
|
|
|
func findChromaInSRGB(chroma Value, lightness Value, a Value, b Value, precision Value) Value {
|
|
|
|
high := chroma
|
|
|
|
low := 0.0
|
|
|
|
chroma *= 0.5
|
|
|
|
|
|
|
|
for high-low > precision {
|
|
|
|
if inSRGB(lightness, a, b, chroma) {
|
|
|
|
low = chroma
|
|
|
|
} else {
|
|
|
|
high = chroma
|
|
|
|
}
|
|
|
|
|
|
|
|
chroma = (high + low) * 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
return chroma
|
|
|
|
}
|
|
|
|
|
|
|
|
// OKLAB to linear RGB.
|
2024-03-05 23:18:51 +00:00
|
|
|
// Source: https://bottosson.github.io/posts/oklab/
|
2024-03-06 18:23:49 +00:00
|
|
|
func oklabToLinearRGB(lightness Value, a Value, b Value) (Value, Value, Value) {
|
2024-03-05 23:18:51 +00:00
|
|
|
l := lightness + 0.3963377773761749*a + 0.21580375730991364*b
|
|
|
|
m := lightness - 0.10556134581565857*a - 0.0638541728258133*b
|
|
|
|
s := lightness - 0.08948418498039246*a - 1.2914855480194092*b
|
|
|
|
|
|
|
|
l = l * l * l
|
|
|
|
m = m * m * m
|
|
|
|
s = s * s * s
|
|
|
|
|
|
|
|
red := 4.0767416621*l - 3.3077115913*m + 0.2309699292*s
|
|
|
|
green := -1.2684380046*l + 2.6097574011*m - 0.3413193965*s
|
|
|
|
blue := -0.0041960863*l - 0.7034186147*m + 1.7076147010*s
|
|
|
|
|
|
|
|
return red, green, blue
|
|
|
|
}
|