标签:
- (NSString *)getCIELABString:(UIColor *)originColor {
// Method provided by the Colours class extension
NSDictionary *cieDict = [self getCIE_LabArrayByColor:originColor ];
return [NSString stringWithFormat:@"(%0.2f, %0.2f, %0.2f)",
[cieDict[@"CIE_L"] floatValue],
[cieDict[@"CIE_A"] floatValue],
[cieDict[@"CIE_B"] floatValue]];
}
#pragma mark - LAB from Color
- (NSDictionary *)getCIE_LabArrayByColor:(UIColor *)originColor {
// Convert Color to XYZ format first
NSDictionary *rgba = [self getRGBDictionaryByColor:originColor];
CGFloat R = [rgba[@"R"] floatValue];
CGFloat G = [rgba[@"G"] floatValue];
CGFloat B = [rgba[@"B"] floatValue];
// Create deltaR block
void (^deltaRGB)(CGFloat *R);
deltaRGB = ^(CGFloat *r) {
*r = (*r > 0.04045) ? pow((*r + 0.055)/1.055, 2.40) : (*r/12.92);
};
deltaRGB(&R);
deltaRGB(&G);
deltaRGB(&B);
CGFloat X = R*41.24 + G*35.76 + B*18.05;
CGFloat Y = R*21.26 + G*71.52 + B*7.22;
CGFloat Z = R*1.93 + G*11.92 + B*95.05;
// Convert XYZ to L*a*b*
X = X/95.047;
Y = Y/100.000;
Z = Z/108.883;
// Create deltaF block
void (^deltaF)(CGFloat *f);
deltaF = ^(CGFloat *f){
*f = (*f > pow((6.0/29.0), 3.0)) ? pow(*f, 1.0/3.0) : (1/3)*pow((29.0/6.0), 2.0) * *f + 4/29.0;
};
deltaF(&X);
deltaF(&Y);
deltaF(&Z);
NSNumber *L = @(116*Y - 16);
NSNumber *a = @(500 * (X - Y));
NSNumber *b = @(200 * (Y - Z));
return @{@"CIE_L":L,
@"CIE_A":a,
@"CIE_B":b,
@"CIE_ALPHA":rgba[@"A"]};
}
/**
* 获取UIColor对象的RGB值。
*
* @return 包含rgb值的字典对象。
*/
- (NSDictionary *)getRGBDictionaryByColor:(UIColor *)originColor
{
CGFloat r=0,g=0,b=0,a=0;
if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[originColor getRed:&r green:&g blue:&b alpha:&a];
}
else {
const CGFloat *components = CGColorGetComponents(originColor.CGColor);
r = components[0];
g = components[1];
b = components[2];
a = components[3];
}
return @{@"R":@(r),
@"G":@(g),
@"B":@(b),
@"A":@(a)};
}
标签:
原文地址:http://blog.csdn.net/shouqiangwei/article/details/42455841