Last active
March 28, 2024 18:12
-
-
Save sauvikatinnofied/92c25739d4ae7bb5cc45e6b75fd63fe7 to your computer and use it in GitHub Desktop.
FullCode For MediumBlog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Color assets are a solution for this but they are only available on iOS 11+ and macOS 10.13+ . To support older versions you will have to do it in code.