Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/preference_colors.scoop

Selected production source · src/theme/preference_colors.scoop

1package scoop.ui.style
3import scoop.graphics.Color
4import scoop.math.pow
6/** Composite straight sRGB colors onto the known opaque theme surface. */
7internal 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 )
16private fun linearThemeChannel(value: Double): Double =
17 if (value <= 0.04045)
18 value / 12.92
19 else
20 pow((value + 0.055) / 1.055, 2.4)
22private 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)
30internal 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)
36internal fun contrastThemeColor(color: Color, background: Color, minimum: Double): Color {
37 val source = opaqueThemeColor(color, background)
38 if (themeContrast(source, background) >= minimum)
39 return source
40 val target =
41 if (themeContrast(Color.Black, background) >= themeContrast(Color.White, background))
42 Color.Black
43 else
44 Color.White
45 var low = 0.0
46 var high = 1.0
47 var result = target
48 // Binary search the least sRGB change that reaches the requested contrast.
49 for (step in 0 ..< 24) {
50 val middle = (low + high) / 2.0
51 val candidate = mixThemeColor(source, target, middle)
52 if (themeContrast(candidate, background) >= minimum) {
53 high = middle
54 result = candidate
55 } else
56 low = middle
57 }
58 return result
61internal fun preferencePair(
62 pair: ColorPair,
63 canvas: Color,
64 contrast: Boolean,
65 minimum: Double = 7.0
66): 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.Black
76 else
77 Color.White
78 background = contrastThemeColor(background, foreground, minimum)
79 }
80 return ColorPair(background, contrastThemeColor(pair.foreground, background, minimum))
83internal fun preferencePalette(
84 value: ThemePalette,
85 canvas: Color,
86 contrast: Boolean
87): ThemePalette {
88 val background =
89 if (contrast)
90 ColorPair(canvas, contrastThemeColor(value.background.foreground, canvas, 7.0))
91 else
92 preferencePair(value.background, canvas, false)
93 val base = background.background
94 return value.copy {
95 this.background = background
96 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 else
126 value.sidebar.border,
127 if (contrast)
128 contrastThemeColor(value.sidebar.ring, sidebar.background, 3.0)
129 else
130 value.sidebar.ring
131 )
132 this.shadow = Color.Transparent
133 }
136private fun preferenceAccent(pair: ColorPair, background: Color, contrast: Boolean): ColorPair {
137 val fill =
138 if (contrast)
139 contrastThemeColor(pair.background, background, 7.0)
140 else
141 pair.background
142 return preferencePair(ColorPair(fill, pair.foreground), background, contrast)