Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/colors.scoop

Selected production source · src/actions/colors.scoop

1package scoop.ui.component
3import scoop.graphics.Color
4import scoop.math.*
6private struct ActionLch(val lightness: Double, val chroma: Double, val hue: Double)
8// Oklab matrices and sRGB transfer functions: https://bottosson.github.io/posts/oklab/
9// Shared by the Secondary color mix and the conversation bubble relative tint.
10internal fun secondaryHover(background: Color, foreground: Color): Color {
11 val from = actionLch(background)
12 val to = actionLch(foreground)
13 val alpha = background.alpha * 0.95 + foreground.alpha * 0.05
14 if (alpha == 0.0)
15 return Color.Transparent
16 val t = foreground.alpha * 0.05 / alpha
17 val lightness = from.lightness + (to.lightness - from.lightness) * t
18 val chroma = from.chroma + (to.chroma - from.chroma) * t
19 val first =
20 if (from.chroma < 0.000004)
21 to.hue
22 else
23 from.hue
24 val last =
25 if (to.chroma < 0.000004)
26 first
27 else
28 to.hue
29 var difference = last - first
30 if (difference > PI)
31 difference -= PI * 2.0
32 if (difference < -PI)
33 difference += PI * 2.0
34 val hue = first + difference * 0.05
35 return actionLchColor(lightness, chroma, hue, alpha)
38internal fun bubbleTint(color: Color, lightness: Double, chromaScale: Double): Color {
39 val base = actionLch(color)
40 return actionLchColor(lightness, base.chroma * chromaScale, base.hue, color.alpha)
43private fun actionLchColor(lightness: Double, chroma: Double, hue: Double, alpha: Double): Color {
44 val a = chroma * cos(hue)
45 val b = chroma * sin(hue)
46 val l = cube(lightness + 0.3963377774 * a + 0.2158037573 * b)
47 val m = cube(lightness - 0.1055613458 * a - 0.0638541728 * b)
48 val s = cube(lightness - 0.0894841775 * a - 1.2914855480 * b)
49 return Color(
50 actionSrgb(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
51 actionSrgb(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
52 actionSrgb(-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s),
53 alpha
54 )
57private fun cube(value: Double): Double = value * value * value
59private fun actionLinear(value: Double): Double =
60 if (value <= 0.04045)
61 value / 12.92
62 else
63 pow((value + 0.055) / 1.055, 2.4)
65private fun actionSrgb(value: Double): Double =
66 min(
67 1.0,
68 max(
69 0.0,
70 if (value <= 0.0031308)
71 value * 12.92
72 else
73 1.055 * pow(value, 1.0 / 2.4) - 0.055
74 )
75 )
77private fun actionLch(color: Color): ActionLch {
78 val r = actionLinear(color.red)
79 val g = actionLinear(color.green)
80 val b = actionLinear(color.blue)
81 val l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
82 val m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
83 val s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
84 val a = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s
85 val axis = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
86 return ActionLch(
87 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
88 sqrt(a * a + axis * axis),
89 atan2(axis, a)
90 )