Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way to convert CGColors between colorspaces
    primarykey
    data
    text
    <p>There are several questions out there that ask about converting colors between colorspaces on Apple platforms. Unfortunately, the answers quite often involve <code>NSColor</code> or <code>UIColor</code> -- non portable Objective-C classes that cannot be interchangeably used on OS X and iOS.</p> <p>So I'd like to ask a very specific thing that I'm <strong>sure</strong> there must be a good answer to out there. I simply cannot believe that Apple would not foresee the need for this.</p> <p>How does one convert <code>CGColor</code> from one colorspace (for example, monochrome) to another one (for example, RGB) in a generic way, supporting all <code>CGColorSpace</code> types, while using solely the portable Core Graphics functions?</p> <hr> <p>Some context. I need to multiply the value provided by an online service with a value stored in <code>UIColor</code>. Correct way to extract the RGB components prior to iOS5, which finally introduced the method <code>-[UIColor getRed:green:blue:alpha:]</code>, is to use <code>CGColorGetComponents()</code>. I then multiply this color with the color fetched from the online service. This fails in case <code>+[UIColor grayColor]</code> was used to generate the <code>UIColor</code>. 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?</p> <p>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?</p> <hr> <p>Additional research:</p> <ul> <li><a href="http://web.archive.org/web/20090223205518/http://ofcodeandmen.poltras.com:80/" rel="nofollow noreferrer">This article</a> on color conversion, aside from <code>UIColor</code>'s undocumented/private method <code>-styleString</code>, more interestingly mentions also undocumented <code>CGColorTransform</code>s.</li> <li><a href="http://www.opensource.apple.com/source/WebCore/WebCore-737.5/platform/graphics/cg/ColorCG.cpp" rel="nofollow noreferrer">ColorCG.cpp from WebKit</a> suggests there is a header named <code>CoreGraphics/CGColorTransform.h</code>. Unfortunately, this header does not exist, at least not on Mountain Lion. Why would Apple hide these APIs?</li> <li>The only other sensible resource I found mentioning <code>CGColorTransform</code>s is FreeQuartz, a free/open source reimplementation of Core Graphics.</li> </ul> <p>Filed a radar with Apple, #12141580, to open up and document <code>CGColorTransform</code>. I'm not holding my breath, though, so if there are other sensible suggestions, I'm all ears.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI've been doing this for years either using vImage (Accelerate) or bit map contexts. The demand for such utilities as you suggest is low - why would Apple invest the time and energy to do them? Given pixels in RGB format, you can convert to other spaces using easy-to-find C code online, process the data, then convert back into RGB.
      singulars
    2. CO@DavidH: Your argument is silly. Why does Apple provide `-[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 or `NSImage`/`CGImage`. Using vImage sounds interesting, care to post an example as an answer?
      singulars
    3. COThe proper way to vent your frustration is to enter an enhancement bug on bug reporter.apple.com. I have entered something like 300 or so over the past 8 years. Or, you could choose to make a nice open source project to do this and put it on github (I have several such projects), and attain fame and fortune through it!
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload