FANDOM


local C = {}
Color = require('Module:Colors/class')
 
function val(value)
    if value == nil or type(value) == 'table' then return nil end
    value = mw.text.trim(tostring(value))
    if value == '' then return nil end
    return value
end
function round(number, precision)
    precision = precision or 0
    local m = 10 ^ precision
    return math.floor(number * m + 0.5) / m
end
function color_breakdown(color)
    local h, s, l = color:hsl()
    local r, g, b = color:rgb()
    local a = color:a()
 
    r = round(r * 255)
    g = round(g * 255)
    b = round(b * 255)
 
    h = round(h, 2)
    s = round(s * 100, 2)
    l = round(l * 100, 2)
 
    a = round(a * 100, 2)
 
    return string.format('%s

R: %d
G: %d
B: %d

H: %g°
S: %g%%
L: %g%%

Alpha: %g%%', tostring(color), r, b, g, h, s, l, a)
end
function getColor(arg, method)
    local s, r = pcall(function()
        local value = val(arg[1])
        if value == nil then return '' end
 
        local color = Color.new(value)
        if color == nil then
            error{code='bad-format'}
        end
 
        local mods = val(arg['mods'] or arg['mod'])
        if mods then color = color:modify(mods) end
 
        local f1
        if method == 'toHEX' then
            f1 = not not val(arg[2])
        elseif method ~= nil then
            f1 = math.max(0, math.min(5, round(tonumber(val(arg[2])) or 1)))
        end
 
        local result
        if method == nil then
            result = tostring(color)
        elseif type(color[method]) ~= 'function' then
            error{code='bad-method'}
        else
            result = color[method](color, f1)
        end
 
        return (result:gsub('^#', '#'))
    end)
 
    if s then return r
    elseif type(r) == 'table' and r.code then
        local txt = Color.msg('error-' .. r.code, unpack(r))
        if txt ~= '' then
            return mw.html.create('span'):addClass('error'):wikitext(txt)
        end
    else
        error(r)
    end
end
 
function C.color(frame)
    return getColor(frame.args)
end
function C.hex(frame)
    return getColor(frame.args, 'toHEX')
end
function C.rgb(frame)
    return getColor(frame.args, 'toRGB')
end
function C.rgba(frame)
    return getColor(frame.args, 'toRGBA')
end
function C.rgb2(frame)
    return getColor(frame.args, 'toRGB2')
end
function C.rgba2(frame)
    return getColor(frame.args, 'toRGBA2')
end
function C.hsl(frame)
    return getColor(frame.args, 'toHSL')
end
function C.hsla(frame)
    return getColor(frame.args, 'toHSLA')
end
function C.table(frame)
    local color_codes = mw.text.split(val(frame.args['colors'] or frame.args[1]) or '', '%s*\n%s*')
    local colors = {}
    for _,c in ipairs(color_codes) do
        local color = Color.new(c)
        if color ~= nil then
            colors[#colors+1] = color
        elseif #colors > 0 then
            color, count = colors[#colors]:copy():modify(c)
            if count > 0 then
                colors[#colors+1] = color
            end
        end
    end
    if #colors == 0 then
        return mw.html.create('div'):addClass('notice error'):wikitext('No colors (in supported format)')
    end
 
    local mods = mw.text.split(val(frame.args['mods'] or frame.args[2]) or '', '%s*\n%s*')
    if #mods == 0 then
        return mw.html.create('div'):addClass('notice error'):wikitext('No mods')
    end
 
    local hRow = (val(frame.args['head-row']) or 'top'):lower()
    local hCol = (val(frame.args['head-col']) or 'left'):lower()
 
    local formats = {
        ['hex'] = 'toHEX',
        ['rgb'] = 'toRGB',
        ['rgba'] = 'toRGBA',
        ['hsl'] = 'toHSL',
        ['hsla'] = 'toHSLA',
        ['none'] = false
    }
    local format = formats[(val(frame.args['format']) or 'hex'):lower()]
    local hformat = formats[(val(frame.args['head-format'] or frame.args['format']) or 'hex'):lower()]
 
    local full = not not val(frame.args['full'])
 
    local txt
    local tab = mw.html.create('table'):addClass(frame.args['class'] or 'wikitable'):cssText(frame.args['style'] or '')
 
    if hRow == 'top' or hRow == 'both' then
        local row = tab:tag('tr')
        if hCol == 'left' or hCol == 'both' then row:tag('th') end
        for _,col in ipairs(colors) do
 
            if hformat == nil then txt = tostring(col)
            elseif hformat then txt = col[hformat](col, full)
            else txt = '' end
 
            row:tag('th'):wikitext(txt):css('background-color', tostring(col)):attr('title', color_breakdown(col))
        end
        if hCol == 'right' or hCol == 'both' then row:tag('th') end
    end
 
    for _,m in ipairs(mods) do
        local disp
        if m:len() > 23 then disp = m:sub(1, 21) .. '...' end
 
        local row = tab:tag('tr')
        if hCol == 'left' or hCol == 'both' then
            local cell = row:tag('th'):wikitext(disp or m)
            if disp then
                cell:attr('title', m)
            end
        end
        for _,c in ipairs(colors) do
            local col = c:copy():modify(m)
 
            if format == nil then txt = tostring(col)
            elseif format then txt = col[format](col, full)
            else txt = '' end
 
            row:tag('td'):wikitext(txt):css('background-color', tostring(col)):attr('title', color_breakdown(col))
        end
        if hCol == 'right' or hCol == 'both' then
            local cell = row:tag('th'):wikitext(disp or m)
            if disp then
                cell:attr('title', m)
            end
        end
    end
 
    if hRow == 'bottom' or hRow == 'both' then
        local row = tab:tag('tr')
        if hCol == 'left' or hCol == 'both' then row:tag('th') end
        for _,col in ipairs(colors) do
 
            if hformat == nil then txt = tostring(col)
            elseif hformat then txt = col[hformat](col, full)
            else txt = '' end
 
            row:tag('th'):wikitext(txt):css('background-color', tostring(col)):attr('title', color_breakdown(col))
        end
        if hCol == 'right' or hCol == 'both' then row:tag('th') end
    end
    local res = tostring(tab):gsub('
', '
')
    return res
end
 
return C

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.