我想出了以下解决方案,但想知道是否有人对此有更清洁的方法(在Xcode-Playground中进行了测试):
let colors = CGColorGetComponents( UIColor.greenColor().CGColor ) let hexCol:String = NSString(format: "#%02x%02x%02x", Int(colors[0] * 255.0), Int(colors[1] * 255.0), Int(colors[2] * 255.0))
resulting in #00ff00
其中UIColor.greenColor()是任何UIColor的示例。
编辑:摘自莱昂纳多(Leonardo’s Answer),我想对其进行一些重构(他的所有测试仍然适用)。
extension UIColor { typealias RGBComponents = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) typealias HSBComponents = (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) var rgbComponents:RGBComponents { var c:RGBComponents = (0,0,0,0) if getRed(&c.red, green: &c.green, blue: &c.blue, alpha: &c.alpha) { return c } return (0,0,0,0) } var cssRGBA:String { return String(format: "rgba(%d,%d,%d, %.02f)", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255), Int(rgbComponents.blue * 255), Float(rgbComponents.alpha)) } var hexRGB:String { return String(format: "#%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255), Int(rgbComponents.blue * 255)) } var hexRGBA:String { return String(format: "#%02x%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255), Int(rgbComponents.blue * 255), Int(rgbComponents.alpha * 255) ) } var hsbComponents:HSBComponents { var c:HSBComponents = (0,0,0,0) if getHue(&c.hue, saturation: &c.saturation, brightness: &c.brightness, alpha: &c.alpha) { return c } return (0,0,0,0) } } let v1 = UIColor.redColor().rgbComponents.red let v2 = UIColor.redColor().cssRGBA let v3 = UIColor.redColor().hexRGBA let v4test = UIColor.RGBComponents(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
Xcode 8.2•Swift 3.0.2
从UIColor获取红色,绿色,蓝色和Alpha组件:
extension UIColor { var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0 if getRed(&r, green: &g, blue: &b, alpha: &a) { return (r,g,b,a) } return (0, 0, 0, 0) } // hue, saturation, brightness and alpha components from UIColor** var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) { var hue: CGFloat = 0, saturation: CGFloat = 0, brightness: CGFloat = 0, alpha: CGFloat = 0 if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) { return (hue, saturation, brightness, alpha) } return (0,0,0,0) } var htmlRGB: String { return String(format: "#%02x%02x%02x", Int(rgba.red * 255), Int(rgba.green * 255), Int(rgba.blue * 255)) } var htmlRGBA: String { return String(format: "#%02x%02x%02x%02x", Int(rgba.red * 255), Int(rgba.green * 255), Int(rgba.blue * 255), Int(rgba.alpha * 255) ) } }
测试它:
HTML颜色
let myWhiteWebColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).htmlRGBA // #ffffffff let myGreyWebColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1).htmlRGBA // #7f7f7fff let myBlackWebColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).htmlRGBA // #000000ff let myRedWebColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).htmlRGBA // #ff0000ff let myGreenWebColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1).htmlRGBA // #00ff00ff let myBlueWebColor = UIColor(red: 0, green: 0, blue: 1, alpha: 1).htmlRGBA // #0000ffff let myColorBlack = UIColor.black.htmlRGBA // #000000ff let myLghtGrayColor = UIColor.lightGray.htmlRGBA // #aaaaaaff let myDarkGrayColor = UIColor.darkGray.htmlRGBA // #555555ff let myRedHueWebColor = UIColor(hue: 1, saturation: 1, brightness: 1, alpha: 1).htmlRGBA //ff0000ff
RGB组件
UIColor.red.rgba // (1.0, 0.0, 0.0, 1.0) UIColor.red.rgba.red // 1 UIColor.red.rgba.green // 0 UIColor.red.rgba.blue // 0 UIColor.red.rgba.alpha // 1 UIColor.green.rgba // (0.0, 1.0, 0.0, 1.0) UIColor.green.rgba.red // 0 UIColor.green.rgba.green // 1 UIColor.green.rgba.blue // 0 UIColor.green.rgba.alpha // 1 UIColor.blue.rgba // (0.0, 0.0, 1.0, 1.0) UIColor.blue.rgba.red // 0 UIColor.blue.rgba.green // 0 UIColor.blue.rgba.blue // 1 UIColor.blue.rgba.alpha // 1
色相组件
UIColor.red.hsba // (0.0, 1.0, 1.0, 1.0) UIColor.red.hsba.hue // 1 UIColor.red.hsba.saturation // 1 UIColor.red.hsba.brightness // 1 UIColor.red.hsba.alpha // 1 UIColor.green.hsba // (0.333333333333333, 1.0, 1.0, 1.0) UIColor.green.hsba.hue // 0.3333333333333333 UIColor.green.hsba.saturation // 1 UIColor.green.hsba.brightness // 1 UIColor.green.hsba.alpha // 1 UIColor.blue.hsba // (0.666666666666667, 1.0, 1.0, 1.0) UIColor.blue.hsba.hue // 0.666666666666667 UIColor.blue.hsba.saturation // 1.0 UIColor.blue.hsba.brightness // 1.0 UIColor.blue.hsba.alpha // 1.0 UIColor.clear.hsba // (.0 0, .1 0, .2 0, .3 0) UIColor.blue.htmlRGB //"#0000ff" UIColor.blue.htmlRGBA //"#0000ffff"