Page 2 of 2

Re: a working setColor compatibilty multiplier

Posted: Sat May 26, 2018 2:53 pm
by Tricky
For me the change from 255 to 1 didn't make sense anyway.
Yeah 255 is indeed the max color value due to 255 being the decimal equivalent to binary 1111111
For me it works very very confusing since all graphic manipulation programs I have (as well as other programming tools I use) use a 0-255 scale, so you can imagine I was NOT amused about this change, and if it really needed to be done a DIFFERENT FUNCTION handling the 0-1 scale could be desirable (WITHOUT deprecating the original function!!!!)

Now I was "blessed" by laziness. I fell asleep every time I had to type love.graphics.setColor so I had a "color=love.graphics.setColor" present. All I needed to do was adding a version checker that would divide all values by 255 if version 11 or higher was detected. Saved me a lot of "code rot". The speed in which drastic changes take place on LÖVE, makes me wanna avoid all original direct love calls but use my own in stead, so I only need to add a version checker and the different code that I would need and voilà everything works.... again... :-/

Oh well... ;)

Re: a working setColor compatibilty multiplier

Posted: Sat May 26, 2018 4:48 pm
by Nixola
There are more ways to represent colours, some of which exceed 255.

Re: a working setColor compatibilty multiplier

Posted: Mon May 28, 2018 9:05 am
by s-ol
Tricky wrote: Sat May 26, 2018 2:53 pm For me the change from 255 to 1 didn't make sense anyway.
Yeah 255 is indeed the max color value due to 255 being the decimal equivalent to binary 1111111
For me it works very very confusing since all graphic manipulation programs I have (as well as other programming tools I use) use a 0-255 scale, so you can imagine I was NOT amused about this change, and if it really needed to be done a DIFFERENT FUNCTION handling the 0-1 scale could be desirable (WITHOUT deprecating the original function!!!!)

Now I was "blessed" by laziness. I fell asleep every time I had to type love.graphics.setColor so I had a "color=love.graphics.setColor" present. All I needed to do was adding a version checker that would divide all values by 255 if version 11 or higher was detected. Saved me a lot of "code rot". The speed in which drastic changes take place on LÖVE, makes me wanna avoid all original direct love calls but use my own in stead, so I only need to add a version checker and the different code that I would need and voilà everything works.... again... :-/

Oh well... ;)
how about:

Code: Select all

-- compat.lua
local _setColor = love.graphics.setColor
function love.graphics.setColor(r, g, b, a)
  return _setColor(r/255, g/255, b/255, a and a/255)
end
this is Lua after all.

0-1 is semantically more correct, and as Nixola points out, more general than 0-255. Some colorspaces have 0-360 for example.
Having the values normalized is also mathematically ideal, it means that you can multiply components properly.
If you are working with 0-255 values internally then you have to take care to divide by 255 when you do thinks like multiplying colors or exponentials etc.

Re: a working setColor compatibilty multiplier

Posted: Tue May 29, 2018 6:05 pm
by Tricky
s-ol wrote: Mon May 28, 2018 9:05 am
Tricky wrote: Sat May 26, 2018 2:53 pm For me the change from 255 to 1 didn't make sense anyway.
Yeah 255 is indeed the max color value due to 255 being the decimal equivalent to binary 1111111
For me it works very very confusing since all graphic manipulation programs I have (as well as other programming tools I use) use a 0-255 scale, so you can imagine I was NOT amused about this change, and if it really needed to be done a DIFFERENT FUNCTION handling the 0-1 scale could be desirable (WITHOUT deprecating the original function!!!!)

Now I was "blessed" by laziness. I fell asleep every time I had to type love.graphics.setColor so I had a "color=love.graphics.setColor" present. All I needed to do was adding a version checker that would divide all values by 255 if version 11 or higher was detected. Saved me a lot of "code rot". The speed in which drastic changes take place on LÖVE, makes me wanna avoid all original direct love calls but use my own in stead, so I only need to add a version checker and the different code that I would need and voilà everything works.... again... :-/

Oh well... ;)
how about:

Code: Select all

