UsageEdit

{{#invoke:Sandbox/Keyacom/ishexcode|ishex|input|value if input is a color hex code|value if not}}

Note: if value if input is a color hex code and value if not values are not specified, they are true and , respectively.



-- This is for checking if the input is a color hex code.
-- Feel free to correct errors, but notify me (Keyacom) on the talk page.

local getArgs = require('Module:Arguments').getArgs

local p = {}

p.ishex = function(frame)
	local args = getArgs(frame)
	if string.match(args[1],"#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") ~= nil then
--[=[ In short, this would be the same as the regex
     /#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.
However, Lua's limitations do not allow for such regexes.]=]
    	return args[2] or true
	elseif string.match(args[1],"#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") ~= nil then
		return args[2] or true
	elseif string.match(args[1],"#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]?$") ~= nil then -- in Lua, ? still works on character sets
		return args[2] or true
	else
	    return args[3] or ""
	end
end

return p