4b781378b5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
6.4 KiB
Dart
173 lines
6.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'app_design_tokens.dart';
|
|
|
|
/// The WCAG 2 contrast ratio of [a] against [b] (1 to 21), both opaque.
|
|
double contrastRatio(Color a, Color b) {
|
|
final la = a.computeLuminance();
|
|
final lb = b.computeLuminance();
|
|
final hi = la > lb ? la : lb;
|
|
final lo = la > lb ? lb : la;
|
|
return (hi + 0.05) / (lo + 0.05);
|
|
}
|
|
|
|
/// WCAG AAA for body text: what High contrast's reading text and its
|
|
/// status accents reach against the page.
|
|
const double highContrastText = 7;
|
|
|
|
/// WCAG AA for everything else: secondary text, disabled text, borders and
|
|
/// chat colours reach at least this against the page.
|
|
const double highContrastMinimum = 4.5;
|
|
|
|
/// The one strong accent on the black page: a bright yellow (14.9:1).
|
|
const Color highContrastYellow = Color(0xFFFFD60A);
|
|
|
|
/// The accent on the white page: a deep blue (9.4:1); yellow cannot meet
|
|
/// contrast on white.
|
|
const Color highContrastBlue = Color(0xFF0040A0);
|
|
|
|
/// The dark page's error red: bright, and still 7.6:1 against black.
|
|
const Color highContrastDarkRed = Color(0xFFFF6B6B);
|
|
|
|
/// The light page's error red (7.4:1 against white).
|
|
const Color highContrastLightRed = Color(0xFFB00000);
|
|
|
|
/// Disabled text and borders: plainly greyer than the ink, and still 4.5:1
|
|
/// or more against the page (6.5:1 on black, 5.1:1 on white).
|
|
const Color highContrastDarkDisabled = Color(0xFF8F8F8F);
|
|
const Color highContrastLightDisabled = Color(0xFF6E6E6E);
|
|
|
|
/// High contrast's status accents on black: each reads at 7:1 or more, so
|
|
/// a status word in its colour is body-text legible.
|
|
const AnsiAccents highContrastDarkAccents = AnsiAccents(
|
|
green: Color(0xFF3DFF6E),
|
|
cyan: Color(0xFF00E5FF),
|
|
yellow: highContrastYellow,
|
|
red: highContrastDarkRed,
|
|
magenta: Color(0xFFFF80FF),
|
|
blue: Color(0xFF6CB4FF),
|
|
);
|
|
|
|
/// High contrast's status accents on white, each 7:1 or more.
|
|
const AnsiAccents highContrastLightAccents = AnsiAccents(
|
|
green: Color(0xFF006400),
|
|
cyan: Color(0xFF005F6B),
|
|
yellow: Color(0xFF6B5000),
|
|
red: highContrastLightRed,
|
|
magenta: Color(0xFF8B008B),
|
|
blue: highContrastBlue,
|
|
);
|
|
|
|
/// High contrast's colour scheme: a pure black page with white ink and a
|
|
/// yellow accent (dark), or a pure white page with black ink and a deep blue
|
|
/// accent (light). Every surface container is the page itself: panels part
|
|
/// by their borders ([ColorScheme.outline], the ink), not by tone. The
|
|
/// secondary container is the ink, so a selected thing (a navigation
|
|
/// indicator, a chosen chip, the open row) reads inverted.
|
|
ColorScheme highContrastColorScheme(Brightness brightness) {
|
|
if (brightness == Brightness.dark) {
|
|
return const ColorScheme(
|
|
brightness: Brightness.dark,
|
|
primary: highContrastYellow,
|
|
onPrimary: Color(0xFF000000),
|
|
primaryContainer: highContrastYellow,
|
|
onPrimaryContainer: Color(0xFF000000),
|
|
secondary: Color(0xFFFFFFFF),
|
|
onSecondary: Color(0xFF000000),
|
|
secondaryContainer: Color(0xFFFFFFFF),
|
|
onSecondaryContainer: Color(0xFF000000),
|
|
tertiary: Color(0xFFFF9F0A),
|
|
onTertiary: Color(0xFF000000),
|
|
tertiaryContainer: Color(0xFFFF9F0A),
|
|
onTertiaryContainer: Color(0xFF000000),
|
|
error: highContrastDarkRed,
|
|
onError: Color(0xFF000000),
|
|
errorContainer: Color(0xFF000000),
|
|
onErrorContainer: highContrastDarkRed,
|
|
surface: Color(0xFF000000),
|
|
onSurface: Color(0xFFFFFFFF),
|
|
onSurfaceVariant: Color(0xFFD0D0D0),
|
|
surfaceDim: Color(0xFF000000),
|
|
surfaceBright: Color(0xFF000000),
|
|
surfaceContainerLowest: Color(0xFF000000),
|
|
surfaceContainerLow: Color(0xFF000000),
|
|
surfaceContainer: Color(0xFF000000),
|
|
surfaceContainerHigh: Color(0xFF000000),
|
|
surfaceContainerHighest: Color(0xFF000000),
|
|
outline: Color(0xFFFFFFFF),
|
|
outlineVariant: Color(0xFF9E9E9E),
|
|
shadow: Color(0x00000000),
|
|
scrim: Color(0xFF000000),
|
|
inverseSurface: Color(0xFFFFFFFF),
|
|
onInverseSurface: Color(0xFF000000),
|
|
inversePrimary: highContrastBlue,
|
|
surfaceTint: Color(0x00000000),
|
|
);
|
|
}
|
|
return const ColorScheme(
|
|
brightness: Brightness.light,
|
|
primary: highContrastBlue,
|
|
onPrimary: Color(0xFFFFFFFF),
|
|
primaryContainer: highContrastBlue,
|
|
onPrimaryContainer: Color(0xFFFFFFFF),
|
|
secondary: Color(0xFF000000),
|
|
onSecondary: Color(0xFFFFFFFF),
|
|
secondaryContainer: Color(0xFF000000),
|
|
onSecondaryContainer: Color(0xFFFFFFFF),
|
|
tertiary: Color(0xFF8A3B00),
|
|
onTertiary: Color(0xFFFFFFFF),
|
|
tertiaryContainer: Color(0xFF8A3B00),
|
|
onTertiaryContainer: Color(0xFFFFFFFF),
|
|
error: highContrastLightRed,
|
|
onError: Color(0xFFFFFFFF),
|
|
errorContainer: Color(0xFFFFFFFF),
|
|
onErrorContainer: highContrastLightRed,
|
|
surface: Color(0xFFFFFFFF),
|
|
onSurface: Color(0xFF000000),
|
|
onSurfaceVariant: Color(0xFF333333),
|
|
surfaceDim: Color(0xFFFFFFFF),
|
|
surfaceBright: Color(0xFFFFFFFF),
|
|
surfaceContainerLowest: Color(0xFFFFFFFF),
|
|
surfaceContainerLow: Color(0xFFFFFFFF),
|
|
surfaceContainer: Color(0xFFFFFFFF),
|
|
surfaceContainerHigh: Color(0xFFFFFFFF),
|
|
surfaceContainerHighest: Color(0xFFFFFFFF),
|
|
outline: Color(0xFF000000),
|
|
outlineVariant: Color(0xFF595959),
|
|
shadow: Color(0x00000000),
|
|
scrim: Color(0xFF000000),
|
|
inverseSurface: Color(0xFF000000),
|
|
onInverseSurface: Color(0xFFFFFFFF),
|
|
inversePrimary: highContrastYellow,
|
|
surfaceTint: Color(0x00000000),
|
|
);
|
|
}
|
|
|
|
/// The disabled ink for [brightness] ([highContrastDarkDisabled] or
|
|
/// [highContrastLightDisabled]).
|
|
Color highContrastDisabled(Brightness brightness) =>
|
|
brightness == Brightness.dark
|
|
? highContrastDarkDisabled
|
|
: highContrastLightDisabled;
|
|
|
|
/// [color] with its lightness moved away from [page] in small steps until
|
|
/// it reaches [ratio] against it, keeping its hue and saturation. Cached:
|
|
/// a design has a handful of marks and two pages.
|
|
Color colorWithContrast(Color color, Color page, double ratio) =>
|
|
_withContrast[(color, page, ratio)] ??= _moveToContrast(color, page, ratio);
|
|
|
|
final Map<(Color, Color, double), Color> _withContrast = {};
|
|
|
|
Color _moveToContrast(Color color, Color page, double ratio) {
|
|
final lighter = page.computeLuminance() < 0.5;
|
|
var hsl = HSLColor.fromColor(color);
|
|
var result = color;
|
|
while (contrastRatio(result, page) < ratio) {
|
|
final next = (hsl.lightness + (lighter ? 0.02 : -0.02)).clamp(0.0, 1.0);
|
|
if (next == hsl.lightness) break;
|
|
hsl = hsl.withLightness(next);
|
|
result = hsl.toColor();
|
|
}
|
|
return result;
|
|
}
|