contrastRatio function

double contrastRatio(
  1. Color foreground,
  2. Color background, {
  3. bool hasDarkBackground = true,
})

Computes the WCAG contrast ratio of foreground vs background.

The returned value is always >= 1.0.

Implementation

double contrastRatio(
  Color foreground,
  Color background, {
  bool hasDarkBackground = true,
}) {
  final l1 = relativeLuminance(
    foreground,
    hasDarkBackground: hasDarkBackground,
  );
  final l2 = relativeLuminance(
    background,
    hasDarkBackground: hasDarkBackground,
  );
  final high = math.max(l1, l2);
  final low = math.min(l1, l2);
  return (high + 0.05) / (low + 0.05);
}