-
-
Save astericky/3ba0619d825d97558f5807c73af4dd48 to your computer and use it in GitHub Desktop.
SwiftUI Hex Colors
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
//Trying out a prefix operator | |
//I thought vertical elipses made sense, representative of rbg | |
prefix operator ⋮ | |
prefix func ⋮(hex:UInt32) -> Color { | |
return Color(hex) | |
} | |
extension Color { | |
init(_ hex: UInt32, opacity:Double = 1.0) { | |
let red = Double((hex & 0xff0000) >> 16) / 255.0 | |
let green = Double((hex & 0xff00) >> 8) / 255.0 | |
let blue = Double((hex & 0xff) >> 0) / 255.0 | |
self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity) | |
} | |
} | |
let hexColor:(UInt32) -> (Color) = { | |
return Color($0) | |
} | |
struct ContentView : View { | |
var body: some View { | |
Text("Hello World") | |
.color(⋮0x4286f4) | |
.background(Color(0x420666)) | |
.background(hexColor(0x426f45)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment