Skip to content

davdroman/swiftui-color-macros

Repository files navigation

#Color

CI

ColorMacros is a bundle of expression macros that turn the color tokens you copy from Figma/Sketch into compile-time SwiftUI.Color literals:

import SwiftUI
import ColorMacros

let brand = #Color(hex: "#FF9900")
let overlay = #Color(hex: "0x336699CC")
let grass = #Color(rgba: 154, 234, 98, 1)
let warning = #Color(hsla: 32, 100, 50, 0.8)
let accent = #Color(hsba: 200, 60, 80, 0.65)

Features

  • hex: accepts RGB, RGBA, RRGGBB, RRGGBBAA, with or without # or a 0x prefix.
  • rgb: / rgba: accept 0–255 integer channels, optionally plus opacity (0–1).
  • hsl: / hsla: accept degrees/percentages, matching what design tools output.
  • hsb: / hsba: cover hue–saturation–brightness (%), again with optional opacity.
  • Every variant emits a SwiftUI.Color literal (no runtime helpers) and validates inputs at build time.

Example diagnostic:

#Color(hex: "#12345")
            ┬───────
            ╰─ 🛑 Hex literals must contain 3, 4, 6, or 8 digits, but found 5.

Installation

Add the package to your project:

dependencies: [
  .package(url: "https://github.com/davdroman/swiftui-color-macros.git", from: "0.1.0")
],
targets: [
  .target(
    name: "App",
    dependencies: [
      .product(name: "ColorMacros", package: "swiftui-color-macros")
    ]
  )
]

Then import the module alongside SwiftUI wherever you need the macro.

About

Compile-time-checked SwiftUI Colors

Resources

License

Stars

Watchers

Forks

Languages