|
| 1 | +# `#Color` |
| 2 | + |
| 3 | +[](https://github.com/davdroman/swiftui-color-macros/actions/workflows/ci.yml) |
| 4 | +[](https://swiftpackageindex.com/davdroman/swiftui-color-macros) |
| 5 | +[](https://swiftpackageindex.com/davdroman/swiftui-color-macros) |
| 6 | + |
| 7 | +`ColorMacros` is a bundle of expression macros that turn the color tokens you copy from |
| 8 | +Figma/Sketch into compile-time `SwiftUI.Color` literals: |
| 9 | + |
| 10 | +```swift |
| 11 | +import SwiftUI |
| 12 | +import ColorMacros |
| 13 | + |
| 14 | +let brand = #Color(hex: "#FF9900") |
| 15 | +let overlay = #Color(hex: "0x336699CC") |
| 16 | +let grass = #Color(rgba: 154, 234, 98, 1) |
| 17 | +let warning = #Color(hsla: 32, 100, 50, 0.8) |
| 18 | +let accent = #Color(hsba: 200, 60, 80, 0.65) |
| 19 | +``` |
| 20 | + |
| 21 | +## Features |
| 22 | + |
| 23 | +- `hex:` accepts `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, with or without `#` or a `0x` prefix. |
| 24 | +- `rgb:` / `rgba:` accept 0–255 integer channels, optionally plus opacity (0–1). |
| 25 | +- `hsl:` / `hsla:` accept degrees/percentages, matching what design tools output. |
| 26 | +- `hsb:` / `hsba:` cover hue–saturation–brightness (%), again with optional opacity. |
| 27 | +- Every variant emits a `SwiftUI.Color` literal (no runtime helpers) and validates inputs at build time. |
| 28 | + |
| 29 | +Example diagnostic: |
| 30 | + |
| 31 | +``` |
| 32 | +#Color(hex: "#12345") |
| 33 | + ┬─────── |
| 34 | + ╰─ 🛑 Hex literals must contain 3, 4, 6, or 8 digits, but found 5. |
| 35 | +``` |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +Add the package to your project: |
| 40 | + |
| 41 | +```swift |
| 42 | +dependencies: [ |
| 43 | + .package(url: "https://github.com/davdroman/swiftui-color-macros.git", from: "0.1.0") |
| 44 | +], |
| 45 | +targets: [ |
| 46 | + .target( |
| 47 | + name: "App", |
| 48 | + dependencies: [ |
| 49 | + .product(name: "ColorMacros", package: "swiftui-color-macros") |
| 50 | + ] |
| 51 | + ) |
| 52 | +] |
| 53 | +``` |
| 54 | + |
| 55 | +Then import the module alongside SwiftUI wherever you need the macro. |
0 commit comments