ColorToolbox

1.1.0

Swift color utilities
raymondjavaxx/ColorToolbox

What's New

1.1.0

2024-04-23T20:37:50Z
  • Lowered the minimum deployment target to iOS/tvOS/Catalyst 13.0, macOS 10.15, and watchOS 6.0. (#16)

ColorToolbox

Swift color utilities for UIKit, AppKit and SwiftUI.

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/raymondjavaxx/ColorToolbox.git", from: "1.0.1")

CocoaPods

Add the following line to your Podfile:

pod 'ColorToolbox', '~> 1.0'

Usage

ColorToolbox is implemented as a set of extensions on UIColor, NSColor and Color (SwiftUI). All utility methods and properties are available on all supported platforms.

Converting from and to hex string

To create a color from a hex string, use the init(hex:) initializer:

import ColorToolbox

// UIKit
let color = UIColor(hex: "#ff0000")

// AppKit
let color = NSColor(hex: "#ff0000")

// SwiftUI
let color = Color(hex: "#ff0000")

To convert a color to hex, use the toHex() method:

let hexString = color.toHex()

Calculating the relative luminance

let color: UIColor = .red
print(color.relativeLuminance) // 0.2126

Calculating WCAG contrast ratio

let color1 = ...
let color2 = ...

let contrastRatio = color1.contrastRatio(to: color2)

Lightening and darkening colors

let lighterColor = color.lightening(by: 0.2)
let darkerColor = color.darkening(by: 0.2)

License

ColorToolbox is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 11:18:35 GMT-0900 (Hawaii-Aleutian Daylight Time)