Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active December 5, 2017 12:23
Show Gist options
  • Save gotev/76df9006674762859626846cf171ff80 to your computer and use it in GitHub Desktop.
Save gotev/76df9006674762859626846cf171ff80 to your computer and use it in GitHub Desktop.
Color from Hex String in Swift 3
//
// UIColorFromRGB.swift
//
//
// Created by Alex Gotev on 21/11/16.
//
//
import Foundation
import UIKit
public extension UIColor {
public static func color(fromHexString: String, alpha:CGFloat? = 1.0) -> UIColor {
// Convert hex string to an integer
let hexint = Int(colorInteger(fromHexString: fromHexString))
let red = CGFloat((hexint & 0xff0000) >> 16) / 255.0
let green = CGFloat((hexint & 0xff00) >> 8) / 255.0
let blue = CGFloat((hexint & 0xff) >> 0) / 255.0
let alpha = alpha!
// Create color object, specifying alpha as well
let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
return color
}
private static func colorInteger(fromHexString: String) -> UInt32 {
var hexInt: UInt32 = 0
// Create scanner
let scanner: Scanner = Scanner(string: fromHexString)
// Tell scanner to skip the # character
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
// Scan hex value
scanner.scanHexInt32(&hexInt)
return hexInt
}
var redValue: CGFloat{
return cgColor.components! [0]
}
var greenValue: CGFloat{
return cgColor.components! [1]
}
var blueValue: CGFloat{
return cgColor.components! [2]
}
var alphaValue: CGFloat{
return cgColor.components! [3]
}
// credits to @andreaantonioni for this addition
var isWhiteText: Bool {
let red = self.redValue * 255
let green = self.greenValue * 255
let blue = self.blueValue * 255
// https://en.wikipedia.org/wiki/YIQ
// https://24ways.org/2010/calculating-color-contrast/
let yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000
return yiq < 192
}
}
@BarOdr
Copy link

BarOdr commented Dec 5, 2017

It crashes on some hex strings on 32 bit devices... "Not enough bits to represent a signed value"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment