【问题标题】:finding the closest web safe color if I have a palette如果我有调色板,找到最接近的网络安全颜色
【发布时间】:2011-12-06 05:21:22
【问题描述】:

如何获取 r,g,b 值并将它们与网络安全调色板进行比较以找到 r,g,b 值的最佳匹配?

有这个: What is the best algorithm for finding the closest color in an array to another color?

但我不认为这是我需要的。我只需要将 r,g,b 与网络安全颜色进行比较,然后确定网络安全颜色是否是最佳选择。

Edit1:已删除

编辑2: 这是我到目前为止所拥有的。

local r, g, b = HSV2RGB(h, s, v)
local dither = copy(WEB_SAFE)
local lmod
for i, v in ipairs(dither) do
        local r2, g2, b2 = Color2RGBA(v)
        local hh, ss, vv = RGB2HSV(r2, g2, b2)
        local a = hh - h
        local b = ss - s
        local c = vv - v
        local mod = a*a + b*b + c*c
        if not lmod or mod < lmod then
                lmod = mod
                r, g, b = r2, g2,b2
        end
end
texture:SetBackgroundColor(r, g, b)

编辑 3: 这是它应该看起来的样子吗?

h=1 到 360,步长 5 pt,s=1 到 100,v = 89

【问题讨论】:

  • 这正是您所需要的。只需将所有网络安全颜色放入一个数组中,将您的颜色与数组中的每种颜色一一比较,然后取差异最小的颜色。如果您愿意,我可以发布示例代码。
  • 是否需要取每个 r,g,b 值或 h,s,v 或 32 位数字的差值?
  • 这在我看来像 Lua 代码,所以我将其标记为这样。
  • 我很高兴你得到了答案,但我真的很好奇:为什么在 2011 年需要网络安全颜色?现在你不能依赖至少 16 位的一切吗?

标签: colors lua color-palette web-safe-colors


【解决方案1】:

我不确定 HSV 是否是执行计算的最佳色彩空间——它也是圆柱体,而不是立方体,因此您的距离公式(在 RGB 中可以正常工作)会产生不适合 HSV 的结果.

无论如何,Web safe palette 本身就是一个简单的 RGB 颜色立方体,每个组件有六个可能的值 (0-5)。您甚至不需要做一些复杂的事情,比如通过迭代从输入颜色派生 Web 安全颜色:只需为每个颜色分量(R、G、B)独立确定适当的 Web 安全值。

假设您的 RGB 分量值范围为 0..255:

local max_color_component_value = 255
local quantum = max_color_component_value / 5

r = quantum * math.floor((r + (quantum / 2)) / quantum)
g = quantum * math.floor((g + (quantum / 2)) / quantum)
b = quantum * math.floor((b + (quantum / 2)) / quantum)

如果使用其他范围,请适当调整 max_color_component_value。

【讨论】:

  • 明白了。谢谢。这些绝对是网络安全的?
  • @Scott 关闭,非常接近。您的输出颜色分量值比应有的值小一(254 而不是 255,等等)。公式中实际的 max_color_component_value 是多少?
  • 此外,算法似乎只运行了彩虹矩形,而不是整个图像。这原本是一个平滑的渐变,“海报化”是个问题吗?
  • 整个图像的饱和度为 100。我打算提供一个调整饱和度的选项。我不确定整个算法是什么。这正是我想出的。
  • max_color_compent_value 是 255。我尝试使用 1,因为我在 1 和 0 之间的范围内工作,但顶行不正确。
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2012-04-06
  • 2019-08-17
  • 2019-04-27
相关资源
最近更新 更多