A Swift 6 package for working with colors at the pixel level. The PixelColor struct provides utilities for defining, manipulating, and converting colors with red, green, blue, and opacity channels (CGFloat).
- Color Channels:
- Work with individual
red,green,blue, andopacitychannels.
- Work with individual
- Color Conversions:
- Use
hue,saturation, andbrightness(HSB) representations. - Get a SwiftUI
Angleof the hue (.hueAngle). - Initialize colors using
hexstrings with or without opacity.
- Use
- Built-in Colors:
- System colors like
.red,.green,.blue,.orange,.teal, etc. - Raw colors for precision:
.rawRed,.rawYellow,.rawGreen, etc. - Clear "white" color for blending gradients (
.clearWhite). - Adaptable colors (
.primary,.background) that respect light/dark appearance modes.
- System colors like
- Operators:
- Arithmetic (
+,-,*,/) for blending and scaling colors. - Prefix operator
!for inverting colors.
- Arithmetic (
- Codable, Equatable and Hashable:
- Serialize, equate and hash colors easily.
- Sendable:
- Work with colors concurrently.
- SwiftUI / UIKit / AppKit Compatibility:
- Convert
PixelColorto and from SwiftUI'sColoror platform-specificUIColor/NSColor.
- Convert
- Utility Methods:
- Modify hue, saturation, brightness, and opacity.
- Generate random colors (
.random()) or random fully saturated hues (.randomHue()). - Check and identify pure channel colors (
.isPureChannel).
Add the following dependency to your Package.swift file:
dependencies: [
.package(url: "https://github.com/heestand-xyz/PixelColor", from: "3.0.0")
]Then, import PixelColor in your code:
import PixelColorlet color = PixelColor(red: 0.5, green: 0.25, blue: 0.75, opacity: 1.0)let color = PixelColor(hex: "#FF8000") // Orange
let semiTransparentColor = PixelColor(hexWithOpacity: "#FF800080") // 50% transparent orangelet color = PixelColor(hue: 0.5, saturation: 1.0, brightness: 1.0, opacity: 1.0)let shiftedColor = color.shiftHue(by: .degrees(180))let brighterColor = color.brighten(by: 1.5)let semiTransparentColor = color.withOpacity(of: 0.5)Note that
PixelColordoes not manage the color space, these functions are just for convenience.
let linearColor = color.sRGBToLinear()let srgbColor = linearColor.linearToSRGB()let swiftUIColor: Color = color.color
let uiColor: UIColor = color.uiColor
/// macOS only
let nsColor: NSColor = color.nsColorlet blendedColor = color1 + color2let invertedColor = !colorlet scaledColor = color * 0.8let randomColor = PixelColor.random()
let randomHueColor = PixelColor.randomHue()
PixelColor.Channelis an enum of the 4 channels.
if color.hasPureChannel {
print("Pure channel: \(color.pureChannel!)")
}A color has a pure channel when one channel is at
1.0and the other channels are at0.0.
let primaryColor = PixelColor.primary // White in dark mode, black in light mode
let backgroundColor = PixelColor.background // Opposite of primarylet hex = color.hex // "7F3FBF"
let hexWithOpacity = color.hexWithOpacity // "7F3FBFFF"Feel free to submit pull requests or open issues for improvements and feature requests.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by Anton Heestand