## A little library for convert colors

nickelodeon0077
Prole
Posts: 20
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

### A little library for convert colors

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
Attachments
colorize.lua
JUST PUT AND REFERECE

Code: Select all

languages = {{name = "lua", from = "Brazil"}, {name = "c#", from = "USA", ...}}
function RealName(index)
if languages[index].name == "lua" then
return "LUA == MOON IN PORTUGUESE"
end
end

ivan
Party member
Posts: 1410
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: A little library for convert colors

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.

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

### Re: A little library for convert colors

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.

nickelodeon0077
Prole
Posts: 20
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

### Re: A little library for convert colors

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

Code: Select all

languages = {{name = "lua", from = "Brazil"}, {name = "c#", from = "USA", ...}}
function RealName(index)
if languages[index].name == "lua" then
return "LUA == MOON IN PORTUGUESE"
end
end

nickelodeon0077
Prole
Posts: 20
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

### Re: A little library for convert colors

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

Code: Select all

languages = {{name = "lua", from = "Brazil"}, {name = "c#", from = "USA", ...}}
function RealName(index)
if languages[index].name == "lua" then
return "LUA == MOON IN PORTUGUESE"
end
end

ingsoc451
Citizen
Posts: 71
Joined: Sat Feb 06, 2016 9:42 pm
Location: Oceania

I tried.
Attachments
test.lua