Skip to content

Color Contrast Check for Focus Indicator ​

This document describes how the tool performs the automatic color contrast (CC) check on the focus indicator.

1. Algorithm Description ​

Once it is determined that the focus indicator is visible, its contrast against the background is checked. This detail is important: the CC check assumes that the focus indicator exists and is visible.

We will call image1 the image of the unfocused element and image2 the image of the focused element.

The difference between image1 and image2 is therefore another image with all pixels in black except for those corresponding to the focus indicator, which only appears in image2.

The fg_pixels are those pixels from image2 whose coordinates correspond to the non-black pixels in the difference image.

The bg_pixels are those pixels from image1 whose coordinates fall within a 2-pixel radius neighborhood of any non-black pixel in the difference image.

So fg_pixels correspond to the focus indicator pixels while bg_pixels correspond to the pixels around this focus indicator.

Subsequently, the bg_pixels are separated into connected components. A connected component is a set of adjacent pixels such that the color gradient between them does not exceed a defined threshold. In our case, this threshold is (10, 10, 10).

The same connected component separation procedure is performed on the fg_pixels.

For each connected component, the average color is calculated: the mean of all pixels in the component.

For each background average color, its contrast ratio is calculated against each foreground average color. The resulting contrast ratio is the maximum contrast ratio achieved for each pair of average colors.

Limitations ​

The above approach does not produce correct contrast ratios in all situations and may lead to missing issues. However, it should not cause false positives since it takes the maximum possible contrast ratio between the focus indicator and its neighborhood.

Scenarios where missing issues may occur are those where the focus indicator has a non-uniform background. For example, on the right the background is black and on the left it is white, while the focus indicator is a color that does not contrast well with black but does with white. In this case, the part that does not contrast well will be ignored because only the pair of average colors that contrasts most is considered, without taking into account the position of the background connected components relative to the foreground ones.

Despite these limitations, we consider that the current state of the test can benefit auditors in the vast majority of cases.

Future Improvements ​

The analysis can be improved by including position information of connected components so that average colors that are far apart from each other are not compared.