import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /// Colours for recorded outcome names, assigned per run by first appearance. /// /// Outcome words are pipeline-defined ('approved', 'rejected', 'retry', …), so /// no name is hard-wired to a colour: the same name always gets the same /// colour within a run and two different names never share one. The first /// [_hues] names use well-separated hues; later ones continue around the /// colour wheel by the golden angle so they stay distinct. class OutcomePalette { OutcomePalette(Iterable outcomes) { for (final outcome in outcomes) { final key = _key(outcome); if (key.isNotEmpty) _index.putIfAbsent(key, () => _index.length); } } final Map _index = {}; static String _key(String outcome) => outcome.trim().toLowerCase(); /// The position [outcome] was first seen at, or null for a blank or unseen /// name. int? indexOf(String outcome) => _index[_key(outcome)]; /// [outcome]'s colour for the host's brightness; an unseen or blank name /// uses the host's secondary text colour. Color colorFor(BuildContext context, String outcome) { final cupertino = context.findAncestorWidgetOfExactType() == null; final brightness = cupertino ? CupertinoTheme.brightnessOf(context) : Theme.of(context).brightness; final index = indexOf(outcome); if (index == null) { return cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; } return outcomeColorAt(index, brightness); } } const List _hues = [ Colors.blue, Colors.green, Colors.deepOrange, Colors.purple, Colors.teal, Colors.pink, Colors.amber, Colors.indigo, Colors.brown, Colors.cyan, ]; /// The colour for the [index]th distinct outcome, readable on the page in /// [brightness] (darker shades on light pages, lighter on dark). Color outcomeColorAt(int index, Brightness brightness) { final dark = brightness == Brightness.dark; if (index < _hues.length) { final hue = _hues[index]; return dark ? hue.shade300 : hue.shade800; } final degrees = (index * 137.508) % 360; return HSLColor.fromAHSL(1, degrees, 0.6, dark ? 0.7 : 0.35).toColor(); }