ColorCalculation
A color extension library for calculating hex, brightness, etc.
Installation
See the following subsections for details on the different installation methods.
Color Hex Usage
Create a color from hex value.
// 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.
color.hex
uiColor.hex
nsColor.hexAn extension to String is also provided to get hex values in Int32.
// Below all are valid and returns 0xFFFFFF.
"FFFFFF".toColorHex
"#FFFFFF".toColorHex
"0xFFFFFF".toColorHexColor 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.
let backgroundColor = UIColor(hex: 0xFFFFFF)
let foregroundColor: UIColor = (backgroundColor.isBrightColor ? .black : .white)
// `foregroundColor = .black` in this caseColor Conversion Usage
Convenient extension for converting colors between Color, UIColor and NSColor.
color.toUIColor
color.toNSColor
uiColor.toColor
nsColor.toColor