The colour is one of the most important aspect when we design an interface or when we need to define a graphical charter. That’s why the UIColor class (from the UIKit framework) is a fundamental class. Unfortunately for us, this object is only focused on the creation, not the manipulation, and it is only based on the RGBA model. This is where DynamicColor comes in with plenty of useful features to manipulate the color objects easily.
Let’s code!
First of all, the library can be downloaded here, but the simplest way to work with the library is to use Cocoapods or Carthage. Then when you have added the DynamicColor framework to your project, just import it like that import DynamicColor
.
Let’s start off by creating a variable for the colour that we’re going to manipulate and that can be compared to the base colour of our theme:
// DynamicColor let's you create color with hex number or strings
let baseColor = UIColor(hex: 0xc0392b)
// Equivalent to
// let baseColor = UIColor(hexString: "#c0392b")
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 darkenColor and lightenColor 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).
Conclusion
DynamicColor provides a lot of useful method to make the color manipulation easier. For example if you let the user customise the main color of your application it can generate all the theme for you. For more information you can let a comment or contact me via social network.