Page 1 of 1

simple tool for converting CSS color names to RGB/A

Posted: Fri Apr 10, 2020 7:01 pm
by tristanmvh
So I just switched from HTML5 Canvas to LÖVE and I'm used to typing the name of the color I want (I find it easier to use than using RGB or HEX because I'm not very good at remembering those kind of values) so I thought I'd create a simple function that converts CSS colors to RGB/RGBA. Let me know what you think :D
https://github.com/malmodev/love2d-css-colors

Re: simple tool for converting CSS color names to RGB/A

Posted: Sat Apr 11, 2020 3:17 pm
by yetneverdone
I've taken a look at the code. I suggest the following:
1. Instead of making the library global, you could make it return a local table that contains the functions

Code: Select all

--yourlib.lua
local yourlib = {}
function yourlib.method() end
return yourlib

--usage
local color = require("yourlib")
color.method("red", 0.1)

2. Instead of creating a table per function call, you could define the colors ahead. Also, instead of calculating per function call, predefine the values. Another thing to add, you can skip a bunch of it statements by using table indexing.

Code: Select all

local yourlib = {}
local colors = {}
colors.red = {1, 0, 0}
function yourlib.method(color, a)
  assert(colors[color], "passed color does not exist")
  local r,g,b = colors[color]
  return {r,g,b,a or 1}
end

Re: simple tool for converting CSS color names to RGB/A

Posted: Sat Apr 11, 2020 5:18 pm
by tristanmvh
Thanks for the feedback! I'll be looking in to it:)

Re: simple tool for converting CSS color names to RGB/A

Posted: Sun Apr 12, 2020 1:11 am
by Ref
yetneverdone really doesn't appreciate how much effort went in to your file.
My only interest is the opposite of yours - I really don't know the names of the various colors.
Some of the names really don't bring to mind the color.
Best

Re: simple tool for converting CSS color names to RGB/A

Posted: Sun Apr 12, 2020 9:57 am
by yetneverdone
If I didnt appreciate his work, i would have just ignored the library. My suggestion was to make it "more usable" by other people as well. I mean, if a module/library wastes memory and processing, and is prone to conflict with your codebase, why use it?

Re: simple tool for converting CSS color names to RGB/A

Posted: Sun Apr 12, 2020 9:18 pm
by Ref
More power to you!
I tried to group the colors together but the names just don't convey the colors I expect.
Best