Uicolor 中 255 背后的逻辑
我们一般在UIColor
setcolor RGB argument中写143.0/255.0。谁能告诉我除以 255.0 背后的逻辑。我有一些知识,但我想它是不完整的。
We generally write 143.0/255.0 in UIColor
setcolor RGB arguements . Can anybody pl tel me the logic behind Dividing by 255.0 . I have some knowledge but i guess it is incomplete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大多数核心图形 API 期望 RGB 颜色分量在 0.0 到 1.0 的范围内。然而,许多计算机调色板和 Mac 输出设备期望 RGB 颜色分量能够以 8 位值表示,当表示为整数时,其范围在 0 到 255 之间。
如果您要从包含 8 位的表中查找颜色,位组件值(例如 #RRGGBB 的网页颜色),那么您需要将值从 0 到 255 范围缩放到 0 到 1.0 范围,这是通过除以 255.0f 来完成的(以浮点形式表示,因此您不需要t 最终截断为 0)。
Most Core Graphic APIs expect the RGB color components to be in the range of 0.0 to 1.0. However many computer color palettes and Mac output devices expect RGB color components to be representable in 8 bit values, which, when expressed as integers, are in the range of 0 to 255.
If you are looking up a color from a table with 8-bit component values (e.g. a web color of #RRGGBB), then you need to scale the values from the range 0 to 255 to the range 0 to 1.0, which is done by dividing by 255.0f (in floating point, so you don't end up truncating to 0).
您因 2 个原因而下降 255.0
You are diving by 255.0 for 2 reasons
这是因为大多数人习惯于在 0-255 的范围内定义颜色。因为参数要求参数从 0-1 重新分配颜色,所以需要 /255.0。您可以轻松指定 0.5 之类的值来表示 128.0/255.0。
请注意,这个问题是关于颜色表示的,并不特定于 iPhone。
This is because most people are accustomed to defining colors on a scale from 0-255. Because the parameter demand arguments to reperest color from 0-1, the /255.0 is needed. You could just as easily specify something like 0.5 to mean 128.0/255.0.
Note that this question is about color reperesentation and inspecific to iPhone.
UIColor 没有方法来创建人们习惯的 0-255 rgb 范围之外的颜色对象(主要来自网页设计)。
UIColor 最简单的 RGB 颜色初始化程序对每个通道使用 0.0 - 1.0 颜色范围。
因此,如果您有基于 0 - 255 的颜色值,则将其除以 255.0 会得到一个介于 0.0 和 1.0 之间的值:
255.0 / 255.0 = 1.0
0.0 / 255.0 = 0.0
这只是无损转换它们的最简单方法,因此您可以调整基于255的值,而无需每次都自己重新计算值。
UIColor doesn't have methods to create color Objects out the 0-255 rgb ranges people are used to (mostly from webdesign).
UIColor's easiest initializer for rgb colors use a 0.0 - 1.0 color range for each channel.
So, if you have a a 0 - 255 based color value, dividing it with 255.0 gives you a value between 0.0 and 1.0:
255.0 / 255.0 = 1.0
0.0 / 255.0 = 0.0
It's just the easiest way to convert them non-destructive, so you can adjust the 255-based values without recalculate the values everytime by yourself.