a working setColor compatibilty multiplier

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Posts: 2
Joined: Fri May 11, 2018 7:50 am

a working setColor compatibilty multiplier

Post by memzen » Thu May 17, 2018 1:16 am

When making a library compatible with all versions concerning the new setColor parameters range such that

lg.setColor(r, g, b, a or 255)


lg.setColor(r * CDIV, g * CDIV, b * CDIV, (a or 255) * CDIV)

using the following formula is not exactly accurate.

local CDIV = love._version_major >= 11 and 1/255 or 1

This is because the result is floored and not rounded. Using this conversion formula does work.

local CDIV = love._version_major >= 11 and 0.003922 or 1

User avatar
Party member
Posts: 2247
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary

Re: a working setColor compatibilty multiplier

Post by zorg » Thu May 17, 2018 7:24 am

I'm not sure why it would be either floored or rounded; 1/255 is 0.00392156862, and that should work. Löve uses luaJIT by default, so you don't even need to do bull like 1.0/255.0 for explicit non-integers either, like what the newer lua versions would assume you'd *want* to do.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Inner party member
Posts: 1904
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: a working setColor compatibilty multiplier

Post by Nixola » Thu May 17, 2018 7:29 am

If I recall correctly, the / operator means float division while the // means integer division, so you wouldn't need to use floats explicitly, but that discussion would be a bit off topic.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
Party member
Posts: 1060
Joined: Sun Oct 18, 2015 2:58 pm

Re: a working setColor compatibilty multiplier

Post by pgimeno » Thu May 17, 2018 12:02 pm

This thread would be better suited for General, but I think I get the point of the OP. It has to do with FP precision.

Code: Select all

$ luajit
LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> CDIV = 1/255
> print(math.floor(CDIV*33*255))
Or, in more practical and LÖVEly terms (for 11.0):

Code: Select all

local CDIV = 1/255
local img = love.image.newImageData(1, 1)
img:setPixel(0, 0, CDIV*33, CDIV*37, CDIV*41) -- chosen values
local a, b, c = img:getPixel(0,0)
print(a*255, b*255, c*255) -- prints 32, 36, 40
Making the division factor slightly bigger, gets around that problem and manages to assign the right colours to the textures with a byte encoding.

It's easy to see which values cause problems with Python, typing this in the REPL: [CDIV*i*255 for i in range(256)]

Party member
Posts: 318
Joined: Sat Jul 22, 2017 7:43 pm

Re: a working setColor compatibilty multiplier

Post by grump » Thu May 17, 2018 3:06 pm

BTW, when I made cindy, I used multiplication instead of division at first, because I naturally assumed it would be faster. When I started measuring, I was surprised that x / 255 seemed to be consistently faster than x * 0.0039..., by a small margin though.

Code: Select all

local t = love.timer.getTime()
for i = 1, 1e10 do
	local x = 37 / 255
print(love.timer.getTime() - t)

local t = love.timer.getTime()
for i = 1, 1e10 do
	local x = 37 * 0.003921568627451
print(love.timer.getTime() - t)

Code: Select all

I didn't investigate any further and just left / 255 in the code.

Post Reply

Who is online

Users browsing this forum: SuperZazu and 2 guests