|
| 1 | +using System.Globalization; |
| 2 | + |
| 3 | +namespace Motus; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Implements WCAG 2.1 relative luminance and contrast ratio calculations. |
| 7 | +/// </summary> |
| 8 | +internal static class ContrastCalculator |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// WCAG AA minimum contrast ratio for normal text. |
| 12 | + /// </summary> |
| 13 | + internal const double NormalTextThreshold = 4.5; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// WCAG AA minimum contrast ratio for large text (>= 18pt or bold >= 14pt). |
| 17 | + /// </summary> |
| 18 | + internal const double LargeTextThreshold = 3.0; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Computes the WCAG contrast ratio between two colors. |
| 22 | + /// Returns a value >= 1.0 where 1.0 means no contrast and 21.0 is maximum. |
| 23 | + /// </summary> |
| 24 | + internal static double ContrastRatio(double luminance1, double luminance2) |
| 25 | + { |
| 26 | + var lighter = Math.Max(luminance1, luminance2); |
| 27 | + var darker = Math.Min(luminance1, luminance2); |
| 28 | + return (lighter + 0.05) / (darker + 0.05); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Computes relative luminance per WCAG 2.1 definition. |
| 33 | + /// Input is an sRGB color with components in [0, 255]. |
| 34 | + /// </summary> |
| 35 | + internal static double RelativeLuminance(int r, int g, int b) |
| 36 | + { |
| 37 | + var rLin = Linearize(r / 255.0); |
| 38 | + var gLin = Linearize(g / 255.0); |
| 39 | + var bLin = Linearize(b / 255.0); |
| 40 | + return 0.2126 * rLin + 0.7152 * gLin + 0.0722 * bLin; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Converts an sRGB component (0..1) to linear light. |
| 45 | + /// </summary> |
| 46 | + internal static double Linearize(double c) => |
| 47 | + c <= 0.04045 ? c / 12.92 : Math.Pow((c + 0.055) / 1.055, 2.4); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Determines whether text is "large" per WCAG criteria. |
| 51 | + /// Large text is >= 18pt (24px) or bold >= 14pt (18.66px). |
| 52 | + /// </summary> |
| 53 | + internal static bool IsLargeText(string? fontSize, string? fontWeight) |
| 54 | + { |
| 55 | + var px = ParsePx(fontSize); |
| 56 | + if (px is null) |
| 57 | + return false; |
| 58 | + |
| 59 | + var isBold = IsBold(fontWeight); |
| 60 | + |
| 61 | + // 18pt = 24px, 14pt = 18.66px |
| 62 | + return px.Value >= 24.0 || (isBold && px.Value >= 18.66); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Parses a CSS color string (rgb/rgba format from computed styles) into RGB components. |
| 67 | + /// Returns false if the format is not recognized. |
| 68 | + /// </summary> |
| 69 | + internal static bool TryParseColor(string? color, out int r, out int g, out int b) |
| 70 | + { |
| 71 | + r = g = b = 0; |
| 72 | + if (string.IsNullOrWhiteSpace(color)) |
| 73 | + return false; |
| 74 | + |
| 75 | + var span = color.AsSpan().Trim(); |
| 76 | + |
| 77 | + // Handle "rgb(r, g, b)" and "rgba(r, g, b, a)" |
| 78 | + if (span.StartsWith("rgb")) |
| 79 | + { |
| 80 | + var open = span.IndexOf('('); |
| 81 | + var close = span.IndexOf(')'); |
| 82 | + if (open < 0 || close < 0 || close <= open + 1) |
| 83 | + return false; |
| 84 | + |
| 85 | + var inner = span.Slice(open + 1, close - open - 1); |
| 86 | + return ParseRgbComponents(inner, out r, out g, out b); |
| 87 | + } |
| 88 | + |
| 89 | + // Handle "#RRGGBB" and "#RGB" |
| 90 | + if (span.Length > 0 && span[0] == '#') |
| 91 | + { |
| 92 | + return TryParseHexColor(span.Slice(1), out r, out g, out b); |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + private static bool ParseRgbComponents(ReadOnlySpan<char> inner, out int r, out int g, out int b) |
| 99 | + { |
| 100 | + r = g = b = 0; |
| 101 | + |
| 102 | + // Split by comma or space (modern CSS allows both) |
| 103 | + Span<Range> parts = stackalloc Range[5]; |
| 104 | + int count; |
| 105 | + |
| 106 | + if (inner.Contains(',')) |
| 107 | + { |
| 108 | + count = inner.Split(parts, ',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + count = inner.Split(parts, ' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); |
| 113 | + } |
| 114 | + |
| 115 | + if (count < 3) |
| 116 | + return false; |
| 117 | + |
| 118 | + return int.TryParse(inner[parts[0]], NumberStyles.Integer, CultureInfo.InvariantCulture, out r) && |
| 119 | + int.TryParse(inner[parts[1]], NumberStyles.Integer, CultureInfo.InvariantCulture, out g) && |
| 120 | + int.TryParse(inner[parts[2]], NumberStyles.Integer, CultureInfo.InvariantCulture, out b); |
| 121 | + } |
| 122 | + |
| 123 | + private static bool TryParseHexColor(ReadOnlySpan<char> hex, out int r, out int g, out int b) |
| 124 | + { |
| 125 | + r = g = b = 0; |
| 126 | + |
| 127 | + if (hex.Length == 6 || hex.Length == 8) |
| 128 | + { |
| 129 | + return int.TryParse(hex.Slice(0, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out r) && |
| 130 | + int.TryParse(hex.Slice(2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out g) && |
| 131 | + int.TryParse(hex.Slice(4, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b); |
| 132 | + } |
| 133 | + |
| 134 | + if (hex.Length == 3 || hex.Length == 4) |
| 135 | + { |
| 136 | + if (!int.TryParse(hex.Slice(0, 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out r) || |
| 137 | + !int.TryParse(hex.Slice(1, 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out g) || |
| 138 | + !int.TryParse(hex.Slice(2, 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b)) |
| 139 | + return false; |
| 140 | + |
| 141 | + r = r * 16 + r; |
| 142 | + g = g * 16 + g; |
| 143 | + b = b * 16 + b; |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + private static double? ParsePx(string? fontSize) |
| 151 | + { |
| 152 | + if (string.IsNullOrWhiteSpace(fontSize)) |
| 153 | + return null; |
| 154 | + |
| 155 | + var span = fontSize.AsSpan().Trim(); |
| 156 | + if (span.EndsWith("px", StringComparison.OrdinalIgnoreCase)) |
| 157 | + span = span.Slice(0, span.Length - 2); |
| 158 | + |
| 159 | + return double.TryParse(span, NumberStyles.Float, CultureInfo.InvariantCulture, out var val) |
| 160 | + ? val |
| 161 | + : null; |
| 162 | + } |
| 163 | + |
| 164 | + private static bool IsBold(string? fontWeight) |
| 165 | + { |
| 166 | + if (string.IsNullOrWhiteSpace(fontWeight)) |
| 167 | + return false; |
| 168 | + |
| 169 | + if (string.Equals(fontWeight, "bold", StringComparison.OrdinalIgnoreCase) || |
| 170 | + string.Equals(fontWeight, "bolder", StringComparison.OrdinalIgnoreCase)) |
| 171 | + return true; |
| 172 | + |
| 173 | + return int.TryParse(fontWeight, NumberStyles.Integer, CultureInfo.InvariantCulture, out var weight) && |
| 174 | + weight >= 700; |
| 175 | + } |
| 176 | +} |
0 commit comments