简单的问题,很可能复杂的答案更多:
我如何从一个的UIColor对象(我知道的RGB值,如果有帮助)获得CMYK和Lab值?
我发现这对于越来越CMYK值,但是,我不能得到任何准确的数值了它尽管它是无处不在的,我听说这是不是一个伟大的片段。
CGFloat rgbComponents[4];
[color getRed:&rgbComponents[0] green:&rgbComponents[1] blue:&rgbComponents[2] alpha:&rgbComponents[3]];
CGFloat k = MIN(1-rgbComponents[0], MIN(1-rgbComponents[1], 1-rgbComponents[2]));
CGFloat c = (1-rgbComponents[0]-k)/(1-k);
CGFloat m = (1-rgbComponents[1]-k)/(1-k);
CGFloat y = (1-rgbComponents[2]-k)/(1-k);
对于基于ICC的色彩转换,你可以使用一点颜色管理系统 。 (我刚才说从下载存档的所有.c和.h文件到iOS Xcode项目,它编译和运行没有问题,下面的代码。)
备注:RGB和CMYK是设备相关颜色空间,实验室是一个与设备无关的色彩空间。 因此,为了从RGB转换为Lab,你必须选择一个与设备无关的(或“校准”)为转换的RGB颜色空间,例如sRGB的。
小CMS附带了sRGB的内置配置文件和Lab颜色空间。 从sRGB进行实验室的转换是这样的:
创建色彩变换:
cmsHPROFILE rgbProfile = cmsCreate_sRGBProfile();
cmsHPROFILE labProfile = cmsCreateLab4Profile(NULL);
cmsHTRANSFORM xform = cmsCreateTransform(rgbProfile, TYPE_RGB_FLT, labProfile,
TYPE_Lab_FLT,
INTENT_PERCEPTUAL, 0);
cmsCloseProfile(labProfile);
cmsCloseProfile(rgbProfile);
转换颜色:
float rgbValues[3];
// fill rgbValues array with input values ...
float labValues[3];
cmsDoTransform(xform, rgbValues, labValues, 1);
// labValues array contains output values.
处理颜色转换的:
cmsDeleteTransform(xform);
当然,转型将创建只有一次,用于所有的颜色转换。
对于RGB到CMYK转换,你也可以用小CMS,但你必须要提供的ICC-配置文件,例如一个从免费的Adobe下载页面ICC配置文件下载适用于Mac OS 。
代码示例为RGB到CMYK转换:
float rgb[3]; // fill with input values (range 0.0 .. 1.0)
float cmyk[4]; // output values (range 0.0 .. 100.0)
cmsHPROFILE rgbProfile = cmsCreate_sRGBProfile();
// The CMYK profile is a resource in the application bundle:
NSString *cmykProfilePath = [[NSBundle mainBundle] pathForResource:@"YourCMYKProfile.icc" ofType:nil];
cmsHPROFILE cmykProfile = cmsOpenProfileFromFile([cmykProfilePath fileSystemRepresentation], "r");
cmsHTRANSFORM xform = cmsCreateTransform(rgbProfile, TYPE_RGB_FLT, cmykProfile,
TYPE_CMYK_FLT,
INTENT_PERCEPTUAL, 0);
cmsCloseProfile(cmykProfile);
cmsCloseProfile(rgbProfile);
cmsDoTransform(xform, rgb, cmyk, 1);
cmsDeleteTransform(xform);
要获得LAB值,您需要将RGB值转换为XYZ值,然后可以转换成RGB值。
- (NSMutableArray *) convertRGBtoLABwithColor: (UIColor *)color
////make variables to get rgb values
CGFloat red3;
CGFloat green3;
CGFloat blue3;
//get rgb of color
[color getRed:&red3 green:&green3 blue:&blue3 alpha:nil];
float red2 = (float)red3*255;
float blue2 = (float)blue3*255;
float green2 = (float)green3*255;
//first convert RGB to XYZ
// same values, from 0 to 1
red2 = red2/255;
green2 = green2/255;
blue2 = blue2/255;
// adjusting values
if(red2 > 0.04045)
{
red2 = (red2 + 0.055)/1.055;
red2 = pow(red2,2.4);
} else {
red2 = red2/12.92;
}
if(green2 > 0.04045)
{
green2 = (green2 + 0.055)/1.055;
green2 = pow(green2,2.4);
} else {
green2 = green2/12.92;
}
if(blue2 > 0.04045)
{
blue2 = (blue2 + 0.055)/1.055;
blue2 = pow(blue2,2.4);
} else {
blue2 = blue2/12.92;
}
red2 *= 100;
green2 *= 100;
blue2 *= 100;
//make x, y and z variables
float x;
float y;
float z;
// applying the matrix to finally have XYZ
x = (red2 * 0.4124) + (green2 * 0.3576) + (blue2 * 0.1805);
y = (red2 * 0.2126) + (green2 * 0.7152) + (blue2 * 0.0722);
z = (red2 * 0.0193) + (green2 * 0.1192) + (blue2 * 0.9505);
//then convert XYZ to LAB
x = x/95.047;
y = y/100;
z = z/108.883;
// adjusting the values
if(x > 0.008856)
{
x = powf(x,(1.0/3.0));
} else {
x = ((7.787 * x) + (16/116));
}
if(y > 0.008856)
{
y = pow(y,(1.0/3.0));
} else {
y = ((7.787 * y) + (16/116));
}
if(z > 0.008856)
{
z = pow(z,(1.0/3.0));
} else {
z = ((7.787 * z) + (16/116));
}
//make L, A and B variables
float l;
float a;
float b;
//finally have your l, a, b variables!!!!
l = ((116 * y) - 16);
a = 500 * (x - y);
b = 200 * (y - z);
NSNumber *lNumber = [NSNumber numberWithFloat:l];
NSNumber *aNumber = [NSNumber numberWithFloat:a];
NSNumber *bNumber = [NSNumber numberWithFloat:b];
//add them to an array to return.
NSMutableArray *labArray = [[NSMutableArray alloc] init];
[labArray addObject:lNumber];
[labArray addObject:aNumber];
[labArray addObject:bNumber];
return labArray;
}