| | 1 | | using Allyaria.Theming.Constants; |
| | 2 | | using Allyaria.Theming.Primitives; |
| | 3 | | using Allyaria.Theming.Values; |
| | 4 | |
|
| | 5 | | namespace Allyaria.Theming.Helpers; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Color utilities: WCAG contrast computation, hue-preserving contrast repair for opaque colors, and small reusable |
| | 9 | | /// helpers used by palette derivations (e.g., HSVA clamped creation, scalar blending). No allocations (static helper), |
| | 10 | | /// alpha/canvas blending. |
| | 11 | | /// </summary> |
| | 12 | | internal static class ColorHelper |
| | 13 | | { |
| | 14 | | /// <summary>Linearly blends a scalar value toward a target by a factor in [0..1].</summary> |
| | 15 | | /// <param name="start">Starting value.</param> |
| | 16 | | /// <param name="target">Target value.</param> |
| | 17 | | /// <param name="t"> |
| | 18 | | /// Blend factor in [0..1]. <c>0</c> returns <paramref name="start" />, <c>1</c> returns <paramref name="target" />. |
| | 19 | | /// </param> |
| | 20 | | /// <returns>The blended scalar.</returns> |
| | 21 | | public static double Blend(double start, double target, double t) |
| | 22 | | { |
| 30 | 23 | | t = Math.Clamp(t, 0.0, 1.0); |
| | 24 | |
|
| 30 | 25 | | return start + (target - start) * t; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Chooses the initial direction to adjust V (HSV Value) to locally increase contrast (+1 brighten, -1 darken). |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="foreground">ForegroundColor (opaque).</param> |
| | 32 | | /// <param name="background">BackgroundColor (opaque).</param> |
| | 33 | | /// <returns>+1 if brightening increases contrast more; otherwise -1.</returns> |
| | 34 | | private static int ChooseValueDirection(AllyariaColorValue foreground, AllyariaColorValue background) |
| | 35 | | { |
| | 36 | | const double step = 2.0; // percent V |
| | 37 | |
|
| 120 | 38 | | double h = foreground.H, s = foreground.S, v = foreground.V; |
| | 39 | |
|
| 40 | 40 | | var up = AllyariaColorValue.FromHsva(h, s, Math.Clamp(v + step, 0.0, 100.0)); |
| 40 | 41 | | var dn = AllyariaColorValue.FromHsva(h, s, Math.Clamp(v - step, 0.0, 100.0)); |
| | 42 | |
|
| 40 | 43 | | var rUp = ContrastRatio(up, background); |
| 40 | 44 | | var rDn = ContrastRatio(dn, background); |
| | 45 | |
|
| 40 | 46 | | if (Math.Abs(rUp - rDn) < 1e-6) |
| | 47 | | { |
| | 48 | | // Tie-break: push away from mid to reach an extreme sooner |
| 4 | 49 | | return v >= 50.0 |
| 4 | 50 | | ? -1 |
| 4 | 51 | | : +1; |
| | 52 | | } |
| | 53 | |
|
| 36 | 54 | | return rUp > rDn |
| 36 | 55 | | ? +1 |
| 36 | 56 | | : -1; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary>Computes WCAG contrast ratio between two opaque sRGB colors.</summary> |
| | 60 | | /// <param name="foreground">ForegroundColor color (opaque).</param> |
| | 61 | | /// <param name="background">BackgroundColor color (opaque).</param> |
| | 62 | | /// <returns>(L1 + 0.05) / (L2 + 0.05) with WCAG relative luminance.</returns> |
| | 63 | | public static double ContrastRatio(AllyariaColorValue foreground, AllyariaColorValue background) |
| | 64 | | { |
| 1680 | 65 | | var lf = RelativeLuminance(foreground); |
| 1680 | 66 | | var lb = RelativeLuminance(background); |
| | 67 | |
|
| 1680 | 68 | | var lighter = Math.Max(lf, lb); |
| 1680 | 69 | | var darker = Math.Min(lf, lb); |
| | 70 | |
|
| 1680 | 71 | | return (lighter + 0.05) / (darker + 0.05); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Resolves a foreground color that meets a minimum contrast over the background by preserving the foreground hue a |
| | 76 | | /// saturation (HSV H/S) and adjusting only value (V). If that hue rail cannot reach the target (even at V=0% or V=1 |
| | 77 | | /// mixes toward black and white and returns the closest solution that meets (or best-approaches) the target. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="foreground">Starting foreground (opaque).</param> |
| | 80 | | /// <param name="background">BackgroundColor (opaque).</param> |
| | 81 | | /// <param name="minimumRatio">Required minimum ratio (e.g., 4.5 for body text).</param> |
| | 82 | | /// <returns><see cref="ContrastResult" /> with final color and achieved ratio.</returns> |
| | 83 | | public static ContrastResult EnsureMinimumContrast(AllyariaColorValue foreground, |
| | 84 | | AllyariaColorValue background, |
| | 85 | | double minimumRatio = 3.0) |
| | 86 | | { |
| | 87 | | // Early accept |
| 240 | 88 | | var startRatio = ContrastRatio(foreground, background); |
| | 89 | |
|
| 240 | 90 | | if (startRatio >= minimumRatio) |
| | 91 | | { |
| 200 | 92 | | return new ContrastResult(foreground, background, startRatio, true); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | // Reuse AllyariaColorValue’s HSV API to avoid duplicating conversions. |
| 40 | 96 | | var h = foreground.H; // degrees |
| 40 | 97 | | var s = foreground.S; // percent |
| 40 | 98 | | var v = foreground.V; // percent |
| | 99 | |
|
| | 100 | | // Choose the V direction that locally increases contrast. |
| 40 | 101 | | var initialDir = ChooseValueDirection(foreground, background); |
| | 102 | |
|
| | 103 | | // 1) Try along the hue rail in the better direction first. |
| 40 | 104 | | var first = SearchValueRail(h, s, v, initialDir, background, minimumRatio); |
| | 105 | |
|
| 40 | 106 | | if (first.MeetsMinimum) |
| | 107 | | { |
| 22 | 108 | | return first; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // 2) Try the opposite direction on the hue rail. |
| 18 | 112 | | var second = SearchValueRail(h, s, v, -initialDir, background, minimumRatio); |
| | 113 | |
|
| 18 | 114 | | if (second.MeetsMinimum) |
| | 115 | | { |
| 6 | 116 | | return second; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | // 3) Guarantee path: mix toward white; prefer any that meets; otherwise best-approaching. |
| 12 | 120 | | var towardPole = SearchTowardPole(foreground, Colors.White, background, minimumRatio); |
| | 121 | |
|
| 12 | 122 | | if (towardPole.MeetsMinimum) |
| | 123 | | { |
| 2 | 124 | | return towardPole; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | // 4) Still not met: return the best-approaching overall. |
| 10 | 128 | | var best = first; |
| | 129 | |
|
| 10 | 130 | | if (second.ContrastRatio > best.ContrastRatio) |
| | 131 | | { |
| 2 | 132 | | best = second; |
| | 133 | | } |
| | 134 | |
|
| 10 | 135 | | if (towardPole.ContrastRatio > best.ContrastRatio) |
| | 136 | | { |
| 4 | 137 | | best = towardPole; |
| | 138 | | } |
| | 139 | |
|
| 10 | 140 | | return best; |
| | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary>Linear interpolation in sRGB between two opaque colors.</summary> |
| | 144 | | /// <param name="start">Start color.</param> |
| | 145 | | /// <param name="end">End color.</param> |
| | 146 | | /// <param name="t">Mix factor in [0,1].</param> |
| | 147 | | /// <returns>Interpolated color.</returns> |
| | 148 | | private static AllyariaColorValue LerpSrgb(AllyariaColorValue start, AllyariaColorValue end, double t) |
| | 149 | | { |
| 224 | 150 | | t = Math.Clamp(t, 0.0, 1.0); |
| | 151 | |
|
| | 152 | | static byte Lerp(byte a, byte b, double tt) |
| 672 | 153 | | => (byte)Math.Clamp((int)Math.Round(a + (b - a) * tt, MidpointRounding.AwayFromZero), 0, 255); |
| | 154 | |
|
| 224 | 155 | | return AllyariaColorValue.FromRgba( |
| 224 | 156 | | Lerp(start.R, end.R, t), |
| 224 | 157 | | Lerp(start.G, end.G, t), |
| 224 | 158 | | Lerp(start.B, end.B, t) |
| 224 | 159 | | ); |
| | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// sRGB-space linear interpolation between two opaque colors. Note this is *not* perceptually uniform. |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="a">Start color.</param> |
| | 166 | | /// <param name="b">End color.</param> |
| | 167 | | /// <param name="t">Blend factor in [0..1].</param> |
| | 168 | | /// <returns>Blended color in sRGB.</returns> |
| 6 | 169 | | public static AllyariaColorValue MixSrgb(AllyariaColorValue a, AllyariaColorValue b, double t) => LerpSrgb(a, b, t); |
| | 170 | |
|
| | 171 | | /// <summary>WCAG relative luminance from sRGB bytes.</summary> |
| | 172 | | /// <param name="color">Opaque sRGB color.</param> |
| | 173 | | /// <returns>Relative luminance [0..1].</returns> |
| | 174 | | public static double RelativeLuminance(AllyariaColorValue color) |
| | 175 | | { |
| 3384 | 176 | | var rl = SrgbToLinear(color.R); |
| 3384 | 177 | | var gl = SrgbToLinear(color.G); |
| 3384 | 178 | | var bl = SrgbToLinear(color.B); |
| | 179 | |
|
| 3384 | 180 | | return 0.2126 * rl + 0.7152 * gl + 0.0722 * bl; |
| | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// Binary-search mixing the starting foreground toward a pole (black or white) in sRGB, returning the closest solut |
| | 185 | | /// that meets (or best-approaches) the target. |
| | 186 | | /// </summary> |
| | 187 | | /// <param name="start">Starting foreground (opaque).</param> |
| | 188 | | /// <param name="pole">Target pole (<see cref="Colors.Black" /> or <see cref="Colors.White" />).</param> |
| | 189 | | /// <param name="background">BackgroundColor (opaque).</param> |
| | 190 | | /// <param name="minimumRatio">Target ratio.</param> |
| | 191 | | /// <returns>Resolution result for this pole.</returns> |
| | 192 | | private static ContrastResult SearchTowardPole(AllyariaColorValue start, |
| | 193 | | AllyariaColorValue pole, |
| | 194 | | AllyariaColorValue background, |
| | 195 | | double minimumRatio) |
| | 196 | | { |
| 24 | 197 | | double lo = 0.0, hi = 1.0; |
| | 198 | | const int iters = 18; |
| 12 | 199 | | var bestRatio = -1.0; |
| 12 | 200 | | var bestColor = start; |
| 12 | 201 | | var met = false; |
| | 202 | |
|
| 456 | 203 | | for (var i = 0; i < iters; i++) |
| | 204 | | { |
| 216 | 205 | | var mid = 0.5 * (lo + hi); |
| 216 | 206 | | var candidate = LerpSrgb(start, pole, mid); |
| 216 | 207 | | var ratio = ContrastRatio(candidate, background); |
| | 208 | |
|
| 216 | 209 | | if (ratio > bestRatio) |
| | 210 | | { |
| 94 | 211 | | bestRatio = ratio; |
| 94 | 212 | | bestColor = candidate; |
| | 213 | | } |
| | 214 | |
|
| 216 | 215 | | if (ratio >= minimumRatio) |
| | 216 | | { |
| 22 | 217 | | met = true; |
| 22 | 218 | | hi = mid; // seek closest-to-start satisfying mix |
| | 219 | | } |
| | 220 | | else |
| | 221 | | { |
| 194 | 222 | | lo = mid; |
| | 223 | | } |
| | 224 | | } |
| | 225 | |
|
| 12 | 226 | | var finalColor = met |
| 12 | 227 | | ? LerpSrgb(start, pole, hi) |
| 12 | 228 | | : bestColor; |
| | 229 | |
|
| 12 | 230 | | var finalRatio = ContrastRatio(finalColor, background); |
| | 231 | |
|
| 12 | 232 | | return new ContrastResult(finalColor, background, finalRatio, met); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// Binary search along the HSV Value rail (keeping H and S) to find the minimum-change V that meets the contrast |
| | 237 | | /// requirement; returns best-approaching if unreachable. |
| | 238 | | /// </summary> |
| | 239 | | /// <param name="h">Hue (degrees).</param> |
| | 240 | | /// <param name="s">Saturation (percent).</param> |
| | 241 | | /// <param name="vStart">Starting Value (percent).</param> |
| | 242 | | /// <param name="direction">+1 brighten, -1 darken.</param> |
| | 243 | | /// <param name="background">BackgroundColor (opaque).</param> |
| | 244 | | /// <param name="minimumRatio">Target ratio.</param> |
| | 245 | | /// <returns>Resolution result for this search branch.</returns> |
| | 246 | | private static ContrastResult SearchValueRail(double h, |
| | 247 | | double s, |
| | 248 | | double vStart, |
| | 249 | | int direction, |
| | 250 | | AllyariaColorValue background, |
| | 251 | | double minimumRatio) |
| | 252 | | { |
| | 253 | | double lo, hi; |
| | 254 | |
|
| 58 | 255 | | if (direction > 0) |
| | 256 | | { |
| 22 | 257 | | lo = vStart; |
| 22 | 258 | | hi = 100.0; |
| | 259 | | } |
| | 260 | | else |
| | 261 | | { |
| 36 | 262 | | lo = vStart; |
| 36 | 263 | | hi = 0.0; |
| | 264 | | } |
| | 265 | |
|
| | 266 | | const int iters = 18; |
| 58 | 267 | | double? found = null; |
| 58 | 268 | | var bestRatio = -1.0; |
| 58 | 269 | | var bestColor = AllyariaColorValue.FromHsva(h, s, vStart); |
| | 270 | |
|
| 2204 | 271 | | for (var i = 0; i < iters; i++) |
| | 272 | | { |
| 1044 | 273 | | var mid = 0.5 * (lo + hi); |
| 1044 | 274 | | var candidate = AllyariaColorValue.FromHsva(h, s, mid); |
| 1044 | 275 | | var ratio = ContrastRatio(candidate, background); |
| | 276 | |
|
| 1044 | 277 | | if (ratio > bestRatio) |
| | 278 | | { |
| 230 | 279 | | bestRatio = ratio; |
| 230 | 280 | | bestColor = candidate; |
| | 281 | | } |
| | 282 | |
|
| 1044 | 283 | | if (ratio >= minimumRatio) |
| | 284 | | { |
| 308 | 285 | | found = mid; |
| 308 | 286 | | hi = mid; // tighten toward smallest change |
| | 287 | | } |
| | 288 | | else |
| | 289 | | { |
| 736 | 290 | | lo = mid; |
| | 291 | | } |
| | 292 | | } |
| | 293 | |
|
| 58 | 294 | | if (found.HasValue) |
| | 295 | | { |
| 28 | 296 | | var final = AllyariaColorValue.FromHsva(h, s, hi); |
| 28 | 297 | | var r = ContrastRatio(final, background); |
| | 298 | |
|
| 28 | 299 | | return new ContrastResult(final, background, r, true); |
| | 300 | | } |
| | 301 | |
|
| 30 | 302 | | return new ContrastResult(bestColor, background, bestRatio, false); |
| | 303 | | } |
| | 304 | |
|
| | 305 | | /// <summary>Converts sRGB 8-bit channel to linear-light [0..1] for luminance computation.</summary> |
| | 306 | | /// <param name="c8">Channel byte.</param> |
| | 307 | | /// <returns>Linear-light value.</returns> |
| | 308 | | private static double SrgbToLinear(byte c8) |
| | 309 | | { |
| 10152 | 310 | | var c = c8 / 255.0; |
| | 311 | |
|
| 10152 | 312 | | return c <= 0.03928 |
| 10152 | 313 | | ? c / 12.92 |
| 10152 | 314 | | : Math.Pow((c + 0.055) / 1.055, 2.4); |
| | 315 | | } |
| | 316 | | } |