Colors

Episode #283 | 9 minutes | published on July 7, 2017 | Uses swift-3.0, Xcode-8.3
Subscribers Only
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))
blog comments powered by Disqus