Account for transparency when calculating WCAG color contrast #54
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:
.nan
and 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
0x808080
gray and that is the value we should be testing for contrast.📈 Coverage
Code
100%
![image](https://user-images.githubusercontent.com/1037520/226464222-5f2d98dd-6197-4208-889b-76181ad2963f.png)
Documentation
100%
![image](https://user-images.githubusercontent.com/1037520/226464373-e0e269a4-5172-4325-a8c8-ea04192efe8c.png)