Objective-C RGB到HSB [英] Objective-C RGB to HSB

查看:261
本文介绍了Objective-C RGB到HSB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个颜色FF0000,它是红色的。找到一个更深的颜色很容易,我只是键入可能CC而不是FF,但让我们说,我有颜色AE83FC,这是一个复杂的颜色,怎么会自动找到一个更亮或更黑的版本? / p>

我想这样做的简单方法是将RGB转换为HSB [Hue,饱和度,亮度]



我如何在Objective-C中这样做?



假设我有一个RGB,它是:1.0,0.0,0.0。

  CGFloat r = 1.0; 
CGFloat g = 0.0;
CGfloat b = 0.0;

如何将它转换为HSB,然后转换颜色,使其返回RGB可以使用CGContextRGBSetFillColor?



是否有任何HSB功能?



:)

解决方案

首先,请注意,三个数字不描述颜色,三个数字与颜色空间一起。 RGB不是色彩空间,它是所谓的色彩模型。有很多与RGB模型的颜色空间。所以((1,0,0),sRGB)是不同于((1,0,0),Adobe RGB)的颜色。你知道字符串编码如何工作,其中一个字节本身不是一个字符串?这很像。它也是类似的,你是一个要求麻烦每当你想看看组件的值,因为这是一个机会,搞乱了色彩空间处理。



对不起,不能帮助自己。无论如何,我会回答这个问题,如果你的原始颜色是((1,0,0),Generic RGB)。

  NSColor * color = [NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:1]; 
NSLog(@hue!%g saturation!%g brightness!%g,[color hueComponent],[color saturationComponent],[color brightnessComponent]);
pre>

另一方面,

  NSColor * color = [NSColor colorWithCalibratedHue:h saturation:s brightness:b alpha:1]; 
NSLog(@red!%g green!%g blue!%g,[color redComponent],[color greenComponent] ]);

这些fooComponent方法不适用于所有颜色,只有两个特定颜色空间,请参阅docs。你已经知道你是好的,如果你自己用上面的方法创建颜色。如果你有一个未知来源的颜色,你可以(试图)将其转换为一个颜色空间,您可以使用 - [NSColor colorUsingColorspaceName:] 。 / p>

Let's say I've got the colour FF0000, which is red. Finding a darker colour is easy, I just type maybe CC instead of the FF, but let's say I've got the colour AE83FC, which is a complicated colour, how the heck would I find a lighter or darker version of it automatically?

I figured the easy way to do this is to convert my RGB to HSB [Hue, Saturation, Brightness]

How would I do that in Objective-C?

Let's say I've got a RGB which is: 1.0, 0.0, 0.0. That's red.

CGFloat r = 1.0;
CGFloat g = 0.0;
CGfloat b = 0.0;

How would I convert that to HSB and then transform the colors and make it go back to RGB to I can use CGContextRGBSetFillColor?

Are there any HSB functions?

Please help. :)

解决方案

First, please be aware that three numbers don't describe a color, three numbers together with a colorspace do. RGB isn't a colorspace, it's what's called a color model. There are lots of colorspaces with the RGB model. So ((1,0,0), sRGB) is a different color than ((1,0,0), Adobe RGB). Are you aware of how string encodings work, where a bunch of bytes by itself is not a string? It's a lot like that. It's also similar in that you're kind of asking for trouble whenever you want to look at the component values, because it's an opportunity for messing up the colorspace handling.

Sorry, cannot help myself. Anyway, I'll answer the question as if your original color was ((1,0,0), Generic RGB).

NSColor *color = [NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:1];
NSLog(@"hue! %g saturation! %g brightness! %g, [color hueComponent], [color saturationComponent], [color brightnessComponent]);

and on the other hand,

NSColor *color = [NSColor colorWithCalibratedHue:h saturation:s brightness:b alpha:1];
NSLog(@"red! %g green! %g blue! %g, [color redComponent], [color greenComponent], [color blueComponent]);

These fooComponent methods do not work with all colors, only those in two specific colorspaces, see docs. You already know you're good if you created the colors yourself with the methods above. If you have a color of unknown provenance, you can (attempt to) convert it to a colorspace in which you can use those component methods with -[NSColor colorUsingColorspaceName:].

这篇关于Objective-C RGB到HSB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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