| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // UIColor+Extension.swift
- // AiKeyboard
- //
- // Created by Destiny on 2025/4/23.
- //
- import UIKit
- // MARK: - 二、使用方法设置颜色
- public extension UIColor {
-
- // MARK: 2.1、根据RGBA的颜色(方法)
- /// 根据RGBA的颜色(方法)
- /// - Parameters:
- /// - r: red 颜色值
- /// - g: green颜色值
- /// - b: blue颜色值
- /// - alpha: 透明度
- /// - Returns: 返回 UIColor
- static func color(r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
- return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha)
- }
-
- // MARK: 2.2、十六进制字符串设置颜色(方法)
- static func hexStringColor(hexString: String, alpha: CGFloat = 1.0) -> UIColor {
- let newColor = hexStringToColorRGB(hexString: hexString)
- guard let r = newColor.r, let g = newColor.g, let b = newColor.b else {
- assert(false, "颜色值有误")
- return .white
- }
- return color(r: r, g: g, b: b, alpha: alpha)
- }
-
- // MARK: 2.3、十六进制 Int 颜色的使用(方法)
- /// 十六进制颜色的使用
- /// - Parameters:
- /// - color: 16进制 Int 颜色 0x999999
- /// - alpha: 透明度
- /// - Returns: 返回一个 UIColor
- static func hexIntColor(hexInt: Int, alpha: CGFloat = 1) -> UIColor {
- let redComponet: Float = Float(hexInt >> 16)
- let greenComponent: Float = Float((hexInt & 0xFF00) >> 8)
- let blueComponent: Float = Float(hexInt & 0xFF)
- return UIColor(red: CGFloat(redComponet / 255.0), green: CGFloat(greenComponent / 255.0), blue: CGFloat(blueComponent / 255.0), alpha: alpha)
- }
- }
- private extension UIColor {
-
- // MARK: 3.1、根据 十六进制字符串 颜色获取 RGB,如:#3CB371 或者 ##3CB371 -> 60,179,113
- /// 根据 十六进制字符串 颜色获取 RGB
- /// - Parameter hexString: 十六进制颜色的字符串,如:#3CB371 或者 ##3CB371 -> 60,179,113
- /// - Returns: 返回 RGB
- static func hexStringToColorRGB(hexString: String) -> (r: CGFloat?, g: CGFloat?, b: CGFloat?) {
- // 1、判断字符串的长度是否符合
- guard hexString.count >= 6 else {
- return (nil, nil, nil)
- }
- // 2、将字符串转成大写
- var tempHex = hexString.uppercased()
- // 检查字符串是否拥有特定前缀
- // hasPrefix(prefix: String)
- // 检查字符串是否拥有特定后缀。
- // hasSuffix(suffix: String)
- // 3、判断开头: 0x/#/##
- if tempHex.hasPrefix("0x") || tempHex.hasPrefix("0X") || tempHex.hasPrefix("##") {
- tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 2)..<tempHex.endIndex])
- }
- if tempHex.hasPrefix("#") {
- tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 1)..<tempHex.endIndex])
- }
- // 4、分别取出 RGB
- // FF --> 255
- var range = NSRange(location: 0, length: 2)
- let rHex = (tempHex as NSString).substring(with: range)
- range.location = 2
- let gHex = (tempHex as NSString).substring(with: range)
- range.location = 4
- let bHex = (tempHex as NSString).substring(with: range)
- // 5、将十六进制转成 255 的数字
- var r: UInt64 = 0, g: UInt64 = 0, b: UInt64 = 0
- Scanner(string: rHex).scanHexInt64(&r)
- Scanner(string: gHex).scanHexInt64(&g)
- Scanner(string: bHex).scanHexInt64(&b)
- return (r: CGFloat(r), g: CGFloat(g), b: CGFloat(b))
- }
-
- // MARK: 3.2、根据 十六进制值 颜色获取 RGB, 如:0x3CB371 -> 60,179,113
- /// 根据 十六进制值 颜色获取 RGB, 如:0x3CB371 -> 60,179,113
- /// - Parameter hexInt: 十六进制值,如:0x3CB37
- /// - Returns: 返回 RGB
- static func hexIntToColorRGB(hexInt: Int) -> (r: CGFloat, g: CGFloat, b: CGFloat) {
- let red: CGFloat = CGFloat(hexInt >> 16)
- let green: CGFloat = CGFloat((hexInt & 0xFF00) >> 8)
- let blue: CGFloat = CGFloat(hexInt & 0xFF)
- return (red, green, blue)
- }
- }
|