Created
June 7, 2015 23:11
-
-
Save ericdke/a535390e5fabf97c0e88 to your computer and use it in GitHub Desktop.
Swift: color components (css, hex, rgb) from NSColor
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
extension NSColor { | |
func components() -> ((alpha: String, red: String, green: String, blue: String, css: String), (alpha: CGFloat, red: CGFloat, green: CGFloat, blue: CGFloat), (alpha: CGFloat, red: CGFloat, green: CGFloat, blue: CGFloat))? { | |
var red: CGFloat = 0 | |
var green: CGFloat = 0 | |
var blue: CGFloat = 0 | |
var alpha: CGFloat = 0 | |
if var color = self.colorUsingColorSpaceName(NSCalibratedRGBColorSpace) { | |
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | |
let nsTuple = (alpha: alpha, red: red, green: green, blue: blue) | |
red = round(red * 255.0) | |
green = round(green * 255.0) | |
blue = round(blue * 255.0) | |
alpha = round(alpha * 255.0) | |
let xalpha = String(Int(alpha), radix: 16, uppercase: true) | |
let xred = String(Int(red), radix: 16, uppercase: true) | |
let xgreen = String(Int(green), radix: 16, uppercase: true) | |
let xblue = String(Int(blue), radix: 16, uppercase: true) | |
let css = "#\(xred)\(xgreen)\(xblue)" | |
let hexTuple = (alpha: xalpha, red: xred, green: xgreen, blue: xblue, css: css) | |
let rgbTuple = (alpha: alpha, red: red, green: green, blue: blue) | |
return (hexTuple, rgbTuple, nsTuple) | |
} | |
return nil | |
} | |
} | |
let c = NSColor(red: 0.2, green: 0.456231576, blue: 0.7, alpha: 1) | |
let (hexTuple, rgbTuple, nsTuple) = c.components()! | |
hexTuple.css // "#285FA3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment