https://github.com/kjuly/colorcalculation
A color extension library for calculating hex, brightness, etc.
https://github.com/kjuly/colorcalculation
calculation color color-conversion ios macos spm watchos
Last synced: about 2 months ago
JSON representation
A color extension library for calculating hex, brightness, etc.
- Host: GitHub
- URL: https://github.com/kjuly/colorcalculation
- Owner: Kjuly
- License: mit
- Created: 2023-12-15T10:14:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-11T08:02:15.000Z (over 2 years ago)
- Last Synced: 2026-03-06T01:47:23.523Z (4 months ago)
- Topics: calculation, color, color-conversion, ios, macos, spm, watchos
- Language: Swift
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ColorCalculation
A color extension library for calculating hex, brightness, etc.
[](https://swiftpackageindex.com/Kjuly/ColorCalculation)
[](https://swiftpackageindex.com/Kjuly/ColorCalculation)
![macOS][macOS-Badge] ![iOS][iOS-Badge] ![watchOS][watchOS-Badge]
[![SPM][SPM-Badge]][SPM-Link] [![CocoaPods][CocoaPods-Badge]][CocoaPods-Link] [![Carthage][Carthage-Badge]][Carthage-Link]
[macOS-Badge]: https://img.shields.io/badge/macOS-12.0%2B-blue?labelColor=00367A&color=3081D0
[iOS-Badge]: https://img.shields.io/badge/iOS-15.5%2B-blue?labelColor=00367A&color=3081D0
[watchOS-Badge]: https://img.shields.io/badge/watchOS-6.0%2B-blue?labelColor=00367A&color=3081D0
[SPM-Badge]: https://img.shields.io/github/v/tag/Kjuly/ColorCalculation?label=SPM&labelColor=2F4858&color=A8DF8E
[SPM-Link]: https://swiftpackageindex.com/Kjuly/ColorCalculation
[CocoaPods-Badge]: https://img.shields.io/cocoapods/v/ColorCalculation?label=CocoaPods&labelColor=2F4858&color=A8DF8E
[CocoaPods-Link]: https://cocoapods.org/pods/ColorCalculation
[Carthage-Badge]: https://img.shields.io/github/v/tag/Kjuly/ColorCalculation?label=Carthage&labelColor=2F4858&color=A8DF8E
[Carthage-Link]: https://swiftpackageindex.com/Kjuly/ColorCalculation
## Installation
See the following subsections for details on the different installation methods.
- [Swift Package Manager](INSTALLATION.md#swift-package-manager)
- [CocoaPods](INSTALLATION.md#cocoaPods)
- [Carthage](INSTALLATION.md#carthage)
- [Submodule](INSTALLATION.md#submodule)
## Color Hex Usage
Create a color from hex value.
```swift
// Notes: there's an optional `alpha` arg available.
// e.g., Color(hex: 0xFFFFFF, alpha: 0.5)
Color(hex: 0xFFFFFF)
UIColor(hex: 0xFFFFFF)
NSColor(hex: 0xFFFFFF)
```
Or get the hex value from a color.
```swift
color.hex
uiColor.hex
nsColor.hex
```
An extension to String is also provided to get hex values in Int32.
```swift
// Below all are valid and returns 0xFFFFFF.
"FFFFFF".toColorHex
"#FFFFFF".toColorHex
"0xFFFFFF".toColorHex
```
## Color Brightness Usage
Get to know a color is bright or dark.
It can be used to determine the color of text on the background. If the background color is bright, use black text color, otherwise use white.
```swift
let backgroundColor = UIColor(hex: 0xFFFFFF)
let foregroundColor: UIColor = (backgroundColor.isBrightColor ? .black : .white)
// `foregroundColor = .black` in this case
```
## Color Conversion Usage
Convenient extension for converting colors between Color, UIColor and NSColor.
```swift
color.toUIColor
color.toNSColor
uiColor.toColor
nsColor.toColor
```