Objective-C RGB 转 HSB
假设我的颜色为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请注意三个数字不能描述颜色,三个数字和一个颜色空间可以描述颜色。 RGB 不是色彩空间,而是所谓的色彩模型。 RGB 模型有很多色彩空间。因此 ((1,0,0), sRGB) 与 ((1,0,0), Adobe RGB) 是不同的颜色。您是否知道字符串编码是如何工作的,其中一堆字节本身不是字符串?很多都是这样的。它的相似之处还在于,每当您想要查看组件值时,您都会遇到麻烦,因为这是一个搞乱色彩空间处理的机会。
抱歉,我无法自拔。不管怎样,我会回答这个问题,就像你的原始颜色是((1,0,0),通用RGB)一样。
另一方面,
这些 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).
and on the other hand,
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:]
.没有必要为此一路跑到HSB。如果你想找到一种颜色的更深版本,只需将每个 R、G 和 B 分量乘以 0 到 1 之间的数字,你就会得到相同的颜色,但更暗。
示例:AE83FC 的一半强度:
以同样的方式,您可以通过乘以任何大于 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:
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.