Skip to content

Instantly share code, notes, and snippets.

@raaowx
Last active March 2, 2023 17:17
Show Gist options
  • Save raaowx/8bf6e79c130731278e41cffff26a8467 to your computer and use it in GitHub Desktop.
Save raaowx/8bf6e79c130731278e41cffff26a8467 to your computer and use it in GitHub Desktop.
Swift - UIKit - UIColor
import UIKit
extension UIColor {
/// An object that stores color data and sometimes opacity.
///
/// This failable convenience init allows to create an `UIColor` from a hexadecimal `String`. Supported format examples:
/// - #FFF = UIExtendedSRGBColorSpace 1 1 1 1
/// - #FFF0 = UIExtendedSRGBColorSpace 1 1 1 0
/// - #FFFFFF = UIExtendedSRGBColorSpace 1 1 1 1
/// - #FFFFFF0 = UIExtendedSRGBColorSpace 1 1 1 0
/// - #FFFFFF00 = UIExtendedSRGBColorSpace 1 1 1 0
convenience init?(hex: String) {
if !hex.hasPrefix("#") || ![4, 5, 7, 9].contains(hex.count) {
return nil
}
let start = hex.index(hex.startIndex, offsetBy: 1)
var color = String(hex[start...])
if [3, 4].contains(color.count) {
for i in 0..<color.count * 2 {
if i % 2 == 0 {
continue
}
let index = color.index(color.startIndex, offsetBy: i - 1)
let nextIndex = color.index(after: index)
let char = color[index]
color.insert(char, at: nextIndex)
}
}
if color.count == 6 {
color.append("FF")
}
var number: UInt64 = 0
let scanner = Scanner(string: color)
if !scanner.scanHexInt64(&number) {
return nil
}
let red, green, blue, alpha: CGFloat
red = CGFloat((number & 0xff000000) >> 24) / 255
green = CGFloat((number & 0x00ff0000) >> 16) / 255
blue = CGFloat((number & 0x0000ff00) >> 8) / 255
alpha = CGFloat(number & 0x000000ff) / 255
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment