How to convert colors from one color space to another?
I'm not sure how to automatically convert them, but to identify different color spaces you can get the CGColorSpaceModel
from the color's CGColorSpaceRef
:
UIColor* color = some color;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([color CGColor]);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
Then you can compare colorSpaceModel
with the constants defined in CoreGraphics/CGColorSpace.h
. UIColor's getRed:green:blue:alpha
works for kCGColorSpaceModelRGB
, whereas getWhite:alpha
works for kCGColorSpaceModelMonochrome
.
Note that a UIColor
that was created with colorWithHue:saturation:brightness:alpha:
will actually be in the RGB color space.
In swift 3 we can directly use
let colorSpace = uiColor.cgColor.colorSpace
let csModel = colorSpace.model
to get the color space and color space model from a UIColor
.