zoukankan      html  css  js  c++  java
  • HexColor

    //
    //  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

  • 相关阅读:
    在已经安装的nginx上,增加ssl模块
    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))
    ab和jmeter进行GET/POST压力测试的使用心得和比较
    linux历史命令查找快捷方式
    HDFS的dfs.replication不同验证
    Set replication in Hadoop
    RVM 安装 Ruby
    Fluentd初探 简介与安装
    Flunetd 用于统一日志记录层的开源数据收集器
    Custom partition assignment and migration kafka集群扩充迁移指定partition
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5106110.html
Copyright © 2011-2022 走看看