local H={}function H.rgbToHsv(H,V,b)local c=H/255 local y=V/255 local E=b/255 local s=math.max(c,y,E)local k=math.min(c,y,E)local X,R,u if s==k then X=0 elseif s==c then X=(60*((y-E)/(s-k)))%360 elseif s==y then X=(60*((E-c)/(s-k))+120)%360 elseif s==E then X=(60*((c-y)/(s-k))+240)%360 end if s==0 then R=0 else R=1-k/s end u=s return X,R,u end function H.rgbToHex(H,V,b)local c=string.format("\037\048\050\088",H)local y=string.format("\037\048\050\088",V)local E=string.format("\037\048\050\088",b)return"\035"..(c..(y..E))end function H.hsvToRgb(H,V,b)local c=b*V local y=H/60 local E=c*(1-math.abs(y%2-1))local s,k,X if y>=0 and y<1 then s,k,X=c,E,0 elseif y>=1 and y<2 then s,k,X=E,c,0 elseif y>=2 and y<3 then s,k,X=0,c,E elseif y>=3 and y<4 then s,k,X=0,E,c elseif y>=4 and y<5 then s,k,X=E,0,c elseif y>=5 and y<6 then s,k,X=c,0,E else s,k,X=0,0,0 end local R=b-c s=(s+R)*255 k=(k+R)*255 X=(X+R)*255 return math.floor(s+.5),math.floor(k+.5),math.floor(X+.5)end function H.hsvToHex(V,b,c)local y,E,s=H.hsvToRgb(V,b,c)return H.rgbToHex(y,E,s)end function H.hexToRgb(H)local V=H:sub(2,3)local b=H:sub(4,5)local c=H:sub(6,7)local y=tonumber(V,16)local E=tonumber(b,16)local s=tonumber(c,16)return y,E,s end function H.hexToHsv(V)local b,c,y=H.hexToRgb(V)return H.rgbToHsv(b,c,y)end return H
--------------------------------------------------------------------------------------------------------------------------------
--[[                                             Copyright (C) Inertia Lighting                                             ]]--
--------------------------------------------------------------------------------------------------------------------------------

local Color = {}

--------------------------------------------------------------------------------------------------------------------------------

--- Converts RGB color values to HSV color values
--- @param red number (0-255)
--- @param green number (0-255)
--- @param blue number (0-255)
--- @returns hue number (0-360), saturation number (0-1), value number (0-1)
function Color.rgbToHsv(red, green, blue)
    local r = red / 255
    local g = green / 255
    local b = blue / 255

    local maxVal = math.max(r, g, b)
    local minVal = math.min(r, g, b)

    local hue, saturation, value

    if maxVal == minVal then
        hue = 0
    elseif maxVal == r then
        hue = (60 * ((g - b) / (maxVal - minVal))) % 360
    elseif maxVal == g then
        hue = (60 * ((b - r) / (maxVal - minVal)) + 120) % 360
    elseif maxVal == b then
        hue = (60 * ((r - g) / (maxVal - minVal)) + 240) % 360
    end

    if maxVal == 0 then
        saturation = 0
    else
        saturation = 1 - (minVal / maxVal)
    end

    value = maxVal

    return hue, saturation, value
end

--- Converts RGB color values to hex string
--- @param red number (0-255)
--- @param green number (0-255)
--- @param blue number (0-255)
--- @returns hexColorString string
function Color.rgbToHex(red, green, blue)
    local r = string.format("%02X", red)
    local g = string.format("%02X", green)
    local b = string.format("%02X", blue)

    return "#" .. r .. g .. b
end

--- Converts HSV color values to RGB color values
--- @param hue number (0-360)
--- @param saturation number (0-1)
--- @param value number (0-1)
--- @returns red number (0-255), green number (0-255), blue number (0-255)
function Color.hsvToRgb(hue, saturation, value)
    local chroma = value * saturation
    local huePrime = hue / 60
    local x = chroma * (1 - math.abs((huePrime % 2) - 1))
    local red, green, blue

    if huePrime >= 0 and huePrime < 1 then
        red, green, blue = chroma, x, 0
    elseif huePrime >= 1 and huePrime < 2 then
        red, green, blue = x, chroma, 0
    elseif huePrime >= 2 and huePrime < 3 then
        red, green, blue = 0, chroma, x
    elseif huePrime >= 3 and huePrime < 4 then
        red, green, blue = 0, x, chroma
    elseif huePrime >= 4 and huePrime < 5 then
        red, green, blue = x, 0, chroma
    elseif huePrime >= 5 and huePrime < 6 then
        red, green, blue = chroma, 0, x
    else
        red, green, blue = 0, 0, 0
    end

    local m = value - chroma

    red = (red + m) * 255
    green = (green + m) * 255
    blue = (blue + m) * 255

    return math.floor(red + 0.5), math.floor(green + 0.5), math.floor(blue + 0.5)
end

--- Converts HSV color values to hex string
--- @param hue number (0-360)
--- @param saturation number (0-1)
--- @param value number (0-1)
--- @returns hexColorString string
function Color.hsvToHex(hue, saturation, value)
    local red, green, blue = Color.hsvToRgb(hue, saturation, value)

    return Color.rgbToHex(red, green, blue)
end

--- Converts hex string to RGB color values
--- @param hexColorString string
--- @returns red number (0-255), green number (0-255), blue number (0-255)
function Color.hexToRgb(hexColorString)
    local redHex = hexColorString:sub(2, 3)
    local greenHex = hexColorString:sub(4, 5)
    local blueHex = hexColorString:sub(6, 7)

    local red = tonumber(redHex, 16)
    local green = tonumber(greenHex, 16)
    local blue = tonumber(blueHex, 16)

    return red, green, blue
end

--- Converts hex string to HSV color values
--- @param hexColorString string
--- @returns hue number (0-360), saturation number (0-1), value number (0-1)
function Color.hexToHsv(hexColorString)
    local red, green, blue = Color.hexToRgb(hexColorString)

    return Color.rgbToHsv(red, green, blue)
end

--------------------------------------------------------------------------------------------------------------------------------

return Color
