123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //
- // UIColor+Extension.swift
- // o2app
- //
- // Created by 刘振兴 on 2017/8/18.
- // Copyright © 2017年 zone. All rights reserved.
- //
- import Foundation
- import UIKit
- extension UIColor {
-
- /// EZSE: init method with RGB values from 0 to 255, instead of 0 to 1. With alpha(default:1)
- public convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1) {
- self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
- }
-
-
- convenience init(red: Int, green: Int, blue: Int) {
- assert(red >= 0 && red <= 255, "Invalid red component")
- assert(green >= 0 && green <= 255, "Invalid green component")
- assert(blue >= 0 && blue <= 255, "Invalid blue component")
-
- self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
- }
-
-
-
- static var random: UIColor {
- let maxValue: UInt32 = 24
- return UIColor(red: CGFloat(arc4random() % maxValue) / CGFloat(maxValue),
- green: CGFloat(arc4random() % maxValue) / CGFloat(maxValue) ,
- blue: CGFloat(arc4random() % maxValue) / CGFloat(maxValue) ,
- alpha: 1)
- }
-
-
- convenience init(netHex:Int) {
- self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
- }
-
- convenience init(hex string: String) {
- var hex = string.hasPrefix("#")
- ? String(string.dropFirst())
- : string
- guard hex.count == 3 || hex.count == 6
- else {
- self.init(white: 1.0, alpha: 0.0)
- return
- }
- if hex.count == 3 {
- for (index, char) in hex.enumerated() {
- hex.insert(char, at: hex.index(hex.startIndex, offsetBy: index * 2))
- }
- }
-
- guard let intCode = Int(hex, radix: 16) else {
- self.init(white: 1.0, alpha: 0.0)
- return
- }
-
- self.init(
- red: CGFloat((intCode >> 16) & 0xFF) / 255.0,
- green: CGFloat((intCode >> 8) & 0xFF) / 255.0,
- blue: CGFloat((intCode) & 0xFF) / 255.0, alpha: 1.0)
- }
-
- public func alpha(_ value: CGFloat) -> UIColor {
- return withAlphaComponent(value)
- }
-
- class func rgb(_ r: Int, _ g: Int, _ b: Int, _ alpha: CGFloat) -> UIColor {
- return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha / 1.0)
- }
-
- class func hexRGB(_ rgbValue: Int, _ alpha: CGFloat) -> UIColor {
- return UIColor.init(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((CGFloat)(rgbValue & 0xFF))/255.0, alpha: alpha / 1.0)
- }
-
- /**
- * 16进制 转 RGBA
- */
- class func UIColorFromRGBA(rgb:Int, alpha:CGFloat) ->UIColor {
-
- return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
- green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
- blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
- alpha: alpha)
- }
-
- /**
- * 16进制 转 RGB
- */
- class func UIColorFromRGB(rgb:Int) ->UIColor {
-
- return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
- green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
- blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
- alpha: 1.0)
- }
-
- // MARK: - RGBA
- class func RGBA(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
- return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
- }
-
- // MARK: - HexInt Covert UIColor
- class func hexInt(_ hexValue:Int) -> UIColor {
- return hexInt(hexValue, 1.0)
- }
-
- // MARK: - HexInt alpha Covert UIColor
- class func hextIntWithAlpha(_ hexValue:Int,_ alpha:CGFloat) -> UIColor {
- return hexInt(hexValue,alpha)
- }
-
- // MARK: - 私有方法实现
- fileprivate class func hexInt(_ hexValue:Int,_ alpha:CGFloat) -> UIColor {
- return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0,
- green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0,
- blue: ((CGFloat)(hexValue & 0xFF)) / 255.0,
- alpha: alpha)
- }
- }
|