contrastRatio method

num contrastRatio(
  1. Color background
)

The contrast ratio against a background color, as specified by https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef.

Return value will be in the range 1, 21.

The background must be opaque.

Implementation

num contrastRatio(Color background) {
  if (background.alpha != 1) {
    throw ArgumentError.value(background, 'background',
        'Cannot calculate contrast against non-opaque backgrounds.');
  }
  final Color a = alpha == 1 ? this : withBackground(background);
  final la = a._relativeLuminance;
  final lb = background._relativeLuminance;
  return (math.max(la, lb) + 0.05) / (math.min(la, lb) + 0.05);
}