Skip to content

Instantly share code, notes, and snippets.

@adamgraham
Last active July 6, 2022 02:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgraham/3ada1f7f4cdf8131dd3d2d95bd116cfc to your computer and use it in GitHub Desktop.
Save adamgraham/3ada1f7f4cdf8131dd3d2d95bd116cfc to your computer and use it in GitHub Desktop.
An extension of the iOS class UIColor to provide conversion to and from HSL (hue, saturation, lightness) colors.
/// An extension to provide conversion to and from HSL (hue, saturation, lightness) colors.
extension UIColor {
/// The HSL (hue, saturation, lightness) components of a color.
struct HSL: 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 lightness component of the color, in the range [0, 100%].
var lightness: CGFloat
}
/// The HSL (hue, saturation, lightness) components of the color.
var hsl: HSL {
var (h, s, b) = (CGFloat(), CGFloat(), CGFloat())
getHue(&h, saturation: &s, brightness: &b, alpha: nil)
let l = ((2.0 - s) * b) / 2.0
switch l {
case 0.0, 1.0:
s = 0.0
case 0.0..<0.5:
s = (s * b) / (l * 2.0)
default:
s = (s * b) / (2.0 - l * 2.0)
}
return HSL(hue: h * 360.0,
saturation: s * 100.0,
lightness: l * 100.0)
}
/// Initializes a color from HSL (hue, saturation, lightness) components.
/// - parameter hsl: The components used to initialize the color.
/// - parameter alpha: The alpha value of the color.
convenience init(_ hsl: HSL, alpha: CGFloat = 1.0) {
let h = hsl.hue / 360.0
var s = hsl.saturation / 100.0
let l = hsl.lightness / 100.0
let t = s * ((l < 0.5) ? l : (1.0 - l))
let b = l + t
s = (l > 0.0) ? (2.0 * t / b) : 0.0
self.init(hue: h, saturation: s, brightness: b, alpha: alpha)
}
}
@limejelly
Copy link

Great! Thanks a lot!

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