Skip to content
This repository was archived by the owner on May 20, 2026. It is now read-only.

Account for transparency when calculating WCAG color contrast - #54

Merged
mpospese merged 3 commits into
mainfrom
issue/42-color-contrast-with-alpha
Mar 21, 2023
Merged

Account for transparency when calculating WCAG color contrast#54
mpospese merged 3 commits into
mainfrom
issue/42-color-contrast-with-alpha

Conversation

@mpospese

@mpospese mpospese commented Mar 20, 2023

Copy link
Copy Markdown
Contributor

Introduction

When we perform our color contrast checking, we only look at the RGB channels and not the A. When the foreground color has alpha, it becomes blended with the background color, and it is this blended color that we should perform the check against.

Purpose

Fix #42 Account for transparency by:

  1. returning .nan and logging a warning if we attempt to check contrast on two transparent colors (insufficient info)
  2. if just one color is transparent, blend it with the other color and compare that blended color to the other color.

Scope

  • Adjustments to UIColor.contrastRatio(to:)

Discussion

So the problem was we weren't considering alpha, so contrast between black at 50% opacity and white was same as that between solid black and white. When really that black would appear as 0x808080 gray and that is the value we should be testing for contrast.

📈 Coverage

Code

100%
image

Documentation

100%
image

@mpospese
mpospese merged commit b7fd931 into main Mar 21, 2023
@mpospese
mpospese deleted the issue/42-color-contrast-with-alpha branch March 21, 2023 14:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Account for transparency when calculating WCAG color contrast

2 participants