A set of Swift macros for type-safe generation of Color
, UIColor
, and NSColor
from hex string values.
- Easily generate
Color
,UIColor
, andNSColor
from hex strings. - Type-safe implementation ensures you get accurate color objects with no runtime errors.
- Supports hex color codes with or without the leading
#
symbol. - Case-insensitive hex string support (e.g.,
ff0000
andFF0000
are equivalent).
Make sure to import the module where you want to use the macro:
import HexColor
let color = #color(hex: "FFF", opacity: 1, in: .sRGB)
// Expands to: Color(.sRGB, red: 1.0, green: 1.0, blue: 1.0, opacity: 1)
let uiColor = #uiColor(hex: "FF0000", alpha: 1)
// Expands to: UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1)
let uiColorDisplayP3 = #uiColor(displayP3Hex: "FF0000")
// Expands to: UIColor(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 1)
let nsColor = #nsColor(hex: "00FF00")
// Expands to: NSColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1)
#FFF
orFFF
(with or without the#
symbol)- Full hex format:
#FF0000
,#00FF00
, etc.
To install this library using Swift Package Manager, add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/zijievv/hex-color.git", from: "0.1.0")
]
Or, if you’re using Xcode, you can add the package through File > Add Packages and search for the repository URL.
Feel free to fork the repository, create a new branch, and submit pull requests. Contributions are welcome!
This project is licensed under the MIT License - see the LICENSE file for details.