1bf2ae41c4
A completed step's outcome chip took the success colour, so 'rejected' read green beside 'success'. Outcome chips now get one colour per distinct outcome name in the run (assigned by first appearance, from a well-separated hue list that continues by the golden angle), so equal names match and different names never share a colour. No outcome word is wired to a colour; the status glyph still shows the step's state.
69 lines
2.3 KiB
Dart
69 lines
2.3 KiB
Dart
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<String> outcomes) {
|
|
for (final outcome in outcomes) {
|
|
final key = _key(outcome);
|
|
if (key.isNotEmpty) _index.putIfAbsent(key, () => _index.length);
|
|
}
|
|
}
|
|
|
|
final Map<String, int> _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<MaterialApp>() == 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<MaterialColor> _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();
|
|
}
|