HexColors
Swift Macros for hexadecimal color codes
Install / Use
/learn @alvmo/HexColorsREADME
HexColors
HexColors is a Swift package that provides convenient macros for converting hex colors to the different color types on Apple platforms that are validated at compile time.
let red = #color("#ff0000") //Macro expands to SwiftUI.Color(red: 1.0, green: 0.0, blue: 0.0, opacity: 1.0)
Installation
Xcode
In Xcode, go to File > Add Package Dependency and paste the repository URL:
https://github.com/alvmo/HexColors.git
Swift Package Manager
You can use the Swift Package Manager to install HexColors by adding it as a dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/alvmo/HexColors.git", from: "1.0.0")
]
And then add the dependency to your targets.
.target(
name: <TARGET_NAME>,
dependencies: [
.product(name: "HexColors", package: "HexColors"),
//...
]
)
Usage
Importing the Package
In your Swift file, import the HexColors package:
import HexColors
Using the macros
To use HexColors, you can use the #color macros with either a static hex color string or a hexadecimal integer literal:
let red = #color("#ff0000") // Macro expands to SwiftUI.Color(red: 1.0, green: 0.0, blue: 0, opacity: 1.0)
let green = #color(0xff00FF55) // Macro expands to SwiftUI.Color(red: 1.0, green: 0.0, blue: 1.000, opacity: 0.3333 )
let blue = #color(0xfff) // Macro expands to SwiftUI.Color(red: 1.0, green: 1.0, blue: 1.0, opacity: 1.0 )
There are also macros for non-SwiftUI colors types:
let uiColor = #uiColor("#ff0055") // Equivalent to UIColor(red: 1.0, green: 0.0, blue: 0.333, alpha: 1.0)
let nsColor = #nsColor(0xff0055) // Equivalent to NSColor(red: 1.0, green: 0.0, blue: 0.333, alpha: 1.0)
let cgColor = #cgColor("#ff0055") // Equivalent to CGColor(red: 1.0, green: 0.0, blue: 0.333, alpha: 1.0)
Related Skills
node-connect
345.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
104.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
