1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import UIKit
import XCTest
@testable import OldFella
final class AvatarRingTests: XCTestCase {
private func image(background: UIColor, center: UIColor? = nil, size: CGSize = CGSize(width: 256, height: 256))
-> CGImage {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
return UIGraphicsImageRenderer(size: size, format: format).image { context in
background.setFill()
context.fill(CGRect(origin: .zero, size: size))
if let center {
center.setFill()
context.fill(
CGRect(
x: size.width * 0.25, y: size.height * 0.25,
width: size.width * 0.5, height: size.height * 0.5))
}
}.cgImage!
}
func testLightAndDarkEdgesChooseContrastingRingsInBothModes() {
let black = AvatarRingColors.calculate(for: image(background: .black))
let white = AvatarRingColors.calculate(for: image(background: .white))
for color in [black.light, black.dark] {
XCTAssertGreaterThan((color.luminance + 0.05) / 0.05, 1.9)
}
for color in [white.light, white.dark] {
XCTAssertGreaterThan(1.05 / (color.luminance + 0.05), 1.9)
}
XCTAssertGreaterThan(1.05 / (black.light.luminance + 0.05), 2)
XCTAssertGreaterThan((white.dark.luminance + 0.05) / 0.05, 2)
}
func testLightModeRingsStandOutAgainstWhiteAndGroupedBackgrounds() {
let avatars: [UIColor] = [
.white, .black, .gray, .blue, .red, .green, .yellow, .cyan,
UIColor(red: 0.95, green: 0.82, blue: 0.7, alpha: 1)
]
for background in avatars {
let ring = AvatarRingColors.calculate(for: image(background: background)).light
XCTAssertGreaterThanOrEqual(1.05 / (ring.luminance + 0.05), 3.8)
XCTAssertGreaterThanOrEqual(0.9 / (ring.luminance + 0.05), 3.3)
}
}
func testDarkModeColorsStayTheSame() {
let blue = AvatarRingColors.calculate(for: image(background: .blue)).dark
XCTAssertEqual(blue.red, 0.3810918585155354, accuracy: 0.000001)
XCTAssertEqual(blue.green, 0.3810918585155354, accuracy: 0.000001)
XCTAssertEqual(blue.blue, 1, accuracy: 0.000001)
let green = AvatarRingColors.calculate(for: image(background: .green)).dark
XCTAssertEqual(green.luminance, 0.3326, accuracy: 0.000001)
XCTAssertEqual(green.red, 0, accuracy: 0.000001)
XCTAssertEqual(green.blue, 0, accuracy: 0.000001)
}
func testRingsKeepTheAvatarColorAndChangeItsBrightness() {
let blue = AvatarRingColors.calculate(for: image(background: .blue))
let red = AvatarRingColors.calculate(for: image(background: .red))
let green = AvatarRingColors.calculate(for: image(background: .green))
for color in [blue.light, blue.dark] {
XCTAssertGreaterThan(color.blue, color.red + 0.1)
XCTAssertGreaterThan(color.blue, color.green + 0.1)
XCTAssertGreaterThan(color.luminance, 0.0722 * 2)
}
for color in [red.light, red.dark] {
XCTAssertGreaterThan(color.red, color.green + 0.01)
XCTAssertGreaterThan(color.red, color.blue + 0.01)
}
for color in [green.light, green.dark] {
XCTAssertGreaterThan(color.green, color.red + 0.1)
XCTAssertGreaterThan(color.green, color.blue + 0.1)
XCTAssertLessThan(color.luminance, 0.7152 / 2)
}
}
func testAvatarColorsOutsideAnyPresetPaletteRemainDistinct() {
let teal = AvatarRingColors.calculate(
for: image(background: UIColor(red: 0.1, green: 0.5, blue: 0.6, alpha: 1)))
let violet = AvatarRingColors.calculate(
for: image(background: UIColor(red: 0.5, green: 0.1, blue: 0.6, alpha: 1)))
for color in [teal.light, teal.dark] {
XCTAssertGreaterThan(color.blue, color.green)
XCTAssertGreaterThan(color.green, color.red)
}
for color in [violet.light, violet.dark] {
XCTAssertGreaterThan(color.blue, color.red)
XCTAssertGreaterThan(color.red, color.green)
}
XCTAssertNotEqual(teal, violet)
}
func testRingBrightnessTracksEdgeBrightnessWithoutPaletteSteps() {
let first = AvatarRingColors.calculate(for: image(background: UIColor(white: 0.10, alpha: 1)))
let second = AvatarRingColors.calculate(for: image(background: UIColor(white: 0.12, alpha: 1)))
XCTAssertGreaterThan(second.light.luminance, first.light.luminance)
XCTAssertGreaterThan(second.dark.luminance, first.dark.luminance)
XCTAssertLessThan(second.light.luminance - first.light.luminance, 0.04)
}
func testWhiteBackgroundDoesNotWashOutColoredSubject() {
let portrait = AvatarRingColors.calculate(for: image(background: .white, center: .blue))
for color in [portrait.light, portrait.dark] {
XCTAssertGreaterThan(color.blue, color.red + 0.3)
XCTAssertGreaterThan(color.blue, color.green + 0.3)
}
}
func testColoredRingsStaySaturatedOnMixedEdges() {
let portrait = AvatarRingColors.calculate(for: image(background: .gray, center: .red))
for color in [portrait.light, portrait.dark] {
XCTAssertGreaterThan(color.red, color.green + 0.3)
XCTAssertGreaterThan(color.red, color.blue + 0.3)
}
}
func testTransparentImageUsesNeutralFallback() {
XCTAssertEqual(AvatarRingColors.calculate(for: image(background: .clear)), .fallback)
}
func testSamplingMatchesCircularAspectFillCrop() {
let square = AvatarRingColors.calculate(for: image(background: .red))
let wide = image(background: .blue, size: CGSize(width: 512, height: 256))
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let cropped = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 256), format: format).image { context in
context.cgContext.draw(wide, in: CGRect(x: 0, y: 0, width: 512, height: 256))
UIColor.red.setFill()
context.fill(CGRect(x: 128, y: 0, width: 256, height: 256))
}.cgImage!
XCTAssertEqual(AvatarRingColors.calculate(for: cropped), square)
}
func testColorCalculationPerformance() {
let avatar = image(background: .blue, center: .yellow)
measure {
for _ in 0..<100 { _ = AvatarRingColors.calculate(for: avatar) }
}
}
}