Module:Color

Revision as of 23:19, 21 July 2026 by Kuhlau (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module implements {{Color}}.

How to Update

  1. Visit MediaWiki:Gadget-Colors.css. Here are the instructions on how to add colors:
    • If you have multiple (or you have one and you're not sure of the format), you can use this link to generate the code (type your color names in the big thing at the top, where it says "test string", each on a new line. Then, copy the result from the bottom.)
    • Replace #hexcode with your hex code(s).
    • Check whether the colors will look good on light or dark mode using WebAIM's Contrast Checker. Currently, our light mode background color is #fff and our dark mode background color is #111a20. If the colors fail multiple checks on one mode, try tweaking it.
    • You have to add the light mode colors to the light mode block (starts at the top) and the dark mode colors to the dark mode block (starts around line 100).
    • Now add the rules to implement the colors to the block starting around line 260 titled /* [[Template:Color]] */. Use this to generate the code (or hit Substitution on the left sidebar of the previous link you used).
  2. In this module, update local colors table starting at line 7 with your new color(s). (here you go)

local p = {}
local lib = require('Module:Feature')

local aliases = {
}

local colors = {
    new        = 'text-new',
    old        = 'text-old',
    buzz       = 'text-buzz',
    menu       = 'text-menu',
    help       = 'text-help',
    r4s        = 'text-R4S',
    r5s        = 'text-R5S',
    numbers    = 'text-numbers',
    magic      = 'text-magic',
    physical   = 'text-physical',
    healing    = 'text-healing',
}

-- Main function for wiki usage.
-- If 2 arguments are present, treats the first one as a keyword.
-- If only 1 argument is present, searches it for keywords.
-- Returns the color associated with the keyword, or "inherit" if not found.
function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame)
	local class = ''
	local span = ''
	local text = ''
	local link = args.link
    -- choose variant based on number of arguments
	if args[2] then
		if args[1]:find('#') then
			span = args[1]
		else
			class = p._getKeywordColor(mw.ustring.lower(args[1]))
		end
		text = args[2]
	else
		class = p._searchTextForKeyword(mw.ustring.lower(args[1]))
		text = args[1]
	end

	if link ~= nil then
		if link == '1' or link == 1 then
			text = '[[' .. text .. ']]'
		else
			text = '[[' .. link .. '|' .. text .. ']]'
		end
	else
		text = '' .. text .. ''
	end

	if (args.nobold and lib.isNotEmpty(class)) then
		return '<span class="' .. class .. '">' .. text .. '</span>'
	elseif (args.nobold and lib.isNotEmpty(span)) then
		return '<span style="color:' .. span .. '">' .. text .. '</span>'
	elseif lib.isNotEmpty(span) then
		return '<span style="color:' .. span .. '"><b>' .. text .. '</b></span>'
	else
		return '<span class="' .. class .. '"><b>' .. text .. '</b></span>'
	end

end

-- Library functions usable in other modules

-- Returns the color associated with given keyword,
-- or "inherit" if input is not a keyword.
-- Runs output through nowiki by default,
-- unless noescape is specified to be true.
-- (input must be in lower case.)
function p._getKeywordColor(input, noescape)
    local element = aliases[input] or input
    local color = colors[element]
    if noescape then
    	return color or 'inherit'
	end
    return color and mw.text.nowiki(color) or 'inherit'
end

-- Helper method to search given text for the keys of given table t.
-- If a key is found, returns its value; returns nil otherwise.
local function searchTextForKeys(text, t)
	for key, val in pairs(t) do
		result = mw.ustring.find(text, key, 1, true)
        if result ~= nil then return val end
    end
end

-- Searches given text for keywords and returns the associated color,
-- or "inherit" if no keyword is found.
-- (text must be in lower case.)
function p._searchTextForKeyword(text)
	-- try elements first
	local color = searchTextForKeys(text, colors)
    if color ~= nil then
        return mw.text.nowiki(color)
    end
    -- try aliases afterwards
    local keyword = searchTextForKeys(text, aliases)
    if keyword ~= nil then
    	return mw.text.nowiki(colors[keyword])
	end

    return 'inherit'
end

return p