博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 色值 转换 干货
阅读量:6568 次
发布时间:2019-06-24

本文共 2645 字,大约阅读时间需要 8 分钟。

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)

 

转载于:https://www.cnblogs.com/jfckliving/p/5836717.html

你可能感兴趣的文章
/etc/security/limits.conf
查看>>
js 框架
查看>>
android 实现ListView中添加RaidoButton单选
查看>>
Oracle数据库:启动操作
查看>>
linux下的防火墙
查看>>
SNAT与DNAT
查看>>
Linux 修改密码“ Authentication token manipulation err”
查看>>
openstack
查看>>
Lync Server 2013 安装体验(一)
查看>>
EBB-24、DNS2
查看>>
css3做的nav
查看>>
汇编笔记
查看>>
点击qq、点击邮箱01
查看>>
时间处理总结(三)javascript与WCF
查看>>
Ubantu下安装jdk 教程
查看>>
ActiveMQ入门实例
查看>>
linux安装至少有哪两个分区,各自作用是什么?
查看>>
swoole 安装和简单实用
查看>>
文件系统 第八次迭代 VFS相关说明
查看>>
速读《构建之法:现代软件工程》提问
查看>>