I'm trying to convert XYZ values to LAB* using the formula available here.
The problem is, when I go through the formula - the output for the three variables var_X,var_Y and var_Z is always 1.00.
If I put values using into wolfram alpha I actually get the (presumably) correct output (the example in the link is the input of var_Z shown in the logs at the bottom of this question).
Could you help me find where I've gone wrong?
Here's my code:
-(NSArray*)convertXYZtoLABSpaceWithLABArray:(NSArray*)labArray{
double var_X = [[labArray objectAtIndex:0] doubleValue] / 95.047;
double var_Y = [[labArray objectAtIndex:1] doubleValue] / 100.000;
double var_Z = [[labArray objectAtIndex:2] doubleValue] / 108.883;
NSLog(@"convertXYZtoLAB... var_X: %f var_Y: %f var_Z: %f", var_X, var_Y, var_Z);
if ( var_X > 0.008856 ){
var_X = pow(var_X, ( 1/3 ));
}else{
var_X = ( 7.787 * var_X ) + ( 16 / 116 );
}
if ( var_Y > 0.008856 ){
var_Y = pow(var_Y, ( 1/3 ));
}else{
var_Y = ( 7.787 * var_Y ) + ( 16 / 116 );
}
if ( var_Z > 0.008856 ){
var_Z = pow(var_Z, ( 1/3 ));
}else{
var_Z = ( 7.787 * var_Z ) + ( 16 / 116 );
}
NSLog(@"convertXYZtoLAB... var_X: %f var_Y: %f var_Z: %f", var_X, var_Y, var_Z);
NSNumber *l,*a,*b;
l = [NSNumber numberWithDouble:( 116 * var_Y ) - 16];
a = [NSNumber numberWithDouble:500 * ( var_X - var_Y )];
b = [NSNumber numberWithDouble:200 * ( var_Y - var_Z )];
return([NSArray arrayWithObjects:l,a,b, nil]);
}
And here is the input/output of the formula at the two log points in the method:
convertXYZtoLAB... var_X: 0.345393 var_Y: 0.502066 var_Z: 0.308145
convertXYZtoLAB... var_X: 1.000000 var_Y: 1.000000 var_Z: 1.000000
You're probably getting integer division instead of floating point division. Try 1.0/3
instead of 1/3
, etc. 1/3
rounds down to zero in integer math.