Episode #283

Colors

Series: Dive Into Core Graphics

9 minutes
Published on July 7, 2017

This video is only available to subscribers. Get access to this video and 582 others.

What are color spaces? Sam discusses the common color spaces you might use and what they are used for. Why would you use these methods instead of just using UIColor? If you want cross-platform compatible drawing code, you’ll want to know how this works.

Getting a Color Space

// RGB
CGColorSpaceCreateDeviceRGB()

// Grayscale
CGColorSpaceCreateDeviceGray()

// CMYK
CGColorSpaceCreateDeviceCMYK()

Creating Colors with Color Spaces

let colorSpace = CGColorSpaceCreateDeviceRGB()
let color = CGColor(colorSpace: colorSpace, components: [0, 1, 0, 1])!

context.setFillColor(color)
context.fill(bounds)

context.setFillColor(CGColor(colorSpace: colorSpace, components: [0, 0, 1, 0.5])!)
context.setBlendMode(.normal)
context.fill(rect.insetBy(dx: 64, dy: 64))

This episode uses Swift 3.0, Xcode 8.3.