love.js icon indicating copy to clipboard operation
love.js copied to clipboard

RGB colors no longer consistent with Love2d

Open djflorio opened this issue 7 years ago • 6 comments

LÖVE version: 11.1 love.js commit: 38ab12b

I'm not entirely sure when/why this happened, but the setColor method in Love2d now takes values between 0-1, instead of 0-255 (I'm using LOVE 11.1).

So, the following code:

love.graphics.setColor(0.2, 0.8, 0.2)

...produces a lovely shade of green in Love2d. After converting to HTML5 with love.js, however, it appears black, because the browser interprets RGB values on a scale of 0-255.

djflorio avatar May 27 '18 21:05 djflorio

Did you ever figure out how to solve this? When I run my game I can hear it playing but everything is black so I am assuming I am having the same issue as you.

memersond avatar Jul 06 '18 17:07 memersond

@memersond the best solution for my case was to create a function for generating colors, which had a boolean value forWeb that I could set to true when I wanted to build the game for the web:

local genColor = function(r, g, b)
    local forWeb = false
    if forWeb then
        return {r, g, b}
    end
    return {r/255, g/255, b/255}
end

Then I could make colors by calling the function and providing the values on a 0-255 scale like normal:

limeGreen = genColor(21, 231, 41)

Leaving forWeb as false means the color values will be divided by 255, which converts them to the 0-1 scale that love2d now uses. Switching it to true will simply return whatever values you put in, keeping it in the 0-255 scale, for when you want to build for the web using love.js.

Not a perfect solution, but at least it makes it so you only have to change a single variable depending on whether you're building for desktop or for HTML5. Hope this helps!

djflorio avatar Jul 06 '18 18:07 djflorio

This is not the only difference between Love 0.10.0 and 11.1, although it is a major one. It would probably be a good idea to develop your game in 0.10.0 instead if you want to ensure compatibility with love.js.

agargara avatar Nov 21 '18 08:11 agargara

Usually I have a constant that is set in the configurations according to the love version like:

local major, minor, revision, codename
major, minor, revision, codename = love.getVersion()

-- Colors in love 0.10 where between 0-255 but now are 0-1
COLOR_SCALE = 1
if major < 1 then
    COLOR_SCALE = 255
end

And when I want some color I just do

love.graphics.setColor(0.8 * COLOR_SCALE, 0.2 * COLOR_SCALE, 0.5 * COLOR_SCALE)

drmargarido avatar Feb 12 '19 14:02 drmargarido

Another option is to wrap lg.setColor on startup:

if major < 1 then
  local _setColor = love.graphics.setColor
  love.graphics.setColor = function(...)
    local vals = { ... }
    for i = 1, #vals do
      vals[i] = 255 * val[i]
    end
    return _setColor(unpack(vals))
  end
end

s-ol avatar Feb 12 '19 16:02 s-ol

Is this issue still present on the 0.11 branch? I'm new to Love and this seems like a common change I keep hitting with older libraries. I was wondering if it'd be worth investigating upgrading Love.js to 11.X, but then I found the 0.11 branch where this seems to already have happened.

ndarilek avatar Apr 08 '19 21:04 ndarilek