Uicolor 中 255 背后的逻辑

发布于 2024-09-25 15:57:53 字数 105 浏览 1 评论 0原文

我们一般在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 技术交流群。

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

发布评论

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

评论(4

找回味觉 2024-10-02 15:57:53

大多数核心图形 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).

小伙你站住 2024-10-02 15:57:53

您因 2 个原因而下降 255.0

  1. 您需要一个小数来表示该通道在 0-1 范围内的百分比
  2. 如果您执行 143/255,则将是整数除法,这将给出整数结果

You are diving by 255.0 for 2 reasons

  1. You need a decimal that represents the percentage of that channel in the range 0-1
  2. If you did 143/255 it would be integer division, which would give an integer result
萌能量女王 2024-10-02 15:57:53

这是因为大多数人习惯于在 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.

懒的傷心 2024-10-02 15:57:53

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.

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