DayByDayTests/ContrastTests.swift
145 lines · 5948 bytes
13 symbols in this file
1import SwiftUI
2import Testing
3import UIKit
4@testable import DayByDay
5
6/// WCAG contrast for white or dark symbols and labels on the app's fills.
7struct ContrastTests {
8 static let styles: [UIUserInterfaceStyle] = [.light, .dark]
9
10 func luminance(_ color: Color, _ style: UIUserInterfaceStyle) -> Double {
11 let ui = UIColor(color).resolvedColor(with: UITraitCollection(userInterfaceStyle: style))
12 var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
13 ui.getRed(&r, green: &g, blue: &b, alpha: &a)
14 func lin(_ c: CGFloat) -> Double {
15 let c = Double(c)
16 return c <= 0.03928 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4)
17 }
18 return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b)
19 }
20
21 func contrast(_ a: Color, _ b: Color, _ style: UIUserInterfaceStyle) -> Double {
22 let la = luminance(a, style), lb = luminance(b, style)
23 return (max(la, lb) + 0.05) / (min(la, lb) + 0.05)
24 }
25
26 @Test(arguments: DayByDay.Category.allCases) func categoryColorAgainstWhite(_ category: DayByDay.Category) {
27 for style in Self.styles {
28 #expect(contrast(category.color, .white, style) >= 3, "\(category) \(style.rawValue)")
29 }
30 }
31
32 @Test func todayBannerAgainstWhite() {
33 for color in TodayBanner.gradientColors {
34 #expect(contrast(color, .white, .light) >= 3)
35 }
36 }
37
38 @Test(arguments: Season.allCases) func wheelColorAgainstWhite(_ season: Season) {
39 for style in Self.styles {
40 #expect(contrast(season.wheelColor, .white, style) >= 3, "\(season)")
41 }
42 }
43
44 /// Uncounted objects sit on the page background in the numbers color.
45 @Test func countingObjectsAgainstBackground() {
46 let numbers = DayByDay.Category.numbers.color
47 #expect(contrast(numbers, .white, .light) >= 3)
48 #expect(contrast(numbers, .black, .dark) >= 3)
49 }
50
51 /// Train wheels sit on the page background in both modes.
52 @Test func trainTireAgainstBackgrounds() {
53 #expect(contrast(TrainStyle.tire, .white, .light) >= 3)
54 #expect(contrast(TrainStyle.tire, .black, .dark) >= 3)
55 }
56
57 @Test(arguments: LearnAnimal.allCases) func animalColorAgainstWhite(_ animal: LearnAnimal) {
58 #expect(contrast(animal.color, .white, .light) >= 3, "\(animal)")
59 }
60
61 @Test(arguments: LearnLetter.allCases) func letterColorAgainstWhite(_ letter: LearnLetter) {
62 #expect(contrast(letter.color, .white, .light) >= 3, "\(letter)")
63 }
64
65 /// Sort bins carry white labels and must stand out on a black page.
66 @Test func sortBinFill() {
67 #expect(contrast(SortStyle.binFill, .white, .light) >= 3)
68 #expect(contrast(SortStyle.binFill, .black, .dark) >= 3)
69 }
70
71 @Test(arguments: LearnWeather.allCases) func weatherCardAgainstWhite(_ weather: LearnWeather) {
72 #expect(contrast(weather.color, .white, .light) >= 3, "\(weather)")
73 }
74
75 @Test(arguments: LearnShape.allCases) func shapeCardAgainstWhite(_ shape: LearnShape) {
76 #expect(contrast(shape.color, .white, .light) >= 3, "\(shape)")
77 }
78
79 @Test(arguments: Season.allCases) func seasonCardAgainstWhite(_ season: Season) {
80 #expect(contrast(season.color, .white, .light) >= 3, "\(season)")
81 }
82
83 @Test(arguments: MonthOfYear.allCases) func monthCardAgainstWhite(_ month: MonthOfYear) {
84 #expect(contrast(month.color, .white, .light) >= 3, "\(month)")
85 }
86
87 @Test(arguments: LearnFood.allCases) func foodCardAgainstWhite(_ food: LearnFood) {
88 #expect(contrast(food.color, .white, .light) >= 3, "\(food)")
89 }
90
91 @Test(arguments: LearnBodyPart.allCases) func bodyPartCardAgainstWhite(_ part: LearnBodyPart) {
92 #expect(contrast(part.color, .white, .light) >= 3, "\(part)")
93 }
94
95 @Test(arguments: LearnNumber.allCases) func numberCardAgainstWhite(_ number: LearnNumber) {
96 #expect(contrast(number.color, .white, .light) >= 3, "\(number)")
97 }
98
99 @Test(arguments: LearnColor.allCases) func colorCardLabel(_ color: LearnColor) {
100 #expect(contrast(color.color, color.labelColor, .light) >= 3, "\(color)")
101 }
102
103 @Test(arguments: DayOfWeek.allCases) func dayColorAgainstWhite(_ day: DayOfWeek) {
104 #expect(contrast(day.color, .white, .light) >= 3, "\(day)")
105 }
106
107 @Test func stickerFillAgainstWhite() {
108 #expect(contrast(StickerStyle.fill, .white, .light) >= 3)
109 }
110
111 @Test func playAgainAgainstWhite() {
112 #expect(contrast(RoundStyle.playAgainFill, .white, .light) >= 3)
113 }
114
115 @Test(arguments: ProfileColor.allCases) func avatarSymbolOnProfileColor(_ color: ProfileColor) {
116 for style in Self.styles {
117 #expect(contrast(color.symbolColor, color.color, style) >= 3, "\(color) \(style.rawValue)")
118 }
119 }
120
121 /// White and black color swatches need an edge that shows on either background.
122 @Test func swatchOutlineAgainstBackgrounds() {
123 #expect(contrast(ChoiceStyle.swatchOutline, .white, .light) >= 3)
124 #expect(contrast(ChoiceStyle.swatchOutline, .black, .dark) >= 3)
125 }
126
127 @Test func foodFillAgainstWhite() {
128 for style in Self.styles {
129 #expect(contrast(ChoiceStyle.foodFill, .white, style) >= 3)
130 }
131 }
132
133 /// Body on the page in both modes; each feature on what it sits on.
134 @Test func mascotContrast() {
135 #expect(contrast(MascotStyle.body, .white, .light) >= 3)
136 #expect(contrast(MascotStyle.body, .black, .dark) >= 3)
137 for style in Self.styles {
138 for feature in [MascotStyle.eyeWhite, MascotStyle.belly, MascotStyle.beak, MascotStyle.ink] {
139 #expect(contrast(feature, MascotStyle.body, style) >= 3, "\(feature) \(style.rawValue)")
140 }
141 #expect(contrast(MascotStyle.ink, MascotStyle.eyeWhite, style) >= 3)
142 #expect(contrast(MascotStyle.ink, MascotStyle.belly, style) >= 3)
143 }
144 }
145}