gears.color: Add support for named colors
Oh hey, Pango exports an API that allows to query for named colors based on the famous rgb.txt! Let's use that! Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
0956aa01d9
commit
79d3dc003a
|
@ -13,14 +13,16 @@ local tonumber = tonumber
|
||||||
local ipairs = ipairs
|
local ipairs = ipairs
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local type = type
|
local type = type
|
||||||
local cairo = require("lgi").cairo
|
local lgi = require("lgi")
|
||||||
|
local cairo = lgi.cairo
|
||||||
|
local Pango = lgi.Pango
|
||||||
local surface = require("gears.surface")
|
local surface = require("gears.surface")
|
||||||
|
|
||||||
local color = { mt = {} }
|
local color = { mt = {} }
|
||||||
local pattern_cache
|
local pattern_cache
|
||||||
|
|
||||||
--- Parse a HTML-color.
|
--- Parse a HTML-color.
|
||||||
-- This function can parse colors like `#rrggbb` and `#rrggbbaa`.
|
-- This function can parse colors like `#rrggbb` and `#rrggbbaa` and also `red`.
|
||||||
-- Thanks to #lua for this. :)
|
-- Thanks to #lua for this. :)
|
||||||
--
|
--
|
||||||
-- @param col The color to parse
|
-- @param col The color to parse
|
||||||
|
@ -28,8 +30,10 @@ local pattern_cache
|
||||||
-- @usage -- This will return 0, 1, 0, 1
|
-- @usage -- This will return 0, 1, 0, 1
|
||||||
-- gears.color.parse_color("#00ff00ff")
|
-- gears.color.parse_color("#00ff00ff")
|
||||||
function color.parse_color(col)
|
function color.parse_color(col)
|
||||||
-- Get all hex chars
|
|
||||||
local rgb = {}
|
local rgb = {}
|
||||||
|
-- Is it a HTML-style color?
|
||||||
|
if string.match(col, "^#%x+$") then
|
||||||
|
-- Get all hex chars
|
||||||
for char in string.gmatch(col, "[^#]") do
|
for char in string.gmatch(col, "[^#]") do
|
||||||
table.insert(rgb, tonumber(char, 16) / 0xf)
|
table.insert(rgb, tonumber(char, 16) / 0xf)
|
||||||
end
|
end
|
||||||
|
@ -49,6 +53,17 @@ function color.parse_color(col)
|
||||||
rgb = merged
|
rgb = merged
|
||||||
factor = next_factor
|
factor = next_factor
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
-- Let's ask Pango for its opinion (but this doesn't support alpha!)
|
||||||
|
local c = Pango.Color()
|
||||||
|
if c:parse(col) then
|
||||||
|
rgb = {
|
||||||
|
c.red / 0xffff,
|
||||||
|
c.green / 0xffff,
|
||||||
|
c.blue / 0xffff,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
-- Add missing groups (missing alpha)
|
-- Add missing groups (missing alpha)
|
||||||
while #rgb < 4 do
|
while #rgb < 4 do
|
||||||
table.insert(rgb, 1)
|
table.insert(rgb, 1)
|
||||||
|
|
|
@ -22,6 +22,10 @@ describe("gears.color", function()
|
||||||
test(0, 0, 0, 0xff, "#000000")
|
test(0, 0, 0, 0xff, "#000000")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("black", function()
|
||||||
|
test(0, 0, 0, 0xff, "black")
|
||||||
|
end)
|
||||||
|
|
||||||
it("opaque black", function()
|
it("opaque black", function()
|
||||||
test(0, 0, 0, 0xff, "#000000ff")
|
test(0, 0, 0, 0xff, "#000000ff")
|
||||||
end)
|
end)
|
||||||
|
@ -34,6 +38,10 @@ describe("gears.color", function()
|
||||||
test(0xff, 0xff, 0xff, 0x7f, "#ffffff7f")
|
test(0xff, 0xff, 0xff, 0x7f, "#ffffff7f")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("chartreuse", function()
|
||||||
|
test(0x7f, 0xff, 0x00, 0xff, "chartreuse")
|
||||||
|
end)
|
||||||
|
|
||||||
describe("different lengths", function()
|
describe("different lengths", function()
|
||||||
local function gray(e, e_a, input)
|
local function gray(e, e_a, input)
|
||||||
local o_r, o_g, o_b, o_a, unused = color.parse_color(input)
|
local o_r, o_g, o_b, o_a, unused = color.parse_color(input)
|
||||||
|
|
Loading…
Reference in New Issue