Page 1 of 1

### A little library for convert colors

Posted: Sun Feb 10, 2019 3:14 am
here are my library for convert color the github link is:

a little example:

Code: Select all

``````  require("colorized")
love.draw()
love.graphics.setBackgroundColor(hex("#fff")) --this will go turn the background into white and 1 of alpha
love.graphics.setColor(rgba(255, 140, 10)) --a type of orange i think
love.graphics.setColor(vec4(0, 0, 50, 0.5)) --dark blue with parcial alpha
love.graphics.setColor(hex("#a2f5a1")) --i dont know ;D
end``````

### Re: A little library for convert colors

Posted: Sun Feb 10, 2019 7:51 am
It's a good try, but forum veteran Grump already beat you to it.
You may want to look at his library for inspiration.
Briefly going over the code, the "hex" function is not very good.
There are more efficient ways to convert a hex value into RGB.
For example, you can use tonumber specifically with base-16 numbers:

Code: Select all

``tonumber(HEX,16)``
And the modulus operator could be used to split numbers:

Code: Select all

``````function hex_to_rgb(rgb)
-- clamp between 0x000000 and 0xffffff
rgb = rgb%0x1000000 -- 0xffffff + 1

-- extract each color
local b = rgb%0x100 -- 0xff + 1 or 256
local g = (rgb - b)%0x10000 -- 0xffff + 1
local r = (rgb - g - b)
-- shift right
g = g/0x100 -- 0xff + 1 or 256
r = r/0x10000 -- 0xffff + 1

return r, g, b
end

function rgb_to_hex(r, g, b)
return r*0x10000 + g*0x100 + b
end``````
Putting the entire module into a separate table would be nicer since it won't pollute the global space _G.

### Re: A little library for convert colors

Posted: Sun Feb 10, 2019 9:41 am
To be fair, this one does different things from mine - mine is (on purpose) only concerned with value ranges and function patching, not so much with different color representations.

In addition to ivan's remarks, I would expect that all components are to be given in the same value range. It makes little sense to me that rgb() expects r, g, b in the [0,255] range, but a has to be given in the [0.0,1.0] range. This is also true for the hex() function, which in my opinion should accept #RRGGBBAA and #RGBA strings.

hex() has a bug in its conversion code: 0xa through 0xf equals 10 through 15, not 11 through 16. Calling hex('#fff') will calculate the color from {272, 272, 272, 255}, which is obviously wrong.

It may be better for performance to return separate values instead of creating a new table in rgb() and hex().

Code: Select all

``````return {r/255, g/255, b/255, a} -- no
return r/255, g/255, b/255, a -- yes
``````
It makes sense to return a table in vec4(), though you should allow the caller to provide the target table for better efficiency:

Code: Select all

``````function vec4 (r, g, b, a, result)
result = result or {}
...
``````
And a useful addition would be conversion to and from the HSV color space.

### Re: A little library for convert colors

Posted: Mon Feb 11, 2019 10:46 pm
grump wrote:
Sun Feb 10, 2019 9:41 am
To be fair, this one does different things from mine - mine is (on purpose) only concerned with value ranges and function patching, not so much with different color representations.

In addition to ivan's remarks, I would expect that all components are to be given in the same value range. It makes little sense to me that rgb() expects r, g, b in the [0,255] range, but a has to be given in the [0.0,1.0] range. This is also true for the hex() function, which in my opinion should accept #RRGGBBAA and #RGBA strings.

hex() has a bug in its conversion code: 0xa through 0xf equals 10 through 15, not 11 through 16. Calling hex('#fff') will calculate the color from {272, 272, 272, 255}, which is obviously wrong.

It may be better for performance to return separate values instead of creating a new table in rgb() and hex().

Code: Select all

``````return {r/255, g/255, b/255, a} -- no
return r/255, g/255, b/255, a -- yes
``````
It makes sense to return a table in vec4(), though you should allow the caller to provide the target table for better efficiency:

Code: Select all

``````function vec4 (r, g, b, a, result)
result = result or {}
...
``````
And a useful addition would be conversion to and from the HSV color space.
the bug i'll see and correct, but thank you to say it

### Re: A little library for convert colors

Posted: Mon Feb 11, 2019 10:47 pm
ivan wrote:
Sun Feb 10, 2019 7:51 am
It's a good try, but forum veteran Grump already beat you to it.
You may want to look at his library for inspiration.
Briefly going over the code, the "hex" function is not very good.
There are more efficient ways to convert a hex value into RGB.
For example, you can use tonumber specifically with base-16 numbers:

Code: Select all

``tonumber(HEX,16)``
And the modulus operator could be used to split numbers:

Code: Select all

``````function hex_to_rgb(rgb)
-- clamp between 0x000000 and 0xffffff
rgb = rgb%0x1000000 -- 0xffffff + 1

-- extract each color
local b = rgb%0x100 -- 0xff + 1 or 256
local g = (rgb - b)%0x10000 -- 0xffff + 1
local r = (rgb - g - b)
-- shift right
g = g/0x100 -- 0xff + 1 or 256
r = r/0x10000 -- 0xffff + 1

return r, g, b
end

function rgb_to_hex(r, g, b)
return r*0x10000 + g*0x100 + b
end``````
Putting the entire module into a separate table would be nicer since it won't pollute the global space _G.
thank you i'll see, because i'm with a new project where i'll utilize a lot of colors picked up the internet

### Re: A little library for convert colors

Posted: Wed Feb 13, 2019 10:41 pm
I tried.