Zum Inhalt springen

In Entwicklung. Diese Website wird gerade neu aufgebaut. Inhalte, Screenshots und Links können sich noch ändern.

Martin SchulzeiOS Developer

Demos

Kontrast prüfen

Weiße Schrift auf einem weißen Button siehst du nicht. Der Button ist da, der Text auch — nur eben unsichtbar. Wie stark sich zwei Farben unterscheiden, kannst du als Verhältnis messen: 1 heißt kein Kontrast, 21 ist Schwarz auf Weiß.

Textfarbe
Hintergrundfarbe

Vorschau

Kontrastverhältnis

1,00 : 1

Zu wenig Kontrast — auch großer Text fällt durch.

1 ist kein Kontrast, 21 ist Schwarz auf Weiß.

  • AA, normaler Textmindestens 4,5 : 1Nicht bestanden
  • AA, großer Textmindestens 3 : 1Nicht bestanden
  • AAA, normaler Textmindestens 7 : 1Nicht bestanden

Dieselbe Prüfung in Swift

RGB hält sRGB-Kanäle von 0 bis 1. luminance rechnet sie mit der sRGB-Linearisierung in relative Leuchtdichte um, contrastRatio(with:) ist das Verhältnis. Das Label darunter wählt in SwiftUI automatisch Schwarz oder Weiß. resolve(in:) gibt es ab 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())
    }
}