A set of Swift macros for type-safe generation of Color
, UIColor
, and NSColor
from hex string/integer values.
- Easily generate
Color
,UIColor
, andNSColor
from hex strings/integers. - 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
#color(hex: "FFF", opacity: 1, in: .sRGB)
#color(hex: 0xfff)
// Expands to: Color(.sRGB, red: 1.0, green: 1.0, blue: 1.0, opacity: 1)
#uiColor(hex: "FF0000", alpha: 1)
// Expands to: UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1)
#uiColor(displayP3Hex: 0xFF0000)
// 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)
- Hexadecimal in string literal:
"#fff"
,"FF0000"
; - Hexadecimal in integer literal:
0xFFF
,0xff0000
.
@freestanding(expression) public macro color(hex: String, opacity: Double = 1, in colorSpace: Color.RGBColorSpace = .sRGB) -> Color
@freestanding(expression) public macro color(hex: Int, opacity: Double = 1, in colorSpace: Color.RGBColorSpace = .sRGB) -> Color
@freestanding(expression) public macro uiColor(hex: String, alpha: CGFloat = 1) -> UIColor
@freestanding(expression) public macro uiColor(hex: Int, alpha: CGFloat = 1) -> UIColor
@freestanding(expression) public macro uiColor(displayP3Hex: String, alpha: CGFloat = 1) -> UIColor
@freestanding(expression) public macro uiColor(displayP3Hex: Int, alpha: CGFloat = 1) -> UIColor
@freestanding(expression) public macro nsColor(hex: String, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(hex: Int, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(srgbHex: String, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(srgbHex: Int, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(displayP3Hex: String, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(displayP3Hex: Int, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(calibratedHex: String, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(calibratedHex: Int, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(deviceHex: String, alpha: CGFloat = 1) -> NSColor
@freestanding(expression) public macro nsColor(deviceHex: Int, alpha: CGFloat = 1) -> NSColor
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.