Created
August 14, 2023 14:54
-
-
Save Shubham-0812/b5083b4330109c482a9e3f92591adcf2 to your computer and use it in GitHub Desktop.
Extensions + Utils
This file contains hidden or 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 SwiftUI | |
extension Color { | |
init(hex: String) { | |
self.init(UIColor(hex: hex)) | |
} | |
} | |
extension UIColor { | |
/// For converting Hex-based colors | |
convenience init(hex: String) { | |
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines) | |
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") | |
var rgb: UInt64 = 0 | |
var r: CGFloat = 0.0 | |
var g: CGFloat = 0.0 | |
var b: CGFloat = 0.0 | |
var a: CGFloat = 1.0 | |
let length = hexSanitized.count | |
Scanner(string: hexSanitized).scanHexInt64(&rgb) | |
if length == 6 { | |
r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0 | |
g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0 | |
b = CGFloat(rgb & 0x0000FF) / 255.0 | |
} else if length == 8 { | |
r = CGFloat((rgb & 0xFF000000) >> 24) / 255.0 | |
g = CGFloat((rgb & 0x00FF0000) >> 16) / 255.0 | |
b = CGFloat((rgb & 0x0000FF00) >> 8) / 255.0 | |
a = CGFloat(rgb & 0x000000FF) / 255.0 | |
} | |
self.init(red: r, green: g, blue: b, alpha: a) | |
} | |
} | |
/// ``==================================================================================================================================`` | |
/// `FOUNDATION` | |
import Foundation | |
extension Double { | |
func clean(places: Int) -> String { | |
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(format: "%.\(places)f", self) | |
} | |
} | |
/// ``==================================================================================================================================`` | |
/// `COLOR UTILS` | |
import SwiftUI | |
extension Color { | |
static let background: Color = Color(UIColor.systemBackground) | |
static let label: Color = Color(UIColor.label) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment