runContrastTest function

  1. @visibleForTesting
void runContrastTest(
  1. Color backgroundColor,
  2. Color foregroundColor, {
  3. ReadabilityLevel readabilityLevel = ReadabilityLevel.normal,
})

Checks if given backgroundColor and foregroundColor meet minimum contrast ratio value for given readabilityLevel.

Throws an AssertionError if contrast ratio is lower than minimum required value for given readabilityLevel.

Example error message:

1.0:1 contrast ratio of Color(0xffffffff) and Color(0xffffffff)
does not meet minimum acceptable contrast ratio
for ReadabilityLevel.normal, which is 4.5:1.

Implementation

@visibleForTesting
void runContrastTest(
  Color backgroundColor,
  Color foregroundColor, {
  ReadabilityLevel readabilityLevel = ReadabilityLevel.normal,
}) {
  final contrastRatio = calculateContrast(backgroundColor, foregroundColor);
  assert(
    doesMatchMinContrastRatio(
          contrastRatio,
          readabilityLevel: readabilityLevel,
        ) ==
        true,
    '${contrastRatio.toStringAsFixed(2)}:1 contrast ratio of $backgroundColor and $foregroundColor '
    'does not meet minimum acceptable contrast ratio for '
    '$readabilityLevel, which is ${readabilityLevel.minimumAcceptableContrastRatio}:1',
  );
}