From 9a247794254c84a2deb2c3dcb6aebdf4347bb60f Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 20 Aug 2010 22:27:07 +0200 Subject: [PATCH] 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 --- lib/gears/color.lua.in | 30 ++++++++++++++++++++++++++++++ lib/gears/init.lua.in | 11 +++++++++++ 2 files changed, 41 insertions(+) create mode 100644 lib/gears/color.lua.in create mode 100644 lib/gears/init.lua.in diff --git a/lib/gears/color.lua.in b/lib/gears/color.lua.in new file mode 100644 index 000000000..8e4ff9c47 --- /dev/null +++ b/lib/gears/color.lua.in @@ -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 diff --git a/lib/gears/init.lua.in b/lib/gears/init.lua.in new file mode 100644 index 000000000..5920eeaf6 --- /dev/null +++ b/lib/gears/init.lua.in @@ -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