//
// UIColorExtension.swift
// HEXColor
//
// Created by R0CKSTAR on 6/13/14.
// Copyright (c) 2014 P.D.Q. All rights reserved.
// import UIKit /**
MissingHashMarkAsPrefix: "Invalid RGB string, missing '#' as prefix"
UnableToScanHexValue: "Scan hex error"
MismatchedHexStringLength: "Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8"
*/
public enum UIColorInputError : ErrorType {
case MissingHashMarkAsPrefix,
UnableToScanHexValue,
MismatchedHexStringLength
} extension UIColor {
/**
The shorthand three-digit hexadecimal representation of color.
#RGB defines to the color #RRGGBB. - parameter hex3: Three-digit hexadecimal value.
- parameter alpha: 0.0 - 1.0. The default is 1.0.
*/
public convenience init(hex3: UInt16, alpha: CGFloat = 1) {
let divisor = CGFloat(15)
let red = CGFloat((hex3 & 0xF00) >> 8) / divisor
let green = CGFloat((hex3 & 0x0F0) >> 4) / divisor
let blue = CGFloat( hex3 & 0x00F ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
} /**
The shorthand four-digit hexadecimal representation of color with alpha.
#RGBA defines to the color #RRGGBBAA. - parameter hex4: Four-digit hexadecimal value.
*/
public convenience init(hex4: UInt16) {
let divisor = CGFloat(15)
let red = CGFloat((hex4 & 0xF000) >> 12) / divisor
let green = CGFloat((hex4 & 0x0F00) >> 8) / divisor
let blue = CGFloat((hex4 & 0x00F0) >> 4) / divisor
let alpha = CGFloat( hex4 & 0x000F ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
} /**
The six-digit hexadecimal representation of color of the form #RRGGBB. - parameter hex6: Six-digit hexadecimal value.
*/
public convenience init(hex6: UInt32, alpha: CGFloat = 1) {
let divisor = CGFloat(255)
let red = CGFloat((hex6 & 0xFF0000) >> 16) / divisor
let green = CGFloat((hex6 & 0x00FF00) >> 8) / divisor
let blue = CGFloat( hex6 & 0x0000FF ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
} /**
The six-digit hexadecimal representation of color with alpha of the form #RRGGBBAA. - parameter hex8: Eight-digit hexadecimal value.
*/
public convenience init(hex8: UInt32) {
let divisor = CGFloat(255)
let red = CGFloat((hex8 & 0xFF000000) >> 24) / divisor
let green = CGFloat((hex8 & 0x00FF0000) >> 16) / divisor
let blue = CGFloat((hex8 & 0x0000FF00) >> 8) / divisor
let alpha = CGFloat( hex8 & 0x000000FF ) / divisor
self.init(red: red, green: green, blue: blue, alpha: alpha)
} /**
The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB, throws error. - parameter rgba: String value.
*/
public convenience init(rgba_throws rgba: String) throws {
guard rgba.hasPrefix("#") else {
throw UIColorInputError.MissingHashMarkAsPrefix
} guard let hexString: String = rgba.substringFromIndex(rgba.startIndex.advancedBy(1)),
var hexValue: UInt32 = 0
where NSScanner(string: hexString).scanHexInt(&hexValue) else {
throw UIColorInputError.UnableToScanHexValue
} guard hexString.characters.count == 3
|| hexString.characters.count == 4
|| hexString.characters.count == 6
|| hexString.characters.count == 8 else {
throw UIColorInputError.MismatchedHexStringLength
} switch (hexString.characters.count) {
case 3:
self.init(hex3: UInt16(hexValue))
case 4:
self.init(hex4: UInt16(hexValue))
case 6:
self.init(hex6: hexValue)
default:
self.init(hex8: hexValue)
}
} /**
The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB, fails to default color. - parameter rgba: String value.
*/
public convenience init(rgba: String, defaultColor: UIColor = UIColor.clearColor()) {
guard let color = try? UIColor(rgba_throws: rgba) else {
self.init(CGColor: defaultColor.CGColor)
return
}
self.init(CGColor: color.CGColor)
} /**
Hex string of a UIColor instance. - parameter rgba: Whether the alpha should be included.
*/
public func hexString(includeAlpha: Bool) -> String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a) if (includeAlpha) {
return String(format: "#%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
} else {
return String(format: "#%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
}
} public override var description: String {
return self.hexString(true)
} public override var debugDescription: String {
return self.hexString(true)
}
}

项目地址:https://github.com/yeahdongcn/UIColor-Hex-Swift

UIColor-Hex-Swift的更多相关文章

  1. UIColor+Hex

    #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...

  2. Swift 4 Hex Color

    上点干货,写个extension, 可以通过hex值去设置Color,以及通过UIColor的color case 去得到hex值. extension UIColor {     var toHex ...

  3. [Swift]扩展UIColor:实现十六进制颜色字符串与UIColor之间的相互转换

    对[UIColor]进行扩展 import UIKit extension UIColor { // Hex String -> UIColor convenience init(hexStri ...

  4. Swift - UIColor16进制编码与RGB格式互相转换

    Swift UIColor 16进制编码转换RGB : 由于UI出图的时候,通常给的是16进制的编码颜色,我们在开发的时候需要将它转换为RGB格式,现在给出两种代码片段. 一.对UIColor进行扩展 ...

  5. 26.怎样在Swift中定义宏?

    Swift 中没有宏定义,苹果建议使用let 或者 get 属性来替代宏定义值.虽然没有#define,但我们仍然可以使用 #if 并配合编译的配置来完成条件编译.下面会列出Swift项目开发中的一些 ...

  6. [Swift通天遁地]五、高级扩展-(10)整形、浮点、数组、字典、字符串、点、颜色、图像类的实用扩展

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. iOS开发:十六进制颜色转UIColor

    Objective-C UIColor * __nullable UIColorFromHexValue(NSUInteger hexValue) { CGFloat red = (hexValue ...

  8. iOS 十六进制的颜色值转换为UIColor

    UIColor+Hex.h里面中 #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *) colorWithHexSt ...

  9. IOS中十六进制的颜色转换为UIColor

    IOS中十六进制的颜色转换为UIColor #pragma mark - 颜色转换 IOS中十六进制的颜色转换为UIColor + (UIColor *) colorWithHexString: (N ...

  10. Swift之基础知识

    Swift之基础知识 出于对Swift3.0的学习,写下这篇基本语法的笔记.希望能帮助记忆 -0- 这边提供Swift3.0中文教材,资源链接: https://pan.baidu.com/s/1c2 ...

随机推荐

  1. PHP你可能也会掉入的坑

    今天被人问: $var = 'test'; if (isset($var['somekey'])) { echo 'reach here!!!'; } 会不会输出'reach here!!!'? -- ...

  2. 获取被选择的radio的值

    function selectRadio() { var val = $('input:radio[name="address_select"]:checked').val(); ...

  3. MongoDB学习笔记06

    在shell中删除一个集合,执行db.test.drop()或者db.runCommand({"drop":"test"}),在MongoDB中命令其实是作为一 ...

  4. HTML5新增的拖放API---(一)

    HTML5新增了关于拖放的API,通过API可以让HTML页面的任意元素都变成可拖动的,通过使用拖放机制可以开发出更友好的人机交互的界面. 拖放操作可以分为两个动作:在某个元素上按下鼠标移动鼠标(没有 ...

  5. Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求

    开局声明:这是基于nohttp1.0.4-include-source.jar版本写的教程 由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去 ...

  6. poj1111 DFS

    J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit I ...

  7. codeforces 335A Banana(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud    Banana Piegirl is buying stickers for ...

  8. C++ 语言特性的性能分析

    转载:http://www.cnblogs.com/rollenholt/archive/2012/05/07/2487244.html      大多数开发人员通常都有这个观点,即汇编语言和 C 语 ...

  9. PHP基础示例:用PHP+Mysql编写简易新闻管理系统

    实现目标:使用php和mysql操作函数实现一个新闻信息的发布.浏览.修改和删除操作 实现步骤: 一.创建数据库和表 1.创建数据库和表:newsdb 2.创建表格:news 字段:新闻id,标题,关 ...

  10. Javascript经典实例 - 字符串

    1] 'this is a string'这是字符串直接量,new String('this is a string')这是字符串对象,字符串对象可以用字符串对象所带的属性和方法,直接量在“表面上”也 ...