如何将颜色从一个颜色空间转换为另一个颜色空间 [英] How to convert colors from one color space to another?

查看:223
本文介绍了如何将颜色从一个颜色空间转换为另一个颜色空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Cocoa Touch方式将颜色从一个颜色空间转换为另一个颜色空间?



在此代码的末尾:

  UIColor * gray = [UIColor colorWithWhite:0.5 alpha:1.0]; 
CGColorRef greyRef = [灰色CGColor];
int x = CGColorGetNumberOfComponents(greyRef);

... x 是2. / p>

我需要这个的原因是我正在尝试将颜色复制到 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);
免费(theComponents);
返回渐变;
}

我知道我可以在灰度颜色空间中查找颜色并进行转换。但这只能解决一个案例。通过查看此问题,我认为HSB也会得到处理。但是我想在这里编写一些代码而不再考虑它,这意味着不仅要支持现在的色彩空间,还要支持苹果未来可以想象的任何东西。我运气不好吗?

解决方案

我不确定如何自动转换它们,但要识别不同的颜色空间你可以从颜色的 CGColorSpaceRef 获取 CGColorSpaceModel

  UIColor * color =某种颜色; 
CGColorSpaceRef colorSpace = CGColorGetColorSpace([color CGColor]);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);

然后你可以将 colorSpaceModel 与常量进行比较在 CoreGraphics / CGColorSpace.h 中定义。 UIColor的 getRed:绿色:蓝色:alpha 适用于 kCGColorSpaceModelRGB ,而 getWhite:alpha 适用于 kCGColorSpaceModelMonochrome



请注意 UIColor 使用 colorWithHue创建的:饱和度:亮度:alpha:实际上将在RGB颜色空间中。


Is there a Cocoa Touch way to convert colors from one color space to another?

At the end of this code:

UIColor *grey = [UIColor colorWithWhite: 0.5 alpha: 1.0];
CGColorRef greyRef = [grey CGColor];
int x = CGColorGetNumberOfComponents(greyRef);

...x is 2.

The reason I need this is I'm trying to copy colors to a list of color components for CGGradientCreateWithColorComponents, which needs all colors in a single colorspace. The problem is that grey is in the grayscale colorspace, rather than the RGB one. (The name escapes me at the moment, but it isn't important.)

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;
}

I know I can look for colors in the greyscale color space and convert them. But that only solves one case. From looking at this question, I think HSB is handled as well. But I'd like to write some code here and never think about it again, which means supporting not just the color spaces that are there now but anything Apple could conceivably add in the future. Am I out of luck?

解决方案

I'm not sure how to automatically convert them, but to identify different color spaces you can get the CGColorSpaceModel from the color's CGColorSpaceRef:

UIColor* color = some color;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([color CGColor]);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);

Then you can compare colorSpaceModel with the constants defined in CoreGraphics/CGColorSpace.h. UIColor's getRed:green:blue:alpha works for kCGColorSpaceModelRGB, whereas getWhite:alpha works for kCGColorSpaceModelMonochrome.

Note that a UIColor that was created with colorWithHue:saturation:brightness:alpha: will actually be in the RGB color space.

这篇关于如何将颜色从一个颜色空间转换为另一个颜色空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