ColorCalculation

1.1.0

A color extension library for calculating hex, brightness, etc.
Kjuly/ColorCalculation

What's New

1.1.0

2024-01-11T08:02:12Z

ColorCalculation

A color extension library for calculating hex, brightness, etc.

macOS iOS watchOS

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.hex

An extension to String is also provided to get hex values in Int32.

// 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.

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.

color.toUIColor
color.toNSColor

uiColor.toColor
nsColor.toColor

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Apr 15 2024 16:13:53 GMT-0900 (Hawaii-Aleutian Daylight Time)