iOS 色值 轉換 乾貨

來源:互聯網
上載者:User

標籤:

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)

 

iOS 色值 轉換 乾貨

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.