Skip to content

icodesign/SwiftColor

Repository files navigation

License platform Build Status codecov

A Swift Extension for UIColor supporting iOS/watchOS/tvOS and macOS.

Installation

Swift Package Manager

.package(url: "https://github.com/icodesign/SwiftColor.git", from: "1.5.0")

Carthage

Add SwiftColor to your Cartfile:

github "icodesign/SwiftColor"

CocoaPods

Add SwiftColor to your Podfile:

pod 'SwiftColor'

Manually

You can also integrate SwiftColor directly with souce code. Just copy SwiftColor.swift to your project.

Usage

Color Initialization

SwiftColor provides a typealias for UIColor/NSColor as Color.

Initialize with Hex String

Supported hex string format: RGB, RGBA, [0x|#]RRGGBB, [0x|#]RRGGBBAA

Color("000")
Color("000C")
Color("0x4DA2D9")
Color(hexString: "#4DA2D9")
Color(hexString: "#4DA2D9CC")

Color(hexString: "#4DA2D9", alpha: 0.8)

"#4DA2D9CC".color

Initialize with Hex Int

Only hex format 0xRRGGBB is supported

Color(hexInt: 0x4DA2D9)

Color(hexInt: 0x4DA2D9, alpha: 0.8)

(0x4DA2D9).color

Initialize with RGBA

Color(byteRed: 77, green: 162, blue: 217, alpha: 0.8)

Get/Change Color Components

let color = Color(hexString: "#4DA2D9CC")

// get color components
var (r, g, b, a) = color.colorComponents()

// change color components
var red = "000".color.red(255)
var alphaColor = Color(hexInt: 0x4DA2D9).alpha(0.8)

Convert color to image

let image = "000".color.toImage()