A little library for convert colors

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
nickelodeon0077
Prole
Posts: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

A little library for convert colors

Post by nickelodeon0077 »

here are my library for convert color the github link is:

GITHUB LINK!!!

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
(1.58 KiB) Downloaded 302 times
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: A little library for convert colors

Post by ivan »

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: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: A little library for convert colors

Post by grump »

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: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

Re: A little library for convert colors

Post by nickelodeon0077 »

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
nickelodeon0077
Prole
Posts: 22
Joined: Mon Feb 04, 2019 2:38 pm
Location: Curitiba, PR, Brazil

Re: A little library for convert colors

Post by nickelodeon0077 »

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
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: A little library for convert colors

Post by monolifed »

I tried.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests