ColorHex is a simple package for Swift apps. It allows you to easily work with hex codes in your app.
To install with Swift Package Manager, add the following as a dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/CaedmonMyers/ColorHex.git", from: "1.0.1")
]Alternatively, you can install the package by downloading the ColorHex.swift and adding it to your project.
Show a color view from a hex string:
import ColorHex
struct ContentView: View {
var body: some View {
Color(hex: "FFFFFF")
}
}Use .toHex() with a color picker:
ColorPicker("Color Picker", selection: $color)
.onChange(of: color) { oldValue, newValue in
let uiColor = UIColor(newValue)
let hexString = uiColor1.toHex()
spaces[selectedSpaceIndex].startHex = hexString
}Color(hex: "FFFFFF"): View.toHex(): String?
You will need to properly handle the optional value for the .toHex() function in the event the conversion fails.