Skip to content

Instantly share code, notes, and snippets.

@cristhianleonli
Last active April 29, 2020 07:53
Show Gist options
  • Save cristhianleonli/914a8d33d87b4a2f9dbab1a720c564d5 to your computer and use it in GitHub Desktop.
Save cristhianleonli/914a8d33d87b4a2f9dbab1a720c564d5 to your computer and use it in GitHub Desktop.
import UIKit
public extension UIColor {
/**
Creates a color from red, green, blue and alpha
- Parameter red: number <0 to 255> indicating red
- Parameter green: number <0 to 255> indicating green
- Parameter blue: number <0 to 255> indicating blue
- Parameter alpha: number <0 to 1> indicating alpha, default 1
- Returns: a color formed with given values
*/
static func create(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ alpha: CGFloat = 1) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha)
}
/**
Creates a color from hex string
- Parameter hexString: hex value with/without #
- Returns: a color formed with given string
*/
static func create(hexString: String) -> UIColor {
let hex = hexString.trimmingCharacters(in: .whitespaces)
let scanner = Scanner(string: hex)
if hexString.hasPrefix("#") {
scanner.scanLocation = 1
}
var color: UInt32 = 0
scanner.scanHexInt32(&color)
let mask = 0x000000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return UIColor.init(red: red, green: green, blue: blue, alpha: 1)
}
/**
Creates a color from given value
- Parameter value: CGFloat from <0 to 255> indicating same value for color
- Returns: a color formed with given value in red, green and blue, alplha 1
*/
static func create(repeating value: CGFloat) -> UIColor {
return UIColor.init(red: value/255, green: value/255, blue: value/255, alpha: 1)
}
}
import UIKit
import Foundation
struct Colors {
struct Search {
static let lightGrey = UIColor.create(hexString: "E5E5E6")
static let darkGrey = UIColor.create(hexString: "A2A2A3")
static let black = UIColor.create(hexString: "140E12")
static let middleGrey = UIColor.create(hexString: "5F4E52")
static let darkRed = UIColor.create(hexString: "C63420")
static let lightRed = UIColor.create(hexString: "E81306")
static let lightPink = UIColor.create(hexString: "DAB1B4")
static let darkPink = UIColor.create(hexString: "C66893")
static let blue = UIColor.create(hexString: "0B1EF5")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment