Modul:Vorlage:Farbtabelle

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData: 2020-08-17

local ClrTbl = { suite   = "ClrTbl",
                 serial  = "2020-08-17",
                 item    = 98412751 }
--[=[
generate colour tables / Vorlage:Farbtabelle
]=]
local Failsafe  = ClrTbl



local function faith( aR, aG, aB )
    -- Check websafe code
    -- Precondition:
    --     aR   -- number, with code red
    --     aG   -- number, with code green
    --     aB   -- number, with code blue
    -- Postcondition:
    --     Returns true if websafe
    return ( aR % 3  +  aG % 3  +  aB % 3   ==   0 )
end -- faith()



local function first()
    -- Initalize
    if not ClrTbl.hex then
        local s
        ClrTbl.hex = { [false] = { },
                       [true]  = { }
                     }
        for i = 0, 15 do
            s = string.format( "%X", i )
            table.insert( ClrTbl.hex[false], i, s )
            table.insert( ClrTbl.hex[true],  i, s .. s )
        end -- for i
    end
end -- first()



ClrTbl.f = function ( abbr )
    -- Generate page content
    -- Precondition:
    --     abbr   -- number or boolean, with true or 3 for abbreviated
    -- Postcondition:
    --     Returns true if websafe
    local long = ( abbr ~= true  and  abbr ~= 3 )
    local r    = ""
    local iR, iG, iB, hR, hG, hB, hRhG, h, s, table, td, tr
    first()
    for iR = 0, 15 do
        hR = ClrTbl.hex[ long ][ iR ]
        if long then
            h = "xxxx"
        else
            h = "xx"
        end
        r = string.format( "%s\n=== %s%s ===\n", r, hR, h )
        table = mw.html.create( "table" )
                       :css( { ["text-align"] = "center",
                               ["width"]      = "100%" } )
        for iG = 0, 15 do
            hG   = ClrTbl.hex[ long ][ iG ]
            hRhG = hR .. hG
            tr   = mw.html.create( "tr" )
            for iB = 0, 15 do
                hB = ClrTbl.hex[ long ][ iB ]
                h  = hRhG .. hB
                hB = "#" .. h
                if iR + iG + iB  <  24 then
                    s = "#FFF"
                else
                    s = "#000"
                end
                td = mw.html.create( "td" )
                            :attr( { title = hB } )
                            :css( { ["background"] = hB,
                                    ["color"]      = s } )
                            :wikitext( h )
                if faith( iR, iG, iB ) then
                    td:css( { ["font-weight"] = "bold" } )
                end
                tr:newline()
                  :node( td )
            end -- for iB
            table:newline()
                 :node( tr )
        end -- for iG
        r = r .. tostring( table )
    end -- for iR
    return r
end -- ClrTbl.f()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version
    --                         or "wikidata" or "~" or "@" or false
    -- Postcondition:
    --     Returns  string  -- with queried version/item, also if problem
    --              false   -- if appropriate
    -- 2020-08-17
    local since = atleast
    local last    = ( since == "~" )
    local linked  = ( since == "@" )
    local link    = ( since == "item" )
    local r
    if last  or  link  or  linked  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local suited = string.format( "Q%d", item )
            if link then
                r = suited
            else
                local entity = mw.wikibase.getEntity( suited )
                if type( entity ) == "table" then
                    local seek = Failsafe.serialProperty or "P348"
                    local vsn  = entity:formatPropertyValues( seek )
                    if type( vsn ) == "table"  and
                       type( vsn.value ) == "string"  and
                       vsn.value ~= "" then
                        if last  and  vsn.value == Failsafe.serial then
                            r = false
                        elseif linked then
                            if mw.title.getCurrentTitle().prefixedText
                               ==  mw.wikibase.getSitelink( suited ) then
                                r = false
                            else
                                r = suited
                            end
                        else
                            r = vsn.value
                        end
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = {}

function p.f( frame )
    local k = frame.args.bytes
    local lucky, r
    if k then
        k = tonumber( k )
    end
    lucky, r = pcall( ClrTbl.f, k )
    return r or lucky
end

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe

p.ClrTbl = function ()
    -- Module interface
    return ClrTbl
end

return p