ratio static method

double ratio(
  1. Color foreground,
  2. Color background
)

WCAG contrast ratio between foreground and background, 1 to 21.

A translucent foreground is first composited over background.

Implementation

static double ratio(Color foreground, Color background) {
  final fg = foreground.a < 1
      ? Color.alphaBlend(foreground, background)
      : foreground;
  final a = luminance(fg);
  final b = luminance(background);
  final lighter = a > b ? a : b;
  final darker = a > b ? b : a;
  return (lighter + 0.05) / (darker + 0.05);
}