SwiftUI 中使用 Hex 转换成 Color,并适配多种颜色模式

SwiftUI 中的 Color 颜色使用非常方便,但是默认没有支持 Hex 十六进制颜色的创建方式,而 Hex 十六进制会大量的出现在UI 设计稿,取色软件等地方,我们需要使用它来创建 Color 时需要转换成RGB颜色等方式,不太方便。

以下提供两种实用的 extension Color ,你可以单独创建一个 2 文件直接复制粘贴以下代码直接使用。


使用字符串形式 Hex 值创建 Color

  • 允许 hex 值带或不带前缀 # 字符
  • 灰色阴影的 2 位数字格式
  • 速记 的 3 位格式
  • 带 alpha 的灰色的 4 位数字格式
  • RGB 的 6 位数字格式
  • RGBA 的 8 位格式
  • 所有格式无效时返回 nil

使用示例

let grayColor = Color("4f")
let grayColor2 = Color("#68")
let grayColor3 = Color("7813")
let redColor = Color("f00")
let translucentGreen = Color("#00FF0066")
let blue = Color("0000FF")
let invalid = Color("0000F")

Color 扩展代码

extension Color {
    init?(_ hex: String) {
        var str = hex
        if str.hasPrefix("#") {
            str.removeFirst()
        }
        if str.count == 3 {
            str = String(repeating: str[str.startIndex], count: 2)
                + String(repeating: str[str.index(str.startIndex, offsetBy: 1)], count: 2)
                + String(repeating: str[str.index(str.startIndex, offsetBy: 2)], count: 2)
        } else if !str.count.isMultiple(of: 2) || str.count > 8 {
            return nil
        }
        let scanner = Scanner(string: str)
        var color: UInt64 = 0
        scanner.scanHexInt64(&color)
        if str.count == 2 {
            let gray = Double(Int(color) & 0xFF) / 255
            self.init(.sRGB, red: gray, green: gray, blue: gray, opacity: 1)
        } else if str.count == 4 {
            let gray = Double(Int(color >> 8) & 0x00FF) / 255
            let alpha = Double(Int(color) & 0x00FF) / 255
            self.init(.sRGB, red: gray, green: gray, blue: gray, opacity: alpha)
        } else if str.count == 6 {
            let red = Double(Int(color >> 16) & 0x0000FF) / 255
            let green = Double(Int(color >> 8) & 0x0000FF) / 255
            let blue = Double(Int(color) & 0x0000FF) / 255
            self.init(.sRGB, red: red, green: green, blue: blue, opacity: 1)
        } else if str.count == 8 {
            let red = Double(Int(color >> 24) & 0x000000FF) / 255
            let green = Double(Int(color >> 16) & 0x000000FF) / 255
            let blue = Double(Int(color >> 8) & 0x000000FF) / 255
            let alpha = Double(Int(color) & 0x000000FF) / 255
            self.init(.sRGB, red: red, green: green, blue: blue, opacity: alpha)
        } else {
            return nil
        }
    }
}

如果你觉得上面这个方法太复杂,再给出一个简单的代码

只需要以Int方式传入16进制的颜色值即可,看使用实例

extension Color {
    init(hex: Int, opacity: Double = 1.0) {
        let red = Double((hex & 0xff0000) >> 16) / 255.0
        let green = Double((hex & 0xff00) >> 8) / 255.0
        let blue = Double((hex & 0xff) >> 0) / 255.0
        self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity)
    }
}

只需要以Int方式传入16进制的颜色值,即去掉 # 开头,添加 0x 开头,直接看使用示例

// #ff4747 -> 0xff4747
Color(hex: 0xff4747)
Color(hex: 0xff4747, opacity: 0.5)

  本文自 https://www.codeun.com 发布,相应代码均自主编写并严格审阅和测试,完整代码中包含丰富的学习笔记和使用方式、实用技巧。
  · 如若转载,请注明出处:https://www.codeun.com/archives/1082.html ·

(0)
上一篇 2023-04-24
下一篇 2023-05-15

发表回复

登录后才能评论