vds_ios/VDS/Extensions/UIColor.swift
Matt Bruce d61f60cc21 refactored more of the toggle
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-07-26 12:04:41 -05:00

148 lines
5.6 KiB
Swift

//
// UIColor.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import UIKit
extension UIColor {
//--------------------------------------------------
// MARK: - Functions
//--------------------------------------------------
/// Convenience to get a grayscale UIColor where the same value is used for red, green, and blue.
public class func grayscale(rgb: Int, alpha: CGFloat = 1.0) -> UIColor {
let grayscale = CGFloat(rgb) / 255.0
return UIColor(red: grayscale, green: grayscale, blue: grayscale, alpha: alpha)
}
/// Convenience to get a UIColor.
public class func color8Bits(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) -> UIColor {
return UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
}
/// Convenience to get a UIColor.
public class func color8Bits(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
return UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alpha)
}
/// Gets UIColor via an 8 digit hex string.
public class func getColorBy(hex: String) -> UIColor {
var hexint: UInt64 = 0
let scanner = Scanner(string: hex)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
scanner.scanHexInt64(&hexint)
return UIColor(red: (CGFloat((hexint & 0xFF0000) >> 16)) / 255,
green: (CGFloat((hexint & 0xFF00) >> 8)) / 255,
blue: (CGFloat(hexint & 0xFF)) / 255,
alpha: 1)
}
/// Gets UIColor via an 8 digit hex string. The last two being the alpha channel.
public class func getColorWithTransparencyBy(hex: String) -> UIColor {
var hexint: UInt64 = 0
let scanner = Scanner(string: hex)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
scanner.scanHexInt64(&hexint)
return UIColor(red: (CGFloat((hexint & 0xFF000000) >> 24)) / 255,
green: (CGFloat((hexint & 0xFF0000) >> 16)) / 255,
blue: (CGFloat((hexint & 0xFF00) >> 8)) / 255,
alpha: (CGFloat(hexint & 0xFF)) / 255)
}
public class func gradientColor(_ color: UIColor?) -> UIColor {
var h: CGFloat = 0
var s: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
if color?.getHue(&h, saturation: &s, brightness: &b, alpha: &a) ?? false {
return UIColor(hue: h, saturation: max(s - 0.17, 0.0), brightness: min(b - 0.03, 1.0), alpha: a)
}
return .white
}
/// - parameter color: The UIColor intended to retrieve its hex value.
public class func hexString(for color: UIColor) -> String? {
guard let components = color.cgColor.components else { return nil }
let numberOfComponents = color.cgColor.numberOfComponents
if numberOfComponents >= 3 {
// RGB color space
let r = Int(CGFloat(components[0]) * 255)
let g = Int(CGFloat(components[1]) * 255)
let b = Int(CGFloat(components[2]) * 255)
// If alpha of color is less than 1.0 then alpha hex is relevant.
if color.cgColor.numberOfComponents == 4 && components[3] < 1.0 {
let a = Int(CGFloat(components[3]) * 255)
return String(format: "%02X%02X%02X%02X", r, g, b, a)
}
return String(format: "%02X%02X%02X", r, g, b)
} else if numberOfComponents == 2 {
// Monochromatic color space
let value = Int(CGFloat(components[0]) * 255)
// If alpha of color is less than 1.0 then alpha hex is relevant.
if components[1] < 1.0 {
let alpha = Int(CGFloat(components[1]) * 255)
return String(format: "%02X%02X%02X%02X", value, value, value, alpha)
}
return String(format: "%02X%02X%02X", value, value, value)
}
return nil
}
/// - parameter color: The UIColor intended to retrieve its hex value.
public var hexString: String? {
guard let components = cgColor.components else { return nil }
let numberOfComponents = cgColor.numberOfComponents
if numberOfComponents >= 3 {
// RGB color space
let r = Int(CGFloat(components[0]) * 255)
let g = Int(CGFloat(components[1]) * 255)
let b = Int(CGFloat(components[2]) * 255)
// If alpha of color is less than 1.0 then alpha hex is relevant.
if cgColor.numberOfComponents == 4 && components[3] < 1.0 {
let a = Int(CGFloat(components[3]) * 255)
return String(format: "%02X%02X%02X%02X", r, g, b, a)
}
return String(format: "%02X%02X%02X", r, g, b)
} else if numberOfComponents == 2 {
// Monochromatic color space
let value = Int(CGFloat(components[0]) * 255)
// If alpha of color is less than 1.0 then alpha hex is relevant.
if components[1] < 1.0 {
let alpha = Int(CGFloat(components[1]) * 255)
return String(format: "%02X%02X%02X%02X", value, value, value, alpha)
}
return String(format: "%02X%02X%02X", value, value, value)
}
return nil
}
}