solveGlassKey function

KeyColors? solveGlassKey({
  1. required Lch tint,
  2. required int alpha,
  3. required Lch label,
  4. required List<int> bgs,
  5. required double target,
  6. required int edge,
  7. int rounds = 14,
})

Solves a glass key: the label (from label) on tint at alpha composited over every one of bgs, to target. When no label of the same polarity reaches it, the tint moves away from the label (0.04 in lightness) and densifies (+14 alpha) for up to rounds rounds. Null when unreachable.

Implementation

KeyColors? solveGlassKey({
  required Lch tint,
  required int alpha,
  required Lch label,
  required List<int> bgs,
  required double target,
  required int edge,
  int rounds = 14,
}) {
  final light = label.l > 0.5;
  final start = oklchToArgb(label.l, label.c, label.h);
  var l = tint.l;
  var a = alpha;
  for (var i = 0; i < rounds; i++) {
    final face = withAlpha(oklchToArgb(l, tint.c, tint.h), a);
    final samples = faceSamples(face, bgs);
    final solved = solveOnAll(start, samples, target);
    if (solved != null && _keepsPolarity(solved, samples, light: light)) {
      return KeyColors(face: face, label: solved, edge: edge);
    }
    l = clamp01(l + (light ? -0.04 : 0.04));
    a = a + 14 > 250 ? 250 : a + 14;
  }
  return null;
}