zoukankan      html  css  js  c++  java
  • iOS 色值 转换 干货

    1、 整数RGB设置颜色

    convenience init(red: Int, green: Int, blue: Int , alp: CGFloat = 1.0 ) {  
        assert(red >= 0 && red <= 255, "Invalid red component")       
        assert(green >= 0 && green <= 255, "Invalid green component")       
        assert(blue >= 0 && blue <= 255, "Invalid blue component")               
        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alp)    
    }

    2、16进制设置颜色

    convenience init(hexInt: Int) {        
        self.init(red:(hexInt >> 16) & 0xff, green:(hexInt >> 8) & 0xff, blue:hexInt & 0xff)    
    }       

    3、字符串设置颜色

    convenience init(hexString: String) { 
        var hexStr = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString               
        if (hexStr.hasPrefix("#")) {            
            hexStr = hexStr.substringFromIndex(hexStr.startIndex.advancedBy(1))        
        }           
        var hexInt: UInt32 = 0        
        NSScanner(string: hexStr).scanHexInt(&hexInt)               
        self.init(red: Int((hexInt >> 16) & 0xff), green: Int((hexInt >> 8) & 0xff), blue: Int(hexInt & 0xff))    }
    }

    整个封装:

    extension UIColor {       
        // RGB整数设置颜色
        convenience init(red: Int, green: Int, blue: Int , alp: CGFloat = 1.0 ) {  
            assert(red >= 0 && red <= 255, "Invalid red component")       
            assert(green >= 0 && green <= 255, "Invalid green component")       
            assert(blue >= 0 && blue <= 255, "Invalid blue component")               
            self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alp)    
            
        }
        // 16进制设置颜色
        convenience init(hexInt: Int) {        
           self.init(red:(hexInt >> 16) & 0xff, green:(hexInt >> 8) & 0xff, blue:hexInt & 0xff)    
        }       
           
           // 字符串设置颜色
        convenience init(hexString: String) { 
            var hexStr = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString               
            if (hexStr.hasPrefix("#")) {            
                hexStr = hexStr.substringFromIndex(hexStr.startIndex.advancedBy(1))        
            }           
            var hexInt: UInt32 = 0        
            NSScanner(string: hexStr).scanHexInt(&hexInt)               
            self.init(red: Int((hexInt >> 16) & 0xff), green: Int((hexInt >> 8) & 0xff), blue: Int(hexInt & 0xff))    
        }
    }

    应用:

    UIColor(red: 253, green: 77, blue: 79)
    UIColor(red: 253, green: 77, blue: 79, alp: 0.9)
    
    UIColor(hexString: "#A94442")
    UIColor(hexString: "#DCA7A7")
    
    UIColor(hexInt: 0xdcdcdc)
    UIColor(hexInt: 0xdcdaaa)
  • 相关阅读:
    【USACO】又买饲料 单调队列dp
    数论:px+py 不能表示的最大数为pq-p-q的证明
    codeforces round #419 E. Karen and Supermarket
    [USACO4.1]麦香牛块Beef McNuggets
    【USACO】 录制唱片
    【USACO】 洞穴奶牛
    【USACO】奶牛抗议 树状数组+dp
    【USACO】 奶牛会展
    【LSGDOJ 2015】数页码
    阿里云合作伙伴峰会SaaS加速器专场 | 商业加持,迈进亿元俱乐部
  • 原文地址:https://www.cnblogs.com/jfckliving/p/5836717.html
Copyright © 2011-2022 走看看