Test iOS Color Contrast on a Cloud Mac: sRGB and Thresholds

Test iOS Color Contrast on a Cloud Mac: sRGB and Thresholds

Test iOS Color Contrast on a Cloud Mac: sRGB and Thresholds

Developers in two locations update the light and dark themes separately. After the changes are merged, secondary text looks fainter. The design specifies “contrast 4.5,” yet the tests did not catch the regression. The problem may not be the color choices: the script may have formatted 4.478 as 4.5 before using that display value to decide whether the pair passes. Turn the color calculations into a reproducible set of executable test fixtures, and a cloud Mac can check the same rules on every commit—without relying on a reviewer’s display or visual judgment.

Define the inputs and pass criteria first

This article checks contrast only between opaque colors in standard sRGB. Each input consists of three channel values between 0 and 1. Negative, nonfinite, and out-of-range values are rejected, not silently clamped. That way, an upstream unit conversion mistake causes a test failure rather than producing a plausible-looking result.

The threshold for normal text is 4.5:1. Text that meets the definition of large text can use 3:1, but a component named “Large Heading” does not automatically qualify. Font size and weight must be verified separately against the interface rules. Normal text is the default case throughout this article.

A passing contrast result means only that a color pair meets the specified numerical requirement. It does not mean the entire page has passed an accessibility review. Font size, focus indicators, state communication, and the actual background still need to be checked.

Record color roles as pairs such as “secondary text / card background,” rather than testing a palette in isolation. The same text color needs a separate calculation for each surface it appears on.

Build the calculation fixture on a cloud Mac

First, run xcrun swift --version to confirm that the current user can access the Swift toolchain. Save the following code as contrast-check.swift, then run xcrun swift contrast-check.swift. The script does not require a simulator or read the machine’s display configuration.

import Foundation

enum ColorError: Error {
    case invalidChannel
}

struct RGB {
    let r: Double
    let g: Double
    let b: Double

    init(_ r: Double, _ g: Double, _ b: Double) throws {
        guard [r, g, b].allSatisfy({
            $0.isFinite && (0.0...1.0).contains($0)
        }) else {
            throw ColorError.invalidChannel
        }
        self.r = r
        self.g = g
        self.b = b
    }
}

func linear(_ channel: Double) -> Double {
    channel <= 0.04045
        ? channel / 12.92
        : pow((channel + 0.055) / 1.055, 2.4)
}

func luminance(_ color: RGB) -> Double {
    0.2126 * linear(color.r)
        + 0.7152 * linear(color.g)
        + 0.0722 * linear(color.b)
}

func contrast(_ a: RGB, _ b: RGB) -> Double {
    let x = luminance(a)
    let y = luminance(b)
    return (max(x, y) + 0.05) / (min(x, y) + 0.05)
}

func near(_ actual: Double, _ expected: Double) {
    precondition(abs(actual - expected) < 1e-10)
}

let black = try RGB(0, 0, 0)
let white = try RGB(1, 1, 1)
let gray = try RGB(119.0 / 255, 119.0 / 255, 119.0 / 255)

near(contrast(black, white), 21)
near(contrast(gray, gray), 1)
near(contrast(gray, white), contrast(white, gray))
near(linear(0.04045), 0.04045 / 12.92)
near(linear(0.04046), pow((0.04046 + 0.055) / 1.055, 2.4))
precondition(contrast(gray, white) < 4.5)
precondition(contrast(black, white) >= 4.5)

for bad in [-0.01, 1.01, Double.nan, Double.infinity] {
    do {
        _ = try RGB(bad, 0, 0)
        preconditionFailure("Invalid channel accepted")
    } catch ColorError.invalidChannel {
    }
}

Do not skip the linearization step. Applying the weights directly to encoded red, green, and blue values produces incorrect luminance values for intermediate colors. Nor can the coefficients be replaced with a simple average: the green and blue channels contribute differently to relative luminance.

Use boundary cases to catch incorrect implementations

Black against white should return 21, and a color against itself should return 1. Swapping foreground and background must leave the result unchanged. These three checks cover the formula’s constants, its lower bound, and symmetry, respectively, but they are not enough to catch incorrect decisions near the threshold.

The script also tests #777777 against white. Its contrast ratio is approximately 4.478, so it must fail the 4.5 threshold. Reports may display two decimal places, but pass/fail decisions must always use the original Double. Approximate numerical assertions use a tolerance; comparisons against the required threshold do not get an extra “error allowance.”

There is also a sample on each side of the linearization breakpoint to catch implementations that incorrectly use a single power function. When integrating the fixture into a project, add the current theme colors and any color pairs that regressed in the previous version. Do not keep only easy-to-pass samples such as pure black and white.

Add theme colors to commit checks

Export channel values from the color configuration the project actually uses, then pass them to the calculation module. A manually copied test palette can produce false passes when the code changes but the fixture does not. Record the color space during export as well; do not treat channel values from another gamut as sRGB.

Each record should include at least the text role, its surface, the theme, the foreground, and the background. Passing in the light theme does not imply passing in the dark theme. Error messages, disabled-state descriptions, and selected states also need their own results rather than inheriting those of default body text.

Run the color checks as a separate pipeline step. If they are included in the application’s test suite, replace the runtime checks with test-framework assertions, since optimization settings may remove runtime checks. Reports should retain the names of failing pairs, their unrounded ratios, and the required thresholds—not just a failure status that gives no clue where to look.

Separate solid-color calculations from rendered UI review

Semitransparent text, gradient backgrounds, image backgrounds, and material effects are outside the direct scope of this three-channel fixture. You cannot simply discard the alpha channel and continue calculating, or pick an arbitrary point in a gradient to represent the entire background.

For transparency, first fix the background and compositing conditions, then obtain the resulting opaque colors. For text over an image or gradient, checks must cover the low-contrast areas that the text actually overlaps. If necessary, adjust the component so the text sits on a stable, opaque surface. Antialiased edge pixels in screenshots are not suitable substitutes for the intended foreground color either.

The final handoff should document three outcomes: the calculation fixture passes, the theme-role pairs pass, and the rendered interface has been reviewed under controlled conditions. If the color space, opacity, or background structure changes, rerun the relevant checks instead of carrying forward an old pass label. Teams in both locations can then discuss the same inputs and criteria, rather than ending the review with “it looks fine on my screen.”

Frequently asked questions

Can a contrast ratio of 4.478 round up to a passing 4.5?

No. Compare the unrounded ratio with the threshold. Rounding is appropriate for display, not for deciding whether a color pair passes.

Does the opaque-color script validate translucent text?

Not directly. Establish the composited opaque colors first, or obtain them from controlled rendering. Simply discarding alpha is not a valid check.

Get a dedicated Mac mini for your project

A whole machine, dedicated to one customer—ideal for remote development, automated builds, and always-on workloads. Rent by the day for short-term projects, then choose the term that works for you.

Get a cloud Mac

Choose a setup that fits your project

Check the memory, storage, and rental term first. Choose a rental for continuous availability, or keep using a suitable device you already own.