scoop:scoop.ui.component:0.1.0
src/actions/colors.scoop
Selected production source · src/actions/colors.scoop
1
package scoop.ui.component3
import scoop.graphics.Color4
import scoop.math.*6
private 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.10
internal 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.0514
if (alpha == 0.0)15
return Color.Transparent16
val t = foreground.alpha * 0.05 / alpha17
val lightness = from.lightness + (to.lightness - from.lightness) * t18
val chroma = from.chroma + (to.chroma - from.chroma) * t19
val first =20
if (from.chroma < 0.000004)21
to.hue22
else23
from.hue24
val last =25
if (to.chroma < 0.000004)26
first27
else28
to.hue29
var difference = last - first30
if (difference > PI)31
difference -= PI * 2.032
if (difference < -PI)33
difference += PI * 2.034
val hue = first + difference * 0.0535
return actionLchColor(lightness, chroma, hue, alpha)36
}38
internal 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)41
}43
private 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
alpha54
)55
}57
private fun cube(value: Double): Double = value * value * value59
private fun actionLinear(value: Double): Double =60
if (value <= 0.04045)61
value / 12.9262
else63
pow((value + 0.055) / 1.055, 2.4)65
private fun actionSrgb(value: Double): Double =66
min(67
1.0,68
max(69
0.0,70
if (value <= 0.0031308)71
value * 12.9272
else73
1.055 * pow(value, 1.0 / 2.4) - 0.05574
)75
)77
private 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 * s85
val axis = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s86
return ActionLch(87
0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,88
sqrt(a * a + axis * axis),89
atan2(axis, a)90
)91
}