Topics

#195: Mastering UIColor with Hue 🌈

Topics

Today we'll check out a library from Hyper called Hue. It gives us all kinds of helpful utilities for working with colors, let's dive in.

First, Hue helps us easily can parse colors from hex strings:

let beige = UIColor.hex("#FEFAF1")
let lightBrown = UIColor.hex("#AB9372")
let brown = UIColor.hex("#8C5637")

In addition, Hue can analyze colors:

beige.isDark // returns false

Hue can easily create gradients. Here we'll take a couple of colors, and create a CAGradientLayer from them:

let gradient = [lightBrown, brown].gradient { gradient in
  gradient.locations = [0.25, 1.0]
  return gradient
}

Finally, Hue can analyze a UIImage, returning a tuple of colors it finds inside.

let (bg, primary, secondary, _) = albumArt.colors()

It provides background, primary, secondary and detail colors from the image. We can the use these to create interfaces such as the one found in iTunes on OS X, where the colors in the UI match those found in the album artwork. Neat!

More info about Hue can be found at git.io/hue