Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for transparency when calculating WCAG color contrast #54

Merged
merged 3 commits into from
Mar 21, 2023

Conversation

mpospese
Copy link
Contributor

@mpospese mpospese commented Mar 20, 2023

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Account for transparency when calculating WCAG color contrast
2 participants