import 'package:flutter/painting.dart'; import '../../terminal/terminal_layout.dart'; /// The contrast the sixteen ANSI colours keep, as WCAG contrast ratios. abstract final class TerminalContrast { /// Red, green, yellow, blue, magenta and cyan, and white on a dark /// background (black on a light one), against the background: body text. static const double text = 4.5; /// The bright colours on a light background, and bright black (dim text, /// suggestions) on either: secondary text. static const double secondary = 3; /// White text on the normal colours as backgrounds (`ls` setuid is white /// on red) and black text on them (setgid, sticky directories). static const double onColour = 3; /// How far each bright colour stands from its normal one, lighter. static const double brightStep = 1.2; } /// The normal colours programs customarily put white text on (red, blue, /// magenta: `ls` setuid, dialog boxes); black text goes on the others /// (green, yellow, cyan: setgid, sticky directories, status bars). const Set terminalWhiteTextColours = {1, 4, 5}; /// The sixteen ANSI colours (black, red, green, yellow, blue, magenta, cyan, /// white, then the bright ones) of [palette] for a terminal drawn on /// [background], adjusted to meet [TerminalContrast]: each colour keeps its /// hue and saturation and moves only in lightness, and only as far as needed. /// /// White stays light and black dark in both themes, so the coloured /// backgrounds keep their customary text readable (see /// [terminalWhiteTextColours]); as in other terminals, white text on a light /// background and black on a dark one are faint. List chatShellTerminalPalette( TerminalPalette palette, Color background, ) => _computed.putIfAbsent(( palette, background.toARGB32(), ), () => List.unmodifiable(_adjust(palette, background))); /// Adjusted palettes by palette and background (a handful per app run). final Map<(TerminalPalette, int), List> _computed = {}; List _adjust(TerminalPalette palette, Color background) { final base = [for (final value in _bases[palette]!) Color(value)]; final bg = background.computeLuminance(); final dark = bg < 0.5; final out = List.of(base); // Black and white first: the colours under text are bounded by them. out[0] = _within(base[0], 0, dark ? 0.03 : 0.015); final black = out[0].computeLuminance(); out[8] = _within( base[8], _above(dark ? bg : black, TerminalContrast.secondary), _below(dark ? 1 : bg, TerminalContrast.secondary), ); // On a light background white rises to the normal colours chosen below. out[7] = _within(base[7], dark ? 0.78 : 0, 1); final readable = dark ? (low: _above(bg, TerminalContrast.text), high: 1.0) : (low: 0.0, high: _below(bg, TerminalContrast.text)); final brightHigh = dark ? 1.0 : _below(bg, TerminalContrast.secondary); var whiteNeeded = 0.0; for (var i = 1; i <= 6; i++) { final white = terminalWhiteTextColours.contains(i); final textHigh = white && dark ? _below(out[7].computeLuminance(), TerminalContrast.onColour) : 1.0; final textLow = white ? 0.0 : _above(black, TerminalContrast.onColour); out[i] = _within( base[i], _max(readable.low, textLow), _min(readable.high, textHigh), ); final normal = out[i].computeLuminance(); if (white) { whiteNeeded = _max( whiteNeeded, _above(normal, TerminalContrast.onColour), ); } out[i + 8] = _within( base[i + 8], _max( _above(normal, TerminalContrast.brightStep), dark ? readable.low : 0, ), brightHigh, ); } out[7] = _within(out[7], whiteNeeded, 1); out[15] = _within(base[15], out[7].computeLuminance(), 1); return out; } double _max(double a, double b) => a > b ? a : b; double _min(double a, double b) => a < b ? a : b; /// The WCAG contrast ratio of two colours. double terminalContrast(Color a, Color b) { final la = a.computeLuminance(); final lb = b.computeLuminance(); return la > lb ? (la + 0.05) / (lb + 0.05) : (lb + 0.05) / (la + 0.05); } /// The luminance [ratio] above / below [luminance]. double _above(double luminance, double ratio) => ((luminance + 0.05) * ratio - 0.05).clamp(0, 1); double _below(double luminance, double ratio) => ((luminance + 0.05) / ratio - 0.05).clamp(0, 1); /// [color] moved in lightness until its luminance is within [low]..[high] /// (at [low] when the range is empty: the lower bound wins). Color _within(Color color, double low, double high) { final luminance = color.computeLuminance(); if (high < low || luminance < low) return _toLuminance(color, low); if (luminance > high) return _toLuminance(color, high); return color; } /// [color] at the HSL lightness whose luminance is [target], by bisection /// (luminance grows with lightness at a fixed hue and saturation); rounded /// to the side that keeps the bound. Color _toLuminance(Color color, double target) { final hsl = HSLColor.fromColor(color); final up = target > color.computeLuminance(); var lo = up ? hsl.lightness : 0.0; var hi = up ? 1.0 : hsl.lightness; for (var i = 0; i < 24; i++) { final mid = (lo + hi) / 2; if (hsl.withLightness(mid).toColor().computeLuminance() < target) { lo = mid; } else { hi = mid; } } // 8-bit channels round; step a little further so the bound holds. final settle = up ? (hi + 0.004).clamp(0.0, 1.0) : (lo - 0.004).clamp(0.0, 1.0); return hsl.withLightness(settle).toColor(); } /// Each palette's colours before adjustment, ARGB, in ANSI order. const Map> _bases = { // The emulator's standard palette (the colours this app always used). TerminalPalette.theme: [ 0xFF000000, 0xFFCD3131, 0xFF0DBC79, 0xFFE5E510, // 0xFF2472C8, 0xFFBC3FBC, 0xFF11A8CD, 0xFFE5E5E5, 0xFF666666, 0xFFF14C4C, 0xFF23D18B, 0xFFF5F543, 0xFF3B8EEA, 0xFFD670D6, 0xFF29B8DB, 0xFFFFFFFF, ], // The long-standing desktop terminal default (Tango-like). TerminalPalette.classic: [ 0xFF2E3436, 0xFFCC0000, 0xFF4E9A06, 0xFFC4A000, // 0xFF3465A4, 0xFF75507B, 0xFF06989A, 0xFFD3D7CF, 0xFF555753, 0xFFEF2929, 0xFF8AE234, 0xFFFCE94F, 0xFF729FCF, 0xFFAD7FA8, 0xFF34E2E2, 0xFFEEEEEC, ], // Muted, warm accents of even lightness (Solarized-like). TerminalPalette.calm: [ 0xFF073642, 0xFFDC322F, 0xFF859900, 0xFFB58900, // 0xFF268BD2, 0xFFD33682, 0xFF2AA198, 0xFFEEE8D5, 0xFF586E75, 0xFFF0605C, 0xFFA8BF2A, 0xFFD9A825, 0xFF5AAEEA, 0xFFE86AA6, 0xFF4FC4B8, 0xFFFDF6E3, ], // Soft pastel accents (One-like). TerminalPalette.vivid: [ 0xFF282C34, 0xFFE06C75, 0xFF98C379, 0xFFE5C07B, // 0xFF61AFEF, 0xFFC678DD, 0xFF56B6C2, 0xFFABB2BF, 0xFF5C6370, 0xFFFF8A93, 0xFFB5E890, 0xFFFFD68A, 0xFF8CC8FF, 0xFFDE9BF0, 0xFF7FD4DE, 0xFFFFFFFF, ], };