gpt4 book ai didi

xcode - 在 Swift 中将 UIColor 转换为十六进制(网络颜色)的最佳/最短方法是什么?

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:02 25 4
gpt4 key购买 nike

我想出了以下解决方案,但想知道是否有人有更简洁的方法(在 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))

导致#00ff00

其中 UIColor.greenColor() 是任何 UIColor 的示例。

编辑:摘自 Leonardo 的回答,我想稍微重构一下(他的所有测试仍然适用)。

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 {
let rgbaCache = rgba
return String(format: "#%02x%02x%02x", Int(round(rgbaCache.red * 255)), Int(round(rgbaCache.green * 255)), Int(round(rgbaCache.blue * 255)))
}

var htmlRGBA: String {
let rgbaCache = rgba
return String(format: "#%02x%02x%02x%02x", Int(round(rgbaCache.red * 255)), Int(round(rgbaCache.green * 255)), Int(round(rgbaCache.blue * 255)), Int(round(rgbaCache.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"

关于xcode - 在 Swift 中将 UIColor 转换为十六进制(网络颜色)的最佳/最短方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28696862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com