无故事王国
2023-10-25 158f3707711ad4be78a55dc73a98aa1c9acda0dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
//  Colors+Exception.swift
//  BrokerDriver
//
//  Created by 无故事王国 on 2023/4/24.
//
 
import UIKit
 
public extension UIColor {
    /// 通过指定的不透明度和 RGB 分量值, 返回一个颜色对象
    ///
    /// - Parameters:
    ///   - red: 红色分量的值 (0 ~ 255)
    ///   - green: 绿色分量的值 (0 ~ 255)
    ///   - blue: 蓝色分量的值 (0 ~ 255)
    ///   - alpha: 不透明度的值 (0 ~ 1)
    /// - Returns: 颜色对象
    static func RGBA(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> UIColor {
        return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
    }
 
    /// 通过指定的不透明度 和 一个用16进制数字表示 RGB 分量值的字符串, 返回一个颜色对象
    ///
    /// - Parameters:
    ///   - hexString: 以"#"或"0x"开头, 后面跟随6位(或3位)16进制数字 表示RGB分量值的字符串
    ///   - alpha: 不透明度 (0 ~ 1)
    /// - Returns: 颜色对象
    static func color(_ hexString: String, _ alpha: CGFloat = 1.0) -> UIColor {
        var red: CGFloat = 0.0, green: CGFloat = 0.0, blue:  CGFloat = 0.0
        var cString = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
 
        guard cString.hasPrefix("0X") || cString.hasPrefix("#") else {
            return UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
        }
 
        if cString.hasPrefix("0X") {
            cString = String(cString[cString.index(cString.startIndex, offsetBy: 2)...])
        } else if cString.hasPrefix("#") {
            cString = String(cString[cString.index(cString.startIndex, offsetBy: 1)...])
        }
 
        let scanner = Scanner(string: cString)
        var hexValue: CUnsignedLongLong = 0
        guard scanner.scanHexInt64(&hexValue) else {
            return UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
        }
 
        switch (cString.count) {
        case 3:
            red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
            green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
            blue  = CGFloat(hexValue & 0x00F)              / 15.0
 
        case 6:
            red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
            green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
            blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
 
        default:
            red = 0.0; green = 0.0; blue = 1.0
        }
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
 
 
    /// 深色模式适配颜色
    /// - Parameters:
    ///   - light: 普通颜色
    ///   - dark: 深色模式颜色
    static func color(light: UIColor,dark: UIColor) -> UIColor {
        if #available(iOS 13.0, *) {
            let color = UIColor.init { (trait) -> UIColor in
                if trait.userInterfaceStyle == UIUserInterfaceStyle.dark{
                    return dark
                }else{
                    return light
                }
            }
            return color
        } else {
            return light
        }
    }
}