Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noorulain17/e6b2844777680301d0ff4524e23ed92f to your computer and use it in GitHub Desktop.
Save noorulain17/e6b2844777680301d0ff4524e23ed92f to your computer and use it in GitHub Desktop.
FullCode For MediumBlog
import Foundation
import UIKit
// Usage Examples
let shadowColor = Color.shadow.value
let shadowColorWithAlpha = Color.shadow.withAlpha(0.5)
let customColorWithAlpha = Color.custom(hexString: "#123edd", alpha: 0.25).value
enum Color {
case theme
case border
case shadow
case darkBackground
case lightBackground
case intermidiateBackground
case darkText
case lightText
case intermidiateText
case affirmation
case negation
case custom(hexString: String, alpha: Double)
func withAlpha(_ alpha: Double) -> UIColor {
return self.value.withAlphaComponent(CGFloat(alpha))
}
}
extension Color {
var value: UIColor {
var instanceColor = UIColor.clear
switch self {
case .border:
instanceColor = UIColor(hexString: "#333333")
case .theme:
instanceColor = UIColor(hexString: "#ffcc00")
case .shadow:
instanceColor = UIColor(hexString: "#ccccc")
case .darkBackground:
instanceColor = UIColor(hexString: "#999966")
case .lightBackground:
instanceColor = UIColor(hexString: "#cccc66")
case .intermidiateBackground:
instanceColor = UIColor(hexString: "#cccc99")
case .darkText:
instanceColor = UIColor(hexString: "#333333")
case .intermidiateText:
instanceColor = UIColor(hexString: "#999999")
case .lightText:
instanceColor = UIColor(hexString: "#cccccc")
case .affirmation:
instanceColor = UIColor(hexString: "#00ff66")
case .negation:
instanceColor = UIColor(hexString: "#ff3300")
case .custom(let hexValue, let opacity):
instanceColor = UIColor(hexString: hexValue).withAlphaComponent(CGFloat(opacity))
}
return instanceColor
}
}
extension UIColor {
/**
Creates an UIColor from HEX String in "#363636" format
- parameter hexString: HEX String in "#363636" format
- returns: UIColor from HexString
*/
convenience init(hexString: String) {
let hexString: String = (hexString as NSString).trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString as String)
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
self.init(red:red, green:green, blue:blue, alpha:1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment