Swift Only
hue
PoolModern Swift color utilities with hex parsing, HSL conversion, WCAG accessibility, color theory, and platform bridging
$ lpm install @lpm.dev/swiftd.hue
Hue
Modern Swift color utilities with hex parsing, HSL, WCAG accessibility, and color theory.
Swift 5.9+ | iOS 13+ / macOS 10.15+ / watchOS 6+ / tvOS 13+ / visionOS 1+ | Zero Dependencies
// Hex colors
let coral: Hue = "#FF6B6B"
let dark = coral.darker(by: 0.2)
// WCAG accessibility
coral.meetsWCAG(.aa, against: .white) // false
coral.bestTextColor() // .black
// Color theory
let (warm, cool) = coral.analogous
let palette = coral.triadicWhy Hue?
| Feature | UIColor-Hex-Swift | DynamicColor | Hue |
|---|---|---|---|
| Hex parsing | 6-char only | Yes | 3/4/6/8-char + integer |
| HSL support | No | Partial | Full (init + computed) |
| Color manipulation | No | Yes | Yes (7 methods) |
| Color theory | No | No | Yes (5 schemes) |
| WCAG accessibility | No | No | Yes (AA/AAA, luminance, contrast) |
| SwiftUI integration | No | No | Color(hex:) + bridging |
| Codable/Hashable | No | No | Yes (synthesized) |
| Sendable | No | No | Yes |
| String literal | No | No | let c: Hue = "#FF6B6B" |
| Dependencies | None | None | None |
Installation
LPM
# As an SPM dependency (recommended — manages versions and transitive deps)
lpm install @lpm.dev/swiftd.hue
# Source delivery (if you want to modify the code directly)
lpm add @lpm.dev/swiftd.hueUsage
Creating Colors
import Hue
// From hex string
let coral = Hue(hex: "#FF6B6B")
let blue = Hue(hex: "0000FF") // # is optional
let short = Hue(hex: "#F00") // 3-char shorthand
let alpha = Hue(hex: "#FF000080") // 8-char with alpha
// From hex integer
let green = Hue(hex: 0x00FF00)
// From RGBA
let custom = Hue(red: 0.5, green: 0.3, blue: 0.8)
// From HSL
let pastel = Hue(hue: 210, saturation: 0.8, lightness: 0.85)
// String literal
let color: Hue = "#FF6B6B"
// Predefined
Hue.black, Hue.white, Hue.red, Hue.green, Hue.blue, Hue.clearColor Manipulation
All methods return new Hue values (immutable value semantics):
let base = Hue(hex: "#FF6B6B")
base.lighter(by: 0.2) // increase lightness
base.darker(by: 0.2) // decrease lightness
base.saturated(by: 0.3) // more vivid
base.desaturated(by: 0.3) // more muted
base.adjustHue(by: 90) // rotate hue 90°
base.inverted() // RGB complement
base.grayscale() // fully desaturatedColor Mixing
let pink = Hue.red.mixed(with: .white) // 50/50
let mostlyRed = Hue.red.mixed(with: .blue, amount: 0.2) // 80% red, 20% blueColor Theory
let base = Hue(hex: "#FF6B6B")
base.complementary // 180° opposite
let (warm, cool) = base.analogous // ±30°
let (b, c) = base.triadic // ±120°
let (l, r) = base.splitComplementary // 150° and 210°
let (q2, q3, q4) = base.tetradic // 90°, 180°, 270°WCAG Accessibility
let text = Hue(hex: "#333333")
let bg = Hue.white
// Contrast ratio (1:1 to 21:1)
text.contrastRatio(with: bg) // ≈ 12.63
// WCAG compliance
text.meetsWCAG(.aa, against: bg) // true (≥ 4.5:1)
text.meetsWCAG(.aaa, against: bg) // true (≥ 7:1)
text.meetsWCAG(.aa, size: .large, against: bg) // true (≥ 3:1)
// Light/dark detection
bg.isLight // true
text.isDark // true
// Best text color for a background
bg.bestTextColor() // .black (highest contrast)
bg.bestTextColor(from: [Hue(hex: "#333"), Hue(hex: "#CCC")])HSL Components
let color = Hue(hex: "#FF6B6B")
color.hue // 0.0 (degrees, 0–360)
color.saturation // 1.0 (0.0–1.0)
color.lightness // 0.71 (0.0–1.0)
// Full HSLA tuple
let (h, s, l, a) = color.hslaComponent Accessors
let color = Hue(hex: "#FF6B6B")
// Individual components
color.red // 1.0
color.green // 0.42
color.blue // 0.42
color.alpha // 1.0
// RGBA tuple
let (r, g, b, a) = color.rgba
// New color with modified component
color.withAlpha(0.5)
color.withRed(0.8)Platform Bridging
// CGColor (always available)
layer.borderColor = Hue(hex: "#FF6B6B").cgColor
let hue = Hue(cgColor: someCGColor)
// UIColor (iOS, tvOS, watchOS, Mac Catalyst)
view.backgroundColor = Hue(hex: "#FF6B6B").uiColor
let hue = Hue(uiColor: .systemBlue)
// NSColor (macOS)
textField.textColor = Hue(hex: "#333333").nsColor
let hue = Hue(nsColor: .controlAccentColor)SwiftUI
// Hue → SwiftUI Color
Text("Hello")
.foregroundColor(Hue(hex: "#FF6B6B").color)
// Convenience Color(hex:) extension
Text("World")
.foregroundColor(Color(hex: "#FF6B6B"))
// Direct Color init
let hue = Hue(hex: "#FF6B6B")
Text("!").foregroundColor(Color(hue))Codable & Hashable
// Encode/decode colors in JSON
struct Theme: Codable {
let primary: Hue
let secondary: Hue
}
// Use as dictionary keys or in sets
var colorSet = Set<Hue>()
colorSet.insert(Hue(hex: "#FF6B6B"))Hex Export
Hue.red.toHex() // "#FF0000"
Hue.red.toHex(includeAlpha: true) // "#FF0000FF"
Hue(hex: "#FF6B6B").description // "Hue(#FF6B6B)"API Reference
Hue (struct)
| Init | Description |
|---|---|
init(red:green:blue:alpha:) |
RGBA components (0.0–1.0) |
init(hex: String) |
Hex string (#RGB, #RRGGBB, #RRGGBBAA) |
init(hex: UInt32) |
Hex integer (0xFF5733) |
init(hue:saturation:lightness:alpha:) |
HSL components |
| Property | Description |
|---|---|
red, green, blue, alpha |
RGBA components (0.0–1.0) |
hue |
HSL hue angle (0–360) |
saturation |
HSL saturation (0.0–1.0) |
lightness |
HSL lightness (0.0–1.0) |
rgba |
RGBA tuple |
hsla |
HSLA tuple |
isLight, isDark |
Luminance-based detection |
relativeLuminance |
WCAG relative luminance |
complementary |
180° hue rotation |
analogous |
±30° hue pair |
triadic |
±120° hue pair |
splitComplementary |
150° / 210° pair |
tetradic |
90° / 180° / 270° triple |
| Method | Description |
|---|---|
lighter(by:) |
Increase HSL lightness |
darker(by:) |
Decrease HSL lightness |
saturated(by:) |
Increase HSL saturation |
desaturated(by:) |
Decrease HSL saturation |
adjustHue(by:) |
Rotate hue by degrees |
inverted() |
RGB complement |
grayscale() |
Full desaturation |
mixed(with:amount:) |
Linear RGB interpolation |
withAlpha(_:) |
New color with alpha |
withRed(_:) / withGreen(_:) / withBlue(_:) |
Component replacement |
toHex(includeAlpha:) |
Export as hex string |
contrastRatio(with:) |
WCAG contrast ratio |
meetsWCAG(_:size:against:) |
WCAG compliance check |
bestTextColor(from:) |
Highest-contrast candidate |
Predefined Colors
.black, .white, .red, .green, .blue, .clear
Platform Bridging
| Property/Init | Platform |
|---|---|
cgColor / init(cgColor:) |
All (CoreGraphics) |
uiColor / init(uiColor:) |
iOS, tvOS, watchOS, Mac Catalyst |
nsColor / init(nsColor:) |
macOS |
color |
SwiftUI |
Platform Behavior
| Platform | Bridging | Notes |
|---|---|---|
| iOS | UIColor + CGColor | Full support |
| macOS | NSColor + CGColor | Full support |
| watchOS | UIColor + CGColor | Full support |
| tvOS | UIColor + CGColor | Full support |
| visionOS | UIColor + CGColor | Full support |
All core operations (hex, HSL, manipulation, accessibility) work on every platform.
License
MIT
lpmswiftcolorhexhslwcagaccessibilityswiftuiuicolorzero-dependency
Unlimited AccessInstall as many Pool packages as you need.
Fund Real WorkEvery install you run sends revenue directly to the developer who built it.
Taxes calculated at checkout based on your location.
Insights
59/100Fair
Weekly Installs
1Version
1.0.2Published
LicenseMIT
Size106.51 KB
Files28
Swift Tools5.9.0
Platforms
iOS 13.0+macOS 10.15+watchOS 6.0+tvOS 13.0+visionOS 1.0+