cindy - [0-255] color range in LÖVE 11

Showcase your libraries, tools and other projects that help your fellow love users.
BurrickSlayer
Prole
Posts: 15
Joined: Sat Dec 21, 2013 4:28 pm

Re: cindy - [0-255] color range in LÖVE 11

Post by BurrickSlayer »

Just recently I noticed that the 0..1 floating point color range doesn't work so well in one specific area of LÖVE: vertex colors.

Consider the following code snippet:

Code: Select all

local COLOR_INDEX = 3
local mesh = love.graphics.newMesh(3)
mesh:setVertexAttribute(1, COLOR_INDEX, 0.1, 0.2, 0.3, 0.4)
print(mesh:getVertexAttribute(1, COLOR_INDEX))
getVertexAttribute does not return the values that were originally passed to setVertexAttribute. This is because each color component of a vertex is represented by a single byte internally. So setVertexAttribute crams the floating point values into a 0..255 value range and getVertexAttribute converts them back into floating point numbers, leading to some loss in precision.

But this can't be fixed by cindy or any other library. I consider it a small flaw in how this specific part of the API was designed. :monocle:

Best regards
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: cindy - [0-255] color range in LÖVE 11

Post by slime »

BurrickSlayer wrote: Wed Nov 22, 2023 7:01 pm getVertexAttribute does not return the values that were originally passed to setVertexAttribute. This is because each color component of a vertex is represented by a single byte internally.
The data type of color components (and any vertex attribute component) isn't fixed like that. You can make a mesh that uses any supported data type for its vertex attributes, for example 16 bit instead of 8 bit fixed-point values for colors.
BurrickSlayer
Prole
Posts: 15
Joined: Sat Dec 21, 2013 4:28 pm

Re: cindy - [0-255] color range in LÖVE 11

Post by BurrickSlayer »

Ahh, thanks, I knew I had missed something! Actually I forgot about this newMesh overload where you can specify a custom vertex format.

In my case it would be probably somewhat overkill to define the color components as float, only for the sake of having setters and getters that behave symmetrically. I have settled with "normalizing" the input colors (Math.floor(red * 255) / 255 etc.), so I can compare them with the output colors. But it's good to be reminded of this newMesh overload again.

Best regards
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests