scoop:scoop.ui.style:0.1.0
src/theme/preference_colors.scoop
Selected production source · src/theme/preference_colors.scoop
1
package scoop.ui.style3
import scoop.graphics.Color4
import scoop.math.pow6
/** Composite straight sRGB colors onto the known opaque theme surface. */7
internal fun opaqueThemeColor(color: Color, background: Color): Color {8
val alpha = themeUnit(color.alpha)9
return Color(10
themeUnit(color.red) * alpha + background.red * (1.0 - alpha),11
themeUnit(color.green) * alpha + background.green * (1.0 - alpha),12
themeUnit(color.blue) * alpha + background.blue * (1.0 - alpha)13
)14
}16
private fun linearThemeChannel(value: Double): Double =17
if (value <= 0.04045)18
value / 12.9219
else20
pow((value + 0.055) / 1.055, 2.4)22
private fun themeLuminance(color: Color): Double =23
0.2126 *24
linearThemeChannel(color.red) +25
0.7152 *26
linearThemeChannel(color.green) +27
0.0722 *28
linearThemeChannel(color.blue)30
internal fun themeContrast(first: Color, second: Color): Double {31
val a = themeLuminance(first)32
val b = themeLuminance(second)33
return (themeMax(a, b) + 0.05) / (themeMin(a, b) + 0.05)34
}36
internal fun contrastThemeColor(color: Color, background: Color, minimum: Double): Color {37
val source = opaqueThemeColor(color, background)38
if (themeContrast(source, background) >= minimum)39
return source40
val target =41
if (themeContrast(Color.Black, background) >= themeContrast(Color.White, background))42
Color.Black43
else44
Color.White45
var low = 0.046
var high = 1.047
var result = target48
// Binary search the least sRGB change that reaches the requested contrast.49
for (step in 0 ..< 24) {50
val middle = (low + high) / 2.051
val candidate = mixThemeColor(source, target, middle)52
if (themeContrast(candidate, background) >= minimum) {53
high = middle54
result = candidate55
} else56
low = middle57
}58
return result59
}61
internal fun preferencePair(62
pair: ColorPair,63
canvas: Color,64
contrast: Boolean,65
minimum: Double = 7.066
): ColorPair {67
var background = opaqueThemeColor(pair.background, canvas)68
if (!contrast)69
return ColorPair(background, pair.foreground)70
val black = themeContrast(Color.Black, background)71
val white = themeContrast(Color.White, background)72
if (themeMax(black, white) < minimum) {73
val foreground =74
if (black >= white)75
Color.Black76
else77
Color.White78
background = contrastThemeColor(background, foreground, minimum)79
}80
return ColorPair(background, contrastThemeColor(pair.foreground, background, minimum))81
}83
internal fun preferencePalette(84
value: ThemePalette,85
canvas: Color,86
contrast: Boolean87
): ThemePalette {88
val background =89
if (contrast)90
ColorPair(canvas, contrastThemeColor(value.background.foreground, canvas, 7.0))91
else92
preferencePair(value.background, canvas, false)93
val base = background.background94
return value.copy {95
this.background = background96
this.card = preferencePair(value.card, base, contrast)97
this.popover = preferencePair(value.popover, base, contrast)98
this.primary = preferenceAccent(value.primary, base, contrast)99
this.secondary = preferencePair(value.secondary, base, contrast)100
this.muted = preferencePair(value.muted, base, contrast)101
this.accent = preferencePair(value.accent, base, contrast)102
this.destructive = preferenceAccent(value.destructive, base, contrast)103
if (contrast) {104
this.border = contrastThemeColor(value.border, base, 3.0)105
this.input = contrastThemeColor(value.input, base, 3.0)106
this.ring = contrastThemeColor(value.ring, base, 3.0)107
this.disabledText = contrastThemeColor(value.disabledText, base, 4.5)108
this.chart =109
ChartPalette(110
contrastThemeColor(value.chart.one, base, 3.0),111
contrastThemeColor(value.chart.two, base, 3.0),112
contrastThemeColor(value.chart.three, base, 3.0),113
contrastThemeColor(value.chart.four, base, 3.0),114
contrastThemeColor(value.chart.five, base, 3.0)115
)116
}117
val sidebar = preferencePair(value.sidebar.background, base, contrast)118
this.sidebar =119
SidebarPalette(120
sidebar,121
preferenceAccent(value.sidebar.primary, sidebar.background, contrast),122
preferencePair(value.sidebar.accent, sidebar.background, contrast),123
if (contrast)124
contrastThemeColor(value.sidebar.border, sidebar.background, 3.0)125
else126
value.sidebar.border,127
if (contrast)128
contrastThemeColor(value.sidebar.ring, sidebar.background, 3.0)129
else130
value.sidebar.ring131
)132
this.shadow = Color.Transparent133
}134
}136
private fun preferenceAccent(pair: ColorPair, background: Color, contrast: Boolean): ColorPair {137
val fill =138
if (contrast)139
contrastThemeColor(pair.background, background, 7.0)140
else141
pair.background142
return preferencePair(ColorPair(fill, pair.foreground), background, contrast)143
}