-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTColor.swift
More file actions
129 lines (120 loc) · 5.39 KB
/
STColor.swift
File metadata and controls
129 lines (120 loc) · 5.39 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// STColor.swift
// STBaseProject
//
// Created by 寒江孤影 on 2018/10/9.
//
import UIKit
import CoreGraphics
public extension UIColor {
/// 从 Assets 中的颜色集创建颜色(支持暗黑模式)
/// 注意:颜色和透明度都从 Assets 中读取,如需调整透明度请使用 withAlphaComponent 方法
/// - Parameter colorSet: 颜色集名称
/// - Returns: UIColor 对象,如果找不到颜色集则返回 clear
static func color(named colorSet: String) -> UIColor {
guard !colorSet.isEmpty else {
return UIColor.clear
}
return UIColor(named: colorSet) ?? UIColor.clear
}
/// 从十六进制字符串创建颜色(sRGB 色彩空间)
/// - Parameter hexString: 十六进制字符串,支持 #、0x 前缀,支持 3位(#FFF)、6位(#FFFFFF)、8位(#FFFFFFFF)格式
/// 如果使用 8 位格式,alpha 值将从 hexString 中读取;否则 alpha 默认为 1.0
/// - Returns: UIColor 对象
static func color(hex hexString: String) -> UIColor {
return color(hexString: hexString, alphaOverride: nil)
}
/// 从十六进制字符串创建颜色(sRGB 色彩空间,可指定透明度)
/// - Parameters:
/// - hexString: 十六进制字符串,支持 #、0x 前缀,支持 3位(#FFF)、6位(#FFFFFF)、8位(#FFFFFFFF)格式
/// - alpha: 透明度 (0.0-1.0),如果指定则覆盖 hexString 中的 alpha 值
/// - Returns: UIColor 对象
static func color(hex hexString: String, alpha: CGFloat) -> UIColor {
return color(hexString: hexString, alphaOverride: alpha)
}
private static func color(hexString: String, alphaOverride alpha: CGFloat?) -> UIColor {
guard !hexString.isEmpty else {
return UIColor.clear
}
var cString = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if cString.hasPrefix("#") {
cString = String(cString.dropFirst())
} else if cString.hasPrefix("0X") {
cString = String(cString.dropFirst(2))
}
// 支持 3 位简写格式(如 FFF -> FFFFFF)
if cString.count == 3 {
cString = cString.map { "\($0)\($0)" }.joined()
}
var rgbValue: UInt64 = 0
guard Scanner(string: cString).scanHexInt64(&rgbValue) else {
return UIColor.clear
}
var finalAlpha: CGFloat = 1.0
// 如果用户明确指定了 alpha,使用用户指定的值;否则从 hexString 中读取
if let userAlpha = alpha {
finalAlpha = userAlpha
// 如果 hexString 是 8 位格式但用户指定了 alpha,需要清除 alpha 位
if cString.count == 8 {
rgbValue = rgbValue & 0x00FFFFFF
}
} else if cString.count == 8 {
// 8 位格式(RRGGBBAA),alpha 从 hexString 中读取
finalAlpha = CGFloat((rgbValue & 0xFF000000) >> 24) / 255.0
rgbValue = rgbValue & 0x00FFFFFF
}
if cString.count != 6 && cString.count != 8 {
return UIColor.clear
}
// 显式使用 sRGB 色彩空间,确保与设计图一致
let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(rgbValue & 0x0000FF) / 255.0
return srgbColor(red: red, green: green, blue: blue, alpha: finalAlpha)
}
/// 从 RGB 值创建颜色(sRGB 色彩空间,确保与设计图一致)
/// - Parameters:
/// - red: 红色值 (0-255)
/// - green: 绿色值 (0-255)
/// - blue: 蓝色值 (0-255)
/// - alpha: 透明度 (0.0-1.0),默认 1.0
/// - Returns: UIColor 对象
static func color(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) -> UIColor {
return srgbColor(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: alpha
)
}
/// 从 HSB 值创建颜色
/// - Parameters:
/// - hue: 色相 (0.0-1.0)
/// - saturation: 饱和度 (0.0-1.0)
/// - brightness: 亮度 (0.0-1.0)
/// - alpha: 透明度 (0.0-1.0),默认 1.0
/// - Returns: UIColor 对象
static func color(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
}
}
private extension UIColor {
/// 在 sRGB 色彩空间中创建颜色
/// 此方法显式使用 sRGB 色彩空间,避免在支持 Display P3 的设备上出现颜色偏差
/// - Parameters:
/// - red: 红色值 (0.0-1.0)
/// - green: 绿色值 (0.0-1.0)
/// - blue: 蓝色值 (0.0-1.0)
/// - alpha: 透明度 (0.0-1.0)
/// - Returns: UIColor 对象
static func srgbColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor {
guard let sRGBColorSpace = CGColorSpace(name: CGColorSpace.sRGB) else {
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
let components = [red, green, blue, alpha]
guard let cgColor = CGColor(colorSpace: sRGBColorSpace, components: components) else {
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
return UIColor(cgColor: cgColor)
}
}