//
// 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. [Hapi.js] Logging with good and good-console

    hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...

  2. 如何利用 Bootstrap 进行快速 Web 开发

    原文出处: IBM developerworks 了解如何使用 Bootstrap 快速开发网站和 Web 应用程序(包括移动友好型应用程序).Bootstrap 以 LESS 项目为基础,由 Twi ...

  3. html5 拖放---(二)转

    draggable是一个枚举属性,用于指定一个标签是否可以被拖拽.有以下四种取值: true 表示此元素可拖拽 false 表示此元素不可拖拽 auto 除img和带href的标签a标签表示可拖拽外, ...

  4. 企业qq代码,工作中用到的

    <div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...

  5. VisualStudio2013快捷键

    visual studio 2013 是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.VS 2013 中新增了很多提高 ...

  6. poi HSSFRichTextString 对cell中的每一段文字设置字体

    HSSFRichTextString ts= new HSSFRichTextString(" 经审核,我司已同意其出库申请.请你部按规定将托管银行承兑汇票办理出库." + &qu ...

  7. TextView之二:常用属性

    参考自<疯狂android讲义>2.3节 //TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor ...

  8. CSS3动画之旋转魔方盒

    步骤: 1.大盒子里盛放六个子盒子代表立方体的6个面: 2.子盒子开启3d效果  transform-style:preserve-3d; 3.上下面沿X轴旋转90度,一个上移盒子一半高,一个下移盒子 ...

  9. 微信订阅号开发之token验证后,自动回复消息功能做好,发送消息没有返回

    相信很多人会跟我一样,token验证之后,发送消息给订阅号,没有消息返回. 以下,说一下我辛苦调试得到的解决办法: 首先,token验证: 自己写的token一直验证失败,找了好久,没有发现bug.实 ...

  10. mac Word 怎样放大缩小文档结构图文字大小

    在文档结构图的侧栏里按住control+option,然后滑动鼠标滚轮/双指上下滚动触摸板.