TannerRogalsky / love.js

LÖVE ported to the web using Emscripten
MIT License
493 stars 53 forks source link

RGB colors no longer consistent with Love2d #61

Open djflorio opened 6 years ago

djflorio commented 6 years ago

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.

memersond commented 6 years ago

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.

djflorio commented 6 years ago

@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!

agargara commented 5 years ago

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.

drmargarido commented 5 years ago

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)
s-ol commented 5 years ago

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
ndarilek commented 5 years ago

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.