Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raymondjavaxx/colortoolbox
Swift color utilities
https://github.com/raymondjavaxx/colortoolbox
appkit color ios swift swiftui uikit
Last synced: 22 days ago
JSON representation
Swift color utilities
- Host: GitHub
- URL: https://github.com/raymondjavaxx/colortoolbox
- Owner: raymondjavaxx
- License: mit
- Created: 2023-07-02T22:16:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-22T14:24:37.000Z (9 months ago)
- Last Synced: 2024-12-30T18:02:50.844Z (23 days ago)
- Topics: appkit, color, ios, swift, swiftui, uikit
- Language: Swift
- Homepage:
- Size: 29.3 KB
- Stars: 33
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ColorToolbox
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fraymondjavaxx%2FColorToolbox%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/raymondjavaxx/ColorToolbox)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fraymondjavaxx%2FColorToolbox%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/raymondjavaxx/ColorToolbox)Swift color utilities for UIKit, AppKit and SwiftUI.
## Installation
## Swift Package ManagerAdd the following dependency to your `Package.swift` file:
```swift
.package(url: "https://github.com/raymondjavaxx/ColorToolbox.git", from: "1.0.1")
```## CocoaPods
Add the following line to your `Podfile`:
```ruby
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:
```swift
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:
```swift
let hexString = color.toHex()
```### Calculating the relative luminance
```swift
let color: UIColor = .red
print(color.relativeLuminance) // 0.2126
```### Calculating WCAG contrast ratio
```swift
let color1 = ...
let color2 = ...let contrastRatio = color1.contrastRatio(to: color2)
```### Lightening and darkening colors
```swift
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](LICENSE) file for more info.