Skip to content

Instantly share code, notes, and snippets.

@Tricertops
Last active March 31, 2017 14:40
Show Gist options
  • Save Tricertops/c2485f4039b3bdaeaf08932be95e6845 to your computer and use it in GitHub Desktop.
Save Tricertops/c2485f4039b3bdaeaf08932be95e6845 to your computer and use it in GitHub Desktop.
Construct UIColor using HSB components in Display P3 color space.
extension UIColor {
convenience init(displayP3Hue hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat = 1) {
/// HSB to RGB conversion doesn’t depend on color space, so we can use default UIColor space.
let converter = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0
converter.getRed(&red, green: &green, blue: &blue, alpha: nil)
self.init(displayP3Red: red, green: green, blue: blue, alpha: alpha)
}
}
UIColor(displayP3Hue: 0.2, saturation: 1, brightness: 1) // r 0,745 g 1,007 b -0,333 a 1,0
@kartickvad
Copy link

I forgot to say: I also get a warning saying:

[Graphics] UIColor created with component values far outside the expected range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment