What is the range of hue values in `Color.init(hue:saturation:brightness:opacity:)`?

516 views Asked by At

The Color initializer init(hue:saturation:brightness:opacity:) is undocumented.

I think I can safely assume that saturation, brightness, and opacity normalize to the range of 0...1, where values outside that range are valid only for wide-gamut displays.

But I can't make this guess for hue. Is is 0...1, 0...τ, or 0...360?

1

There are 1 answers

0
Ky - On

Happily, it's easy to test this quickly.

I tossed this code into a new SwiftUI app and just looked at the preview to see the results. Turns out, it's 0...1!

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            Color(hue: 0/6, saturation: 1, brightness: 1)
            Color(hue: 1/6, saturation: 1, brightness: 1)
            Color(hue: 2/6, saturation: 1, brightness: 1)
            Color(hue: 3/6, saturation: 1, brightness: 1)
            Color(hue: 4/6, saturation: 1, brightness: 1)
            Color(hue: 5/6, saturation: 1, brightness: 1)
            Color(hue: 6/6, saturation: 1, brightness: 1)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

A screenshot proving that the range is 0...1 by showing colors with hues 0/6 through 6/6 in a VStack