0

I would like to convert a HSL value into an RBG so that i can easily generate random colours in Love2D, however i am having trouble finding how to do this. I did check wikipedia but really didn't understand anything, and i was browsing stack overflow but didn't find anything for Lua. If possible i would want the function to enter 3 values and return a table with table.r, table.b, table.g, so that i can then call function().r etc etc. How can i do this?

Stack overflow isn't letting me post this without adding more information so lets see, the reason i don't want to randomise RBG values is that i only to change the hue, not affecting saturation/lightness. I think HSL would be the perfect way to do this as i can just randomise a hue colour to then convert it into RBG, RBG being the way colours are defined in Love2D.

Dot32
  • 1

1 Answers1

0

I've saw the function you want written in https://github.com/unek/loveframes-snap-theme/blob/master/color.lua

Here's the parts you want extracted from it:

-- Needed for hsl2rgb to work
local function hue2rgb(p, q, t)
    if t < 0   then t = t + 1 end
    if t > 1   then t = t - 1 end
    if t < 1/6 then return p + (q - p) * 6 * t end
    if t < 1/2 then return q end
    if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
    return p
end

-- Here's the function you want --

local function hsl2rgb(h, s, l)
    local r, g, b

    local h = h / 360

    if s == 0 then
        r, g, b = l, l, l
    else
        local q = (l < 0.5) and l * (1 + s) or l + s - l * s
        local p = l * 2 - q

        r = hue2rgb(p, q, h + 1/3)
        g = hue2rgb(p, q, h)
        b = hue2rgb(p, q, h - 1/3)
    end

    return {r=r, g=g, b=b}
end

Please note that the RGB values will be in range [0-1]

Rami Sabbagh
  • 125
  • 9
  • `love.graphics.setColor(hsl2rgb(167, 65, 45).r, hsl2rgb(167, 65, 45).g, hsl2rgb(167, 65, 45).b, 1)` i got the error Error _main.lua:36: attempt to call global 'hsl2rgb' (a nil value)_ , i tried replacing .r .g and .b with [1] [2] and [3] but it still gave the same error – Dot32 Aug 08 '20 at 13:03
  • `attempt to call global` The code I've provided defines the functions as `local`, you have to place them at the top of the `main.lua` file, and if you want to store them in a separate file, remove the `local` in `local function hsl2rgb` and it should be made a global (be sure to require the separate file in the files you wish to use). – Rami Sabbagh Aug 08 '20 at 13:27
  • ah yes thanks so much, but now i am getting the error _attempt to index a number value_ , is the function not returning a table ? im using it like 'hsl2rgb(167, 65, 45).r' – Dot32 Aug 08 '20 at 14:23
  • The original code in GitHub didn't return a table, but in the copy here I've modified it to return a table in the format you wanted. Can you make sure your `hsl2rgb` ends with `return {r=r,g=g,b=b}` ? – Rami Sabbagh Aug 08 '20 at 15:02
  • Yes, it ends with `return {r=r, g=g, b=b}` – Dot32 Aug 09 '20 at 02:41
  • Dunno, if you can upload your code somewhere so I can inspect it. – Rami Sabbagh Aug 09 '20 at 15:02