美文网首页
十六进制字符串&RGB&UIColor转换~~Swift

十六进制字符串&RGB&UIColor转换~~Swift

作者: Sam_xing | 来源:发表于2021-12-11 23:48 被阅读0次

    文章仅供参考,如有更好的编写方法,欢迎指正!!!
    XFJColor.swift文件

    
    import UIKit
    
    class XFJColor: NSObject {
        
        /**
         * RGB转UIColor
         * R : 红
         * G : 绿
         * B : 蓝
         * A : 透明度(默认透明度为1)
         * Return : UIColor
         */
        func colorWithRGB(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1.0) -> UIColor {
            print(String(format: "R:%f", r), String(format: "G:%f", g), String(format: "B:%f", b), String(format: "A:%f", a))
            let color : UIColor = UIColor.init(red: (r / 255.0), green: (g / 255.0), blue: (b / 255.0), alpha: a)
            return color
        }
        
        /**
         * 十六进制字符串转换UIColor
         * Hex : 十六进制字符串
         * A : 透明度(默认透明度为1)
         * Return : UIColor
         */
        func colorWithHexString(hex: String, a:CGFloat = 1.0) -> UIColor {
            var cString : String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
            // String should be 6 or 8 characters
            if cString.count < 6 {
                return UIColor.clear
            }
            // 判断前缀
            if cString.hasPrefix("0x") {
                cString.removeFirst(2)
            }
            if cString.hasPrefix("0X") {
                cString.removeFirst(2)
            }
            if cString.hasPrefix("#") {
                cString.removeFirst(1)
            }
            if cString.count != 6 {
                return UIColor.clear
            }
            // 从六位数值中找到RGB对应的位数并转换
            let mask = 0x000000FF
            let r: Int
            let g: Int
            let b: Int
            
            let red: CGFloat
            let green: CGFloat
            let blue: CGFloat
            if #available(iOS 13.0, *) {
                // 系统版本高于13.0
                var color: UInt64 = 0
                let scanner = Scanner(string: cString)
                scanner.scanHexInt64(&color)
                
                
                 r = Int(color >> 16) & mask
                 g = Int(color >> 8) & mask
                 b = Int(color) & mask
                
                 red   = CGFloat(r)
                 green = CGFloat(g)
                 blue  = CGFloat(b)
                
            } else {
                // 系统版本低于13.0
                var color: UInt32 = 0
                let scanner = Scanner(string: cString)
                scanner.scanHexInt32(&color)
    
                 r = Int(color >> 16) & mask
                 g = Int(color >> 8) & mask
                 b = Int(color) & mask
                
                 red   = CGFloat(r)
                 green = CGFloat(g)
                 blue  = CGFloat(b)
                
            }
            
            return self.colorWithRGB(r: red, g: green, b: blue, a: a)
        }
        
        /**
         * UIColor转十六进制字符串
         * Color : UIColor
         * Perfix : 返回十六进制字符串格式(默认#)
         * Return : 十六进制字符串(如果UIColor透明度小于1,返回值含有透明度)
         */
        func hexFromUIColor(color: UIColor, perfix: String = "#") -> String {
            var red: CGFloat = 0
            var green: CGFloat = 0
            var blue: CGFloat = 0
            var alpha: CGFloat = 0
            
            let multiplier = CGFloat(255.999)
            
            // 判断当前UIColor是否存在并且映射
            // 如果不存在执行guard里面
            guard color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
                return ""
            }
    
            if alpha == 1.0 {
                return String(
                    format: "%@%02lX%02lX%02lX",
                    perfix,
                    Int(red * multiplier),
                    Int(green * multiplier),
                    Int(blue * multiplier)
                )
            } else {
                return String(
                    format: "%@%02lX%02lX%02lX%.2f",
                    perfix,
                    Int(red * multiplier),
                    Int(green * multiplier),
                    Int(blue * multiplier),
                    alpha
                    //Int(alpha * multiplier)
                )
            }
        }
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:十六进制字符串&RGB&UIColor转换~~Swift

          本文链接:https://www.haomeiwen.com/subject/fpglfrtx.html