Module:HexToRgb

local p = {}

function truncate(number, decimals) local power = 10^decimals return math.floor(number*power) / power end

function p.HexToRgbDecimal(frame) local hex = frame.args[1]:gsub("#","") if hex:len == 3 then return "rgb(" .. truncate((tonumber("0x"..hex:sub(1,1))*17)/255, 5) .. "," .. truncate((tonumber("0x"..hex:sub(2,2))*17)/255, 5) .. "," .. truncate((tonumber("0x"..hex:sub(3,3))*17)/255, 5) .. ")" else return "rgb(" .. truncate(tonumber("0x"..hex:sub(1,2))/255, 5) .. "," .. truncate(tonumber("0x"..hex:sub(3,4))/255, 5) .. "," .. truncate(tonumber("0x"..hex:sub(5,6))/255, 5) .. ")" end end

function p.HexToRgb(frame) local hex = frame.args[1]:gsub("#","") if hex:len == 3 then return "rgb(" .. (tonumber("0x"..hex:sub(1,1))*17) .. "," .. (tonumber("0x"..hex:sub(2,2))*17) .. "," .. (tonumber("0x"..hex:sub(3,3))*17) .. ")" else return "rgb(" .. tonumber("0x"..hex:sub(1,2)) .. "," .. tonumber("0x"..hex:sub(3,4)) .. "," .. tonumber("0x"..hex:sub(5,6)) .. ")" end end

return p