Skip to content

Instantly share code, notes, and snippets.

@kylesluder
Created October 19, 2010 00:08
Show Gist options
  • Save kylesluder/633335 to your computer and use it in GitHub Desktop.
Save kylesluder/633335 to your computer and use it in GitHub Desktop.
Version of OQColorGetComponents that goes through a linear RGB color space
OQLinearRGBA OQGetColorComponents(NSColor *c)
{
static dispatch_once_t once;
static NSColorSpace *linearRGBColorSpace;
dispatch_once(&once, ^{
CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
linearRGBColorSpace = [[NSColorSpace alloc] initWithCGColorSpace:cgColorSpace];
CGColorSpaceRelease(cgColorSpace);
OBASSERT_NOTNULL(linearRGBColorSpace);
OBASSERT([linearRGBColorSpace colorSpaceModel] == NSRGBColorSpaceModel);
OBASSERT([linearRGBColorSpace numberOfColorComponents] == 3);
});
OQLinearRGBA l;
if (c) {
CGFloat components[3];
[[c colorUsingColorSpace:linearRGBColorSpace] getComponents:components];
l.r = components[0];
l.g = components[1];
l.b = components[2];
l.a = [c alphaComponent];
} else
memset(&l, 0, sizeof(l)); // Treat nil as clear.
return l;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment