Implement color parsing

This adds a lua module for parsing colors. Named colors like "black" aren't
supported, but #rrggbb and #rrggbbaa colors do work.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-08-20 22:27:07 +02:00
parent 44f64eee58
commit 9a24779425
2 changed files with 41 additions and 0 deletions

30
lib/gears/color.lua.in Normal file
View File

@ -0,0 +1,30 @@
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2010 Uli Schlachter
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
local setmetatable = setmetatable
local string = string
local table = table
local tonumber = tonumber
local unpack = unpack
module("gears.color")
-- Thanks to #lua for this. :)
function parse_color(col)
local rgb = {}
for pair in string.gmatch(col, "[^#].") do
table.insert(rgb, tonumber(pair, 16) / 255)
end
while #rgb < 4 do
table.insert(rgb, 1)
end
return unpack(rgb)
end
setmetatable(_M, { __call = function (_, ...) return parse_color(...) end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

11
lib/gears/init.lua.in Normal file
View File

@ -0,0 +1,11 @@
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2010 Uli Schlachter
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
require("gears.color")
module("gears")
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80