portalReadable function

Color portalReadable(
  1. Color fg,
  2. Color bg,
  3. Color toward
)

fg kept readable on bg: drawn as is when it already clears AA text (kPortalTextContrast), else stepped toward toward (the page ink) until it does. A brand primary that is fine on a button (3.9:1 on a grey page) is too light for a text-only action.

Implementation

Color portalReadable(Color fg, Color bg, Color toward) {
  var c = fg;
  for (var i = 1; i <= 10 && portalContrastRatio(c, bg) < kPortalTextContrast; i++) {
    c = Color.lerp(fg, toward, i / 10)!;
  }
  return c;
}