contrastRatio static method

double contrastRatio(
  1. Color textColor,
  2. Color backgroundColor
)

Calculate the contrast ratio between two Color objects.

Implementation

static double contrastRatio(Color textColor, Color backgroundColor) {
  double textLuminance = calculateLuminance(textColor);
  double backgroundLuminance = calculateLuminance(backgroundColor);
  double lighter = textLuminance > backgroundLuminance
      ? textLuminance
      : backgroundLuminance;
  double darker = textLuminance > backgroundLuminance
      ? backgroundLuminance
      : textLuminance;

  return (lighter + 0.05) / (darker + 0.05);
}