An open API service indexing awesome lists of open source software.

https://github.com/zijievv/hex-color

Swift macros for type-safe generation of Color, UIColor, and NSColor from hex string values.
https://github.com/zijievv/hex-color

appkit hex hex-strings hexcolor ios macro macros swift swift-macro swift-macros swiftmacro swiftmacros swiftui uikit

Last synced: 4 months ago
JSON representation

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

Awesome Lists containing this project

README

          

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

```swift
import HexColor
```

### Example Usage

#### `#color` for `Color` (SwiftUI)

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

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

```swift
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

```swift
@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:

```swift
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](LICENSE) file for details.