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

本文共 2775 字,大约阅读时间需要 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

转载于:https://www.cnblogs.com/Free-Thinker/p/5106110.html

你可能感兴趣的文章
Sublime Text3 个人使用心得
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>
(转载)rabbitmq与springboot的安装与集成
查看>>
C2. Power Transmission (Hard Edition)(线段相交)
查看>>
STM32F0使用LL库实现SHT70通讯
查看>>
Atitit. Xss 漏洞的原理and应用xss木马
查看>>
MySQL源码 数据结构array
查看>>
(文件过多时)删除目录下全部文件
查看>>
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>