Skip to content

Commit

Permalink
Added Geyser.Color.find_color_name and utilize it to harden Geyser.Co…
Browse files Browse the repository at this point in the history
…lor.parse (Mudlet#3380)

* Added Geyser.Color.find_color_name and utilize it to harden Geyser.color.parse
* Adjustments based on suggestions
  • Loading branch information
demonnic authored Mar 3, 2020
1 parent e8b999b commit 810b974
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/mudlet-lua/lua/geyser/GeyserColor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ function Geyser.Color.parse(red, green, blue, alpha)
next_num = string.gmatch(red, "%d+")

-- fourth case is a named string
elseif color_table[red] then
elseif Geyser.Color.find_color_name(red) then
local col = Geyser.Color.find_color_name(red)
local i = 0
local n = #color_table[red]
local n = #color_table[col]
next_num = function()
-- create a simple iterator
i = i + 1
if i <= n then
return color_table[red][i]
return color_table[col][i]
else return nil
end
end
Expand Down Expand Up @@ -148,6 +149,23 @@ function Geyser.Color.parse(red, green, blue, alpha)
return r, g, b, a
end

--- Searches for a close match to 'color' in the color_table
-- Returns the color found, or false if it can't find one
-- @param color the color name you're trying to find
function Geyser.Color.find_color_name(color)
if type(color) ~= "string" then
return false
end
color = color:lower()
color = color:gsub("_", "")
for color_name,_ in pairs(color_table) do
if color_name:lower() == color then
return color_name
end
end
return false
end

--- Applies colors to a window drawing from defaults and overridden values.
-- @param cons The window to apply colors to
function Geyser.Color.applyColors(cons)
Expand Down

0 comments on commit 810b974

Please sign in to comment.