Ste*_*her 17 iphone core-graphics cgcolor uicolor cgcolorspace
是否有Cocoa Touch方式将颜色从一个颜色空间转换为另一个颜色空间?
在此代码的末尾:
UIColor *grey = [UIColor colorWithWhite: 0.5 alpha: 1.0];
CGColorRef greyRef = [grey CGColor];
int x = CGColorGetNumberOfComponents(greyRef);
Run Code Online (Sandbox Code Playgroud)
...... 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;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以在灰度颜色空间中寻找颜色并进行转换.但这只能解决一个案例.从这个问题来看,我认为HSB也是如此.但是我想在这里编写一些代码而不再考虑它,这意味着不仅要支持现在的色彩空间,还要支持苹果未来可以想象的任何东西.我运气不好吗?
我不确定如何自动转换它们,但要识别不同的颜色空间,您可以CGColorSpaceModel
从颜色中获取CGColorSpaceRef
:
UIColor* color = some color;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([color CGColor]);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
Run Code Online (Sandbox Code Playgroud)
然后你可以比较中colorSpaceModel
定义的常量CoreGraphics/CGColorSpace.h
.UIColor getRed:green:blue:alpha
适用于kCGColorSpaceModelRGB
,而getWhite:alpha
适用于kCGColorSpaceModelMonochrome
.
请注意,使用UIColor
它创建的colorWithHue:saturation:brightness:alpha:
实际上将在RGB颜色空间中.
归档时间: |
|
查看次数: |
15554 次 |
最近记录: |