博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HexColor
阅读量:7004 次
发布时间:2019-06-27

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

////  HexColor.swift//  HexColor////  Created by Tuomas Artman on 1.9.2014.//  Copyright (c) 2014 Tuomas Artman. All rights reserved.//import Foundationimport UIKitextension 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

转载地址:http://ntutl.baihongyu.com/

你可能感兴趣的文章
JavaScript基础知识总结(二)
查看>>
-1-3 java集合框架基础 java集合体系结构 Collection 常用java集合框架 如何选择集合 迭代器 泛型 通配符概念 Properties 集合 迭代器...
查看>>
C# 各种导入 Excel 文件的数据的方法总结
查看>>
事务 , 视图 , 和索引
查看>>
杭电ACM--2502月之数
查看>>
File 操作
查看>>
PKCS
查看>>
zabbix 乱码问题
查看>>
jquery中attr和prop的区别
查看>>
OpenLayers2.13.1知识整理
查看>>
动态轮播图
查看>>
P1122 最大子树和
查看>>
css_01之基础属性、选择器
查看>>
LOJ#2249 购票
查看>>
xp局域网设置和xp无法访问局域网的解决方案
查看>>
【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?...
查看>>
php文件下载方法收藏(附js下载技巧)
查看>>
docker简单介绍---部署私有docker仓库Registry
查看>>
Android基础教程——在TextView中显示Html 自定义标签,获取标签属性
查看>>
setTimeout与setInterval
查看>>