Objective-C RGB 转 HSB

发布于 2024-08-27 13:41:10 字数 393 浏览 6 评论 0原文

假设我的颜色为 FF0000,即红色。找到较深的颜色很容易,我只需输入 CC 而不是 FF,但假设我有颜色 AE83FC,这是一种复杂的颜色,我到底如何自动找到它的较浅或较深的版本?

我认为最简单的方法是将 RGB 转换为 HSB [色调、饱和度、亮度]

在 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功能吗?

请帮忙。 :)

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. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-09-03 13:41:10

首先,请注意三个数字不能描述颜色,三个数字和一个颜色空间可以描述颜色。 RGB 不是色彩空间,而是所谓的色彩模型。 RGB 模型有很多色彩空间。因此 ((1,0,0), sRGB) 与 ((1,0,0), Adob​​e RGB) 是不同的颜色。您是否知道字符串编码是如何工作的,其中一堆字节本身不是字符串?很多都是这样的。它的相似之处还在于,每当您想要查看组件值时,您都会遇到麻烦,因为这是一个搞乱色彩空间处理的机会。

抱歉,我无法自拔。不管怎样,我会回答这个问题,就像你的原始颜色是((1,0,0),通用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]);

另一方面,

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

这些 fooComponent 方法不适用于所有颜色,仅适用于两个特定颜色空间中的颜色,请参阅文档。如果您使用上述方法自己创建了颜色,那么您已经知道自己很擅长。如果您有未知来源的颜色,您可以(尝试)将其转换为颜色空间,在该颜色空间中您可以通过 -[NSColor colorUsingColorspaceName:] 使用这些组件方法。

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:].

夏尔 2024-09-03 13:41:10

没有必要为此一路跑到HSB。如果你想找到一种颜色的更深版本,只需将每个 R、G 和 B 分量乘以 0 到 1 之间的数字,你就会得到相同的颜色,但更暗。

示例:AE83FC 的一半强度:

  {0xAE, 0x83, 0xFC} * 0.5 =
  {174, 131, 252} * 0.5 =
  {87, 65, 126} =
  {0x57, 0x41, 0x7E} => 57417E

以同样的方式,您可以通过乘以任何大于 1 的值来获得更亮的版本。每个分量的值不能大于 255,因此当发生这种情况时,您需要将其限制为 255。这意味着颜色不会完全是相同颜色的更亮版本,但可能足够接近您的目的。

There is no need to go all the way to HSB for this. If you want to find a darker version of a color, just multiply each R, G and B component with a number between 0 and 1, and you will get the same color, but darker.

Example: half intensity of AE83FC:

  {0xAE, 0x83, 0xFC} * 0.5 =
  {174, 131, 252} * 0.5 =
  {87, 65, 126} =
  {0x57, 0x41, 0x7E} => 57417E

In the same way, you can obtain brighter versions by multiplying with anything >1. The value of each component can't be larger than 255, so when that happens, you need to limit it to 255. That means that the color wont be exactly a brighter version of the same color, but probably close enough for your purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文