是否有一种 Cocoa Touch 方法可以将颜色从一种颜色空间转换为另一种颜色空间?
在这段代码的最后:
UIColor *grey = [UIColor colorWithWhite: 0.5 alpha: 1.0];
CGColorRef greyRef = [grey CGColor];
int x = CGColorGetNumberOfComponents(greyRef);
...x
是2。
我需要这个的原因是我试图将颜色复制到颜色组件列表中CGGradientCreateWithColorComponents
,这需要单个颜色空间中的所有颜色。问题是灰色在灰度色彩空间中,而不是在 RGB 色彩空间中。(这个名字现在让我忘记了,但这并不重要。)
CGGradientRef createGradient(NSInteger inCount, NSArray* inColors, CGFloat* inLocations) {
CGColorSpaceRef theColorspace = CGColorSpaceCreateDeviceRGB( );
size_t numberOfComponents = 4;
NSInteger colorSize = numberOfComponents * sizeof( CGFloat );
CGFloat *theComponents = malloc( inCount * colorSize );
CGFloat *temp = theComponents;
for ( NSInteger i = 0; i < inCount; i++ ) {
UIColor *theColor = [inColors objectAtIndex: i];
CGColorRef cgColor = [theColor CGColor];
const CGFloat *components = CGColorGetComponents( cgColor );
memmove( temp, components, colorSize );
temp += numberOfComponents;
}
CGGradientRef gradient = CGGradientCreateWithColorComponents( theColorspace,
theComponents, inLocations, inCount );
CGColorSpaceRelease( theColorspace );
free( theComponents );
return gradient;
}
我知道我可以在灰度色彩空间中寻找颜色并转换它们。但这只能解决一种情况。从这个问题来看,我认为 HSB 也得到了处理。但我想在这里写一些代码,以后再也不去想它了,这意味着不仅支持现在存在的色彩空间,还支持苹果未来可能添加的任何东西。我运气不好?