Skip to content

Instantly share code, notes, and snippets.

@aChase55
Last active December 5, 2020 21:43
Show Gist options
  • Save aChase55/da4b2ab8e093a12c3c82015ab455ce77 to your computer and use it in GitHub Desktop.
Save aChase55/da4b2ab8e093a12c3c82015ab455ce77 to your computer and use it in GitHub Desktop.
SwiftUI Hex Colors
//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))
}
}
@davidakoontz
Copy link

If you're after a better prefix op. - maybe the "therefore" ∴ sign - easier to see than the ⋮ verticle elipses and reminds me of the dot pattern on most monitors/TVs RGB triangle pattern.

unicode 2234. https://en.wikipedia.org/wiki/Therefore_sign

@NavidFehren
Copy link

Hello David,
great work!

Do you have any idea how to make an extension to Color for a function like .getHexValue() which you get the current Hex Value as string?

I would highly appreciate you help!!

Kind regards,
Navid

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