DynamicColor
DynamicColor provides powerful methods to manipulate colors in an easy way in Swift.
Requirements
- iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.2+
- Swift 5.0+
Usage
Creation (Hex String)
Firstly, DynamicColor provides useful initializers to create colors using hex strings or values:
To be platform independent, the typealias DynamicColor
can also be used:
Darken & Lighten
These two create a new color by adjusting the lightness of the receiver. You have to use a value between 0 and 1.
Saturate, Desaturate & Grayscale
These will adjust the saturation of the color object, much like darkened
and lighter
adjusted the lightness. Again, you need to use a value between 0 and 1.
Adjust-hue & Complement
These adjust the hue value of the color in the same way like the others do. Again, it takes a value between 0 and 1 to update the value.
Tint & Shade
A tint is the mixture of a color with white and a shade is the mixture of a color with black. Again, it takes a value between 0 and 1 to update the value.
Invert
This can invert the color object. The red, green, and blue values are inverted, while the opacity is left alone.
Mix
This can mix a given color with the receiver. It takes the average of each of the RGB components, optionally weighted by the given percentage (value between 0 and 1).
Gradients
DynamicColor provides an useful object to work with gradients: DynamicGradient. It'll allow you to pick color from gradients, or to build to build a palette using different color spaces (.e.g.: RGB, HSL, HSB, Cie L*a*b*).
Let's define our reference colors and the gradient object:
RGB
Let's build the RGB palette (the default color space) with 8 colors:
HSL
Now if you want to change the gradient color space to have a different effect, just write the following lines:
Cie L*a*b*
Or if you prefer to work directly with array of colors, you can:
And many more...
DynamicColor
also provides many another useful methods to manipulate the colors like hex strings, color components, color spaces, etc. To go further, take a look at the example project.
Installation
CocoaPods
Install CocoaPods if not already available:
Go to the directory of your Xcode project, and Create and Edit your Podfile and add DynamicColor:
Install into your project:
Open your project in Xcode from the .xcworkspace file (not the usual project file):
You can now import DynamicColor
framework into your files.
Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
To integrate DynamicColor
into your Xcode project using Carthage, specify it in your Cartfile
file:
Swift Package Manager
You can use The Swift Package Manager to install DynamicColor
by adding the proper description to your Package.swift
file:
Note that the Swift Package Manager is still in early design and development, for more information checkout its GitHub Page.
Manually
Download the project and copy the DynamicColor
folder into your project to use it in.