Converting colorspaces in Objective-C

Viewed 52

How can I convert from device colour space to calibrated RGB space.Please try to add changes to the following code:

    NSColor *MyColorAtScreenCoordinate(CGDirectDisplayID displayID, NSInteger x, NSInteger y) {
    CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(x, y, 1, 1));
    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
    CGImageRelease(image);
    NSColor *color = [bitmap colorAtX:0 y:0];
    [bitmap release];
    return color;
}

I got the code from this post. I found an answer for swift here ,I am pretty new to swift and even the docs didnt help

Update

I managed to come with the code:

//'color' is the color from the function
//bitmapRep points to a NSBitmapImageRep

CGFloat* components = malloc(color.numberOfComponents *sizeof(CGFloat));
[color getComponents:components];
NSColor* correctedcolor = [NSColor colorWithColorSpace[self.bitmapRep colorspace]
                              components:components count:colorbmp.numberOfComponents];

free(components);
[correctedcolor colorUsingColorSpace:[NSColorSpace sRGBColorSpace]];
return correctedcolor

Even with trying this piece of code I was not getting the correct pixel color, I tried using a colorpicker from Appstore which resulted in showing a value different than which was read from using my app.

Example when picking a pure Red color:

Commercial Colorpicker: 0xFF0000
MY Color Picker: 0xFF0404

How can I get the correct calibrated value of the selected pixel?

0 Answers
Related