//
// HexColor.swift
// HexColor
//
// Created by Tuomas Artman on 1.9.2014.
// Copyright (c) 2014 Tuomas Artman. All rights reserved.
// import Foundation
import UIKit extension UIColor { /// Initializes UIColor with an integer.
///
/// - parameter value: The integer value of the color. E.g. 0xFF0000 is red, 0x0000FF is blue.
public convenience init(_ value: Int) {
let components = getColorComponents(value)
self.init(red: components.red, green: components.green, blue: components.blue, alpha: 1.0)
} /// Initializes UIColor with an integer and alpha value.
///
/// - parameter value: The integer value of the color. E.g. 0xFF0000 is red, 0x0000FF is blue.
/// - parameter alpha: The alpha value.
public convenience init(_ value: Int, alpha: CGFloat) {
let components = getColorComponents(value)
self.init(red: components.red, green: components.green, blue: components.blue, alpha: alpha)
} /// Creates a new color with the given alpha value
///
/// For example, (0xFF0000).alpha(0.5) defines a red color with 50% opacity.
///
/// - returns: A UIColor representation of the Int with the given alpha value
public func alpha(value:CGFloat) -> UIKit.UIColor {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0 self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) return UIKit.UIColor(red: red, green: green, blue: blue, alpha: value)
} /// Mixes the color with another color
///
/// - parameter color: The color to mix with
/// - parameter amount: The amount (0-1) to mix the new color in.
/// - returns: A new UIColor instance representing the resulting color
public func mixWithColor(color:UIColor, amount:Float) -> UIColor {
var comp1: [CGFloat] = Array(count: 4, repeatedValue: 0);
self.getRed(&comp1[0], green: &comp1[1], blue: &comp1[2], alpha: &comp1[3]) var comp2: [CGFloat] = Array(count: 4, repeatedValue: 0);
color.getRed(&comp2[0], green: &comp2[1], blue: &comp2[2], alpha: &comp2[3]) var comp: [CGFloat] = Array(count: 4, repeatedValue: 0);
for i in 0...3 {
comp[i] = comp1[i] + (comp2[i] - comp1[i]) * CGFloat(amount)
} return UIColor(red:comp[0], green: comp[1], blue: comp[2], alpha: comp[3])
}
} private func getColorComponents(value: Int) -> (red: CGFloat, green: CGFloat, blue: CGFloat) {
let r = CGFloat(value >> 16 & 0xFF) / 255.0
let g = CGFloat(value >> 8 & 0xFF) / 255.0
let b = CGFloat(value & 0xFF) / 255.0 return (r, g, b)
}

github地址:https://github.com/artman/HexColor

HexColor的更多相关文章

  1. 【blade利刃出鞘】一起进入移动端webapp开发吧

    前言 在移动浪潮袭来的时候,小钗有幸进入框架组做webapp框架开发,过程中遇到了移动端的各种坑,也产生了各种激情,就我们公司的发展历程来说 第一阶段:使用传统方式开发移动站点,少量引入HTML5元素 ...

  2. UITabBarController 基本定制

    UITabBarController 定制 特点 用法 1.准备好你的tabBar图片及其他图片(哈哈哈!!!!),我的图片都放在了Assets.xcassets中. 2.导入本工程中的Categro ...

  3. IOS开发基础知识--碎片49

    1:iOS项目配置文件info.plist文件解析 Localization native development region本地化 Executable file可执行文件路径 Bundle id ...

  4. jquery颜色选择器

    本站下载 第二种:纯JAVASCRIPT: <html> <head> <meta http-equiv="Content-Type" content ...

  5. IOS开发基础知识--碎片37

    1:iOS 使用NJKWebViewProgress做webview进度条 引入头文件: #import "NJKWebViewProgressView.h" #import &q ...

  6. jQuery Colorpicker Spectrum api 中文 文档 属性 事件 方法

    jQuery Colorpicker Spectrum 所需的CSS和JavaScript文件: <script src='spectrum.js'></script> < ...

  7. iOS根据16进制的色号来设置颜色,适合封装工具类

    iOS中有时候UI给的一个色号就像 #54e1b7 这个,而我们一般设置颜色都是根据RBG来设置的,所以这里需要把这个16进制的色号转为RGB值,这里我们就使用一下的方法来调用设置颜色. + (UIC ...

  8. iOS 多快好省的宏定义

    http://my.oschina.net/yongbin45/blog/150149 // 字符串:#ifndef nilToEmpty#define nilToEmpty(object) (obj ...

  9. TweenMax参数说明

    TweenMax 建立在 TweenLite 和 TweenFilterLite 基础之上,因此,又揉合了这二者的功能,使得功能更加的齐备,但是如果说易用性,觉得还是 TweenLite 来得方便一些 ...

随机推荐

  1. ECMAScript 6新特性介绍

    箭头函数 箭头函数使用=>语法来简化函数.在语句结构上和C#.Java 8 和 CoffeeScript相似,支持表达式和函数体. . =>`操作符左边为输入的參数.而右边则是进行的操作以 ...

  2. [SASS] Make a responsive arrow box

    Check the page:http://www.cssarrowplease.com/ In HTML: {{type}} is tow way binding in Angular, three ...

  3. iOS8 Core Image In Swift:人脸检测以及马赛克

    iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...

  4. vue-resource插件使用

    本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...

  5. JavaBean和EJB的区别

    首先,EJB是指运行在EJB容器中的JavaBean.Tomcat是Web容器的参考实现.一个完整的JavaEE服务器应该包括Web容器和EJB容器.其次,Web容器中无法运行EJB,同时所有的Jav ...

  6. Asp.net数据库缓存依赖

    Asp.net数据库缓存依赖 更多的时候,我们的服务器性能损耗还是在查询数据库的时候,所以对数据库的缓存还是显得特别重要,上面几种方式都可以实现部分数据缓存功能.但问题是我们的数据有时候是在变化的,这 ...

  7. AsyncTask使用注意事项

    AsyncTask是android自带的一个异步处理线程 它带了很多参数 都很方便使用 但是有一些注意事项 1: 官网说明: AsyncTasks should ideally be used for ...

  8. NSString / NSMutableString 字符串处理,常用代码 (实例)

    http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...

  9. iOS-OC-基础-NSArray常用方法

    NSArray常用方法和属性 // ——————————————————————数组常用方法—————————————————————— // 1.计算数组元素的个数: count NSArray * ...

  10. (原)Windows下编译指纹识别的Rel_4.1.0库

    网址:http://www.cnblogs.com/darkknightzh/p/4867372.html.未经允许,严禁转载. 没怎么用过linux,对于MSYS和MinGW也基本没用过,因而编译R ...