Reputation: 23
I have RGB values that I need to convert into CMYK values. I'm using the iPhone SDK to try and do this.
I have tried a couple of things, but cannot either get the values to be anything other than 0 or 1, or the K element is completely wrong (showing up as a negative number).
Any help would be a life saver!
Sorry, code:
float k = MIN(1-red,MIN(1-green,1-blue));
float c = (1-red-k)/(1-k);
float m = (1-green-k)/(1-k);
float y = (1-blue-k)/(1-k);
NSLog(@"%d %d %d %d",c,m,y,k);
With the following values for red, green and blue:
93, 37 and 27 I get the following values back:
0 0 1073741824 1071858897
Upvotes: 2
Views: 1941
Reputation: 308130
Your formula expects values in the range of 0 to 1, but you're feeding it the more common range of 0 to 255.
float k = MIN(255-red,MIN(255-green,255-blue));
float c = 255*(255-red-k)/(255-k);
float m = 255*(255-green-k)/(255-k);
float y = 255*(255-blue-k)/(255-k);
You'll also need a special case when k==255, otherwise you'll get a divide by zero.
Upvotes: 4
Reputation: 224864
You have a few problems:
1-red
, 1-green
, etc. That indicates that your code expects red
, green
and blue
to be values between 0
and 1
, not large integers like 93
.%d
is the format specifier for an integer, and you're passing float
argumentsUpvotes: 1