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
Conversation
mpospese
requested review from
SahilSainiYML,
devkaranCT,
karthikyml,
sanjibchakraborty and
westongpt
March 20, 2023 20:59
devkaranCT
approved these changes
Mar 21, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
.nanand logging a warning if we attempt to check contrast on two transparent colors (insufficient info)Scope
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
0x808080gray and that is the value we should be testing for contrast.📈 Coverage
Code
100%

Documentation
100%
