Вопрос

How do I take r,g,b values and compare them to a websafe color palette to find the best match for the r,g,b value?

There's this one: What is the best algorithm for finding the closest color in an array to another color?

But I don't think it's what I need. I just need to compare an r,g,b with a websafe color and find out if the websafe color is the best choice.

Edit1: deleted

Edit2: This is what I have so far.

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)

Edit 3: Is this what it's supposed to look like?

http://imgur.com/LwFGQ

h=1 through 360 at 5 pt steps, s=1 through 100, v = 89

Это было полезно?

Решение

I'm not sure that HSV is the best color-space to perform the calculation in -- also it's a cylinder, not a cube, so your distance formula (which would work fine in RGB) would produce inappropriate results for HSV.

In any case, the Web safe palette is itself a simple RGB color cube, with six possible values (0-5) for each component. You shouldn't even need to do something as complex as iterating to derive a Web safe color from an input color: just determine the appropriate Web safe value for each color component (R, G, B) independently.

On the rash assumption that your RGB component values range from 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)

If some other range is used, adjust max_color_component_value appropriately.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top