There are several questions out there that ask about converting colors between colorspaces on Apple platforms. Unfortunately, the answers quite often involve NSColor
or UIColor
-- non portable Objective-C classes that cannot be interchangeably used on OS X and iOS.
So I'd like to ask a very specific thing that I'm sure there must be a good answer to out there. I simply cannot believe that Apple would not foresee the need for this.
How does one convert CGColor
from one colorspace (for example, monochrome) to another one (for example, RGB) in a generic way, supporting all CGColorSpace
types, while using solely the portable Core Graphics functions?
Some context. I need to multiply the value provided by an online service with a value stored in UIColor
. Correct way to extract the RGB components prior to iOS5, which finally introduced the method -[UIColor getRed:green:blue:alpha:]
, is to use CGColorGetComponents()
. I then multiply this color with the color fetched from the online service. This fails in case +[UIColor grayColor]
was used to generate the UIColor
. Meaning, I need to convert the color from the greyscale colorspace into RGB. In this case, it's easy. What about some other colorspace being provided? Or what if in a theoretical future scenario I just want to process a single pixel's color?
There is a suggestion somewhere that I paint a pixel into a bitmap context and then read this pixel. That's insane, and I hope it isn't the only way to do this. Obviously the drawing method can figure out how to perform the conversion; how can we leverage this without creating a bitmap context solely to draw a pixel in it?
Additional research:
- This article on color conversion, aside from
UIColor
's undocumented/private method-styleString
, more interestingly mentions also undocumentedCGColorTransform
s. - ColorCG.cpp from WebKit suggests there is a header named
CoreGraphics/CGColorTransform.h
. Unfortunately, this header does not exist, at least not on Mountain Lion. Why would Apple hide these APIs? - The only other sensible resource I found mentioning
CGColorTransform
s is FreeQuartz, a free/open source reimplementation of Core Graphics.
Filed a radar with Apple, #12141580, to open up and document CGColorTransform
. I'm not holding my breath, though, so if there are other sensible suggestions, I'm all ears.
-[NSString pathExtension]
, then? People happily use substrings in other languages. If the demand is so low, why did Apple add-[UIColor getRed:green:blue:alpha:]
? You talk about energy - why would thousands of developers invest energy to reinvent the wheel a thousand times if Apple could? I understand your point of view, but I politely disagree with it in belief that something can be done without creating a context orNSImage
/CGImage
. Using vImage sounds interesting, care to post an example as an answer? – Ivan Vučica