//
// 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. VC调试笔记

    1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...

  2. OpenGL——点的绘制(使用OpenGL来绘制可旋转坐标系的螺旋线)

    package com.example.opengl1; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio. ...

  3. Git服务器 gitweb与gitLab的区别

    昨天我们已经把Git服务器搭建完成了,工程的上传与下载都可以了,不过有些人不喜欢使用git命令进行操作.所以我们就搭建一个可视化操作的环境!配置gitweb和gitlab两种访问方式! 一,配置git ...

  4. java中的二进制

    (1)按位与运算 & 1 & 1 = 1, 0 & 1 = 0 51 & 5  即 0011  0011 & 0000  0101 =0000 0001 = 1 ...

  5. Spring项目的建立-移植流程(非入门教程)

    Creat by Zhou yong in 2016/4/15/19:00 jar包 java 1.7 tomcat的两个jar包 2个文件上传的jar包 json支持的jar包 hibernate- ...

  6. axure 8.0 动态特效库分享

    认准品牌 六脉神掌 尽量给每一个交互加上效果,尽量模拟真实的交互 无图无真相,我们先看效果图 1 这是还原Android Material Design风格的一个底部菜单效果 2 这个是模拟欢迎页面 ...

  7. Comparator和Comparable在排序中的应用

    http://blog.csdn.net/iisgirl/article/details/7269833

  8. MYSQL数据去重与外表填充

    经常要对数据库中的数据进行去重,有时还需要使用外部表填冲数据,本文档记录数据去重与外表填充数据. date:2016/8/17 author:wangxl 1 需求 对user_info1表去重,并添 ...

  9. Qt Library 链接库

    官方教程:http://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application http://qimo601.i ...

  10. checkbox 实现单选效果(html)

    note:在html <input> 标签类中的checkbox实现单选效果. 在最近的开发项目中,客户要求使用小方格子实现“单选”功能,显然圆点的radio被out了,只能选择chckb ...