Skip to content

Instantly share code, notes, and snippets.

@sauvikatinnofied
Last active March 28, 2024 18:12
Show Gist options
  • Save sauvikatinnofied/92c25739d4ae7bb5cc45e6b75fd63fe7 to your computer and use it in GitHub Desktop.
Save sauvikatinnofied/92c25739d4ae7bb5cc45e6b75fd63fe7 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)
}
}
Copy link

ghost commented Jul 19, 2017

That was helpful, thank you.

@iamajijul
Copy link

Tell me one thing Souvik da, suppose in my application, I have 10 screen and all the screen have same background color and I set it using storyBoard background color option, then how can I manage this if client say to change background color?
I want to change it in one place and effect will show to all 10 screen. Is it possible? I don't want go programatically.

@udayeega
Copy link

Tell me one thing Souvik da, suppose in my application, I have 10 screen and all the screen have same background color and I set it using storyBoard background color option, then how can I manage this if client say to change background color?
I want to change it in one place and effect will show to all 10 screen. Is it possible? I don't want go programatically.

I think you can do it by adding your custom color in assets. just google it.

@Arin626
Copy link

Arin626 commented May 7, 2019

I think you can do it by adding your custom color in assets. just google it.

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.

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