-- compat.lua
local _setColor = love.graphics.setColor
function love.graphics.setColor(r, g, b, a)
  return _setColor(r/255, g/255, b/255, a and a/255)
end
this is Lua after all.

0-1 is semantically more correct, and as Nixola points out, more general than 0-255. Some colorspaces have 0-360 for example.
Having the values normalized is also mathematically ideal, it means that you can multiply components properly.
If you are working with 0-255 values internally then you have to take care to divide by 255 when you do thinks like multiplying colors or exponentials etc.
I basically went a bit like this

Code: Select all

 
 --[[ This is fake code, so it won't work, it was just to get the picture]]
 if version<11 then
    color=love.graphics.setcolor
  else 
    color=function(r,g,b,a)
       love.graphics.setcolor(r/255,g/255,b/255,a/255)
    end
 end    
 
 
That is fake code, it was just to get the picture, and this way it works on both 0.10 as on 11.x
Now I wonder what good a 0-360 scale would do since 0-255 already creates more colors a human eye can distinguish (not to mention the only color value I've ever seen in a 0-360 scale is the hue value in hsv coloring, due to 360 degrees making the full color circle, but if it did make sense the change the way it was done, was just way ugly, making all LÖVE programs suffer from code-rot in the process.
I also wonder if having to divide all colors by 255 all the time doesn't lead to performance issues in more complex programs...

Well, I already coded when colors where still in a 256-color-palette with each palette slot only having a 0-63 scale for colors, and I do remember that it was rather a big switch to go to a 0-255 scale, not to mention, getting rid of palettes altogether, but I could see the sense back then. Now it just seems like, well, we can do it, so why don't we.... Figures ....

Re: a working setColor compatibilty multiplier

Posted: Mon Jun 04, 2018 12:42 pm
by holywyvern
You can do it simpler:

Code: Select all

local lib = {}

if love._version_major < 11 then 
  lib.setColor = love.graphics.setColor
  lib.getColor = love.graphics.getColor
else
  lib.setColor = function (r, g, b, a)
     love.graphics.setColor(r / 255, g / 255, b / 255, (a or 255) / 255)
  end
  lib.getColor = function ()
    local r, g, b, a = love.graphics.getColor()
    return r * 255, g * 255, b * 255, a * 255
  end
end

return lib

You could override love.graphics.getColor and love.graphics.setColor directly, but that's your choice to make.

Re: a working setColor compatibilty multiplier

Posted: Mon Jun 04, 2018 12:59 pm
by grump
holywyvern wrote: Mon Jun 04, 2018 12:42 pm

Code: Select all

  lib.getColor = function ()
    local r, g, b, a = love.graphics.getColor()
    return r * 255, g * 255, b * 255, a * 255
  end
That doesn't work as expected, because getColor returns truncated values.

love.graphics.setColor is also not the only function that takes colors - there's plenty more in several interfaces that can't be all replaced so easily. For a complete solution that takes care of everything for you, use this:

Code: Select all

if love._version_major >= 11 then
    require('cindy').applyPatch() -- https://github.com/megagrump/cindy
end
This makes love.graphics, Shader, ImageData, SpriteBatch and all other relevant things work with the [0-255] range again. Or you can omit applyPatch() and use the provided alternative functions (e. g. setColorBytes) instead that work with the old range.

Re: a working setColor compatibilty multiplier

Posted: Wed May 20, 2020 7:56 pm
by D0NM
2grump: You r such a party saVer!
At least migrated our Zabuyaki beat 'em up to Love 11.3
the final thing was cindy.
And usually I use polyamory (for video lessons about old/new love2d, etc).

Re: a working setColor compatibilty multiplier

Posted: Wed May 20, 2020 9:27 pm
by grump
D0NM wrote: Wed May 20, 2020 7:56 pm 2grump: You r such a party safer!
At least migrated our Zabuyaki beat 'em up to Love 11.3
the final thing was cindy.
Thank you very much for the feedback!

The change to the "new" color range makes a lot of sense for newer projects, but I'm happy that this little hack helped you port your game to LÖVE 11 with less effort. That's why I made it in the first place.