Check contrast
White text on a white button is impossible to read. The button is there, and so is the label — you just can’t see it. You can measure how far two colors are apart as a ratio: 1 means no contrast, 21 is black on white.
Preview
Continue
Contrast ratio
1.00 : 1
Not enough contrast — even large text fails.
1 means no contrast, 21 is black on white.
- AA, normal textat least 4.5 : 1Fail
- AA, large textat least 3 : 1Fail
- AAA, normal textat least 7 : 1Fail
The same check in Swift
RGB stores sRGB channels from 0 to 1. luminance runs the sRGB linearization and the WCAG weights, and contrastRatio(with:) is the ratio. The label below picks black or white text in SwiftUI. resolve(in:) needs iOS 17.
Swift
import SwiftUI
/// Gamma-encoded sRGB. Each channel runs from 0 to 1, not 0 to 255.
struct RGB: Equatable, Sendable {
var red: Double
var green: Double
var blue: Double
/// WCAG 2 relative luminance.
///
/// Linearize each sRGB channel, then apply the luma weights:
/// `c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ^ 2.4`
/// `L = 0.2126 R + 0.7152 G + 0.0722 B`
var luminance: Double {
func linearize(_ channel: Double) -> Double {
if channel <= 0.04045 {
return channel / 12.92
}
return pow((channel + 0.055) / 1.055, 2.4)
}
let r = linearize(red)
let g = linearize(green)
let b = linearize(blue)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
}
/// WCAG 2 contrast ratio, from 1 (identical) to 21 (black on white).
func contrastRatio(with other: RGB) -> Double {
let a = luminance
let b = other.luminance
let lighter = max(a, b)
let darker = min(a, b)
return (lighter + 0.05) / (darker + 0.05)
}
}
// `Color.resolve(in:)` and `Color.Resolved` need iOS 17, macOS 14, tvOS 17, or watchOS 10.
// `red` / `green` / `blue` are gamma-encoded sRGB.
// `linearRed` / `linearGreen` / `linearBlue` are those channels after the sRGB
// curve above, so `RGB.luminance` and the linear components are the same math.
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension Color {
func rgb(in environment: EnvironmentValues) -> RGB {
let resolved = resolve(in: environment)
return RGB(
red: Double(resolved.red),
green: Double(resolved.green),
blue: Double(resolved.blue)
)
}
/// Black or white, whichever has the higher contrast ratio on this color.
func readableForeground(in environment: EnvironmentValues) -> Color {
let color = rgb(in: environment)
let black = RGB(red: 0, green: 0, blue: 0)
let white = RGB(red: 1, green: 1, blue: 1)
if color.contrastRatio(with: black) >= color.contrastRatio(with: white) {
return .black
}
return .white
}
}
/// Label that picks black or white text for `background`.
/// Requires iOS 17 because it calls `Color.resolve(in:)`.
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
struct AutoContrastLabel: View {
var title: String
var background: Color
@Environment(\.self) private var environment
var body: some View {
Text(title)
.font(.body.weight(.semibold))
.foregroundStyle(background.readableForeground(in: environment))
.padding(.horizontal, 20)
.padding(.vertical, 12)
.background(background, in: Capsule())
}
}