Last active
May 27, 2019 23:46
-
-
Save adamgraham/0dff7ce1e7a3ba8f224b8cbc7b03da2c to your computer and use it in GitHub Desktop.
An extension of the iOS class UIColor to provide conversion to and from HSV (hue, saturation, value) colors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// An extension to provide conversion to and from HSV (hue, saturation, value) colors. | |
extension UIColor { | |
/// The HSV (hue, saturation, value) components of a color. | |
struct HSV: Hashable { | |
/// The hue component of the color, in the range [0, 360°]. | |
var hue: CGFloat | |
/// The saturation component of the color, in the range [0, 100%]. | |
var saturation: CGFloat | |
/// The value component of the color, in the range [0, 100%]. | |
var value: CGFloat | |
} | |
/// The HSV (hue, saturation, value) components of the color. | |
var hsv: HSV { | |
var (h, s, v) = (CGFloat(), CGFloat(), CGFloat()) | |
getHue(&h, saturation: &s, brightness: &v, alpha: nil) | |
return HSV(hue: h * 360.0, | |
saturation: s * 100.0, | |
value: v * 100.0) | |
} | |
/// Initializes a color from HSV (hue, saturation, value) components. | |
/// - parameter hsv: The components used to initialize the color. | |
/// - parameter alpha: The alpha value of the color. | |
convenience init(_ hsv: HSV, alpha: CGFloat = 1.0) { | |
self.init(hue: hsv.hue / 360.0, | |
saturation: hsv.saturation / 100.0, | |
brightness: hsv.value / 100.0, | |
alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment