Skip to content

zijievv/hex-color

Repository files navigation

Hex Color

A set of Swift macros for type-safe generation of Color, UIColor, and NSColor from hex string/integer values.

Features

  • Easily generate Color, UIColor, and NSColor 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 and FF0000 are equivalent).

Usage

Importing

Make sure to import the module where you want to use the macro:

import HexColor

Example Usage

#color for Color (SwiftUI)

#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 for UIColor (UIKit)

#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)

#nsColor for NSColor (AppKit)

let nsColor = #nsColor(hex: "00FF00")
// Expands to: NSColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1)

Supported Formats

  • Hexadecimal in string literal: "#fff", "FF0000";
  • Hexadecimal in integer literal: 0xFFF, 0xff0000.

Declaration

@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

Installation

Swift Package Manager

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.

Contributing

Feel free to fork the repository, create a new branch, and submit pull requests. Contributions are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Swift macros for type-safe generation of Color, UIColor, and NSColor from hex string values.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages