Modul:Color
Aus DragonballWiki
Weitere Optionen
Die Dokumentation für dieses Modul kann unter Modul:Color/Doku erstellt werden
-- =================================================================
-- Module:Color
-- Stores and retrieves color codes for the wiki.
-- Also generates a dynamic cheatsheet.
-- =================================================================
local p = {}
-- =================================================================
-- Helper Functions
-- =================================================================
local color_data = mw.loadData('Module:Color/data')
-- Checks if a string is nil, empty, or contains only whitespace.
local function isNilOrEmpty(s)
return s == nil or s:match('^%s*$')
end
-- =================================================================
-- Data
-- =================================================================
-- Layout for the auto-generated cheatsheet
local cheatsheet_layout = {
{ title = "Grayscale", items_per_row = 8, items = {
{'0', '1', '2', '3', '4', '5', '6', '7'}
}},
{ title = "Colors", items_per_row = 4, items = {
{'DB', 'DB2', 'DB3', 'DB4'},
{'SG', 'SG2', 'SG3', 'SG4'},
{'KN', 'KN2', 'KN3', 'KN4'},
{'BB', 'BB2', 'BB3', 'BB4'},
{'VA', 'VA2', 'VA3', 'VA4'},
{'MT', 'MT2', 'MT3', 'MT4'},
{'YU', 'YU2', 'YU3', 'YU4'},
{'CC', 'CC2', 'CC3', 'CC4'},
{'TN', 'TN2', 'TN3', 'TN4'}
}},
{ title = "Color 2", items_per_row = 4, items = {
{'PO', 'PO2', 'PO3', 'PO4'},
{'GBK', 'GBK2', 'GBK3', 'GBK4'},
{'SGN', 'SGN2', 'SGN3', 'SGN4'},
{'PL', 'PL2', 'PL3', 'PL4'},
{'SS', 'SS2', 'SS3', 'SS4'},
{'MJO', 'MJO2', 'MJO3', 'MJO4'},
{'FR', 'FR2', 'FR3', 'FR4'}
}},
{ title = "Color 2", items_per_row = 4, items = {
{'NOVEL', 'NOVEL2', 'NOVEL3', 'NOVEL4'},
{'THEBOOK', 'THEBOOK2', 'THEBOOK3', 'THEBOOK4'},
{'CL', 'CL2', 'CL3', 'CL4'},
{'OTP', 'OTP2', 'OTP3', 'OTP4'},
{'DRG', 'DRG2', 'DRG3', 'DRG4'},
{'DBA', 'DBA2', 'DBA3', 'DBA4'},
{'BK', 'BK2', 'BK3', 'BK4'},
{'MRP', 'MRP2', 'MRP3', 'MRP4'},
{'UU', 'UU2', 'UU3', 'UU4'}
}},
{ title = "Colors 4", items_per_row = 4, items = {
{'GAME', 'GAME2', 'GAME3', 'GAME4'},
{'BBY', 'BBY2', 'BBY3', 'BBY4'},
{'CGU', 'CGU2', 'CGU3', 'CGU4'},
{'JNA', 'JNA2', 'JNA3', 'JNA4'},
{'PDB', 'PDB2', 'PDB3', 'PDB4'},
{'ML', 'ML2', 'ML3', 'ML4'},
{'AKA', 'AKA2', 'AKA3', 'AKA4'}
}},
{ title = "Weekly Shonen Jump", items_per_row = 4, items = {
{'WSJ', 'WSJ2', 'WSJ3', 'WSJ4'},
{'WSJEighties', 'WSJEighties2', 'WSJEighties3', 'WSJEighties4'},
{'WSJNineties', 'WSJNineties2', 'WSJNineties3', 'WSJNineties4'},
{'WSJAughts', 'WSJAughts2', 'WSJAughts3', 'WSJAughts4'},
{'WSJTens', 'WSJTens2', 'WSJTens3', 'WSJTens4'}
}}
}
-- =================================================================
-- Main Functions
-- =================================================================
-- Looks up a color key in the data table and returns the hex code
-- If not found, it returns the key itself as a fallback
local function getColorCode(key)
if isNilOrEmpty(key) then return '' end
local lookupKey = mw.ustring.lower(mw.text.trim(key))
return color_data[lookupKey] or key
end
function p.lookup(key)
return getColorCode(key):gsub('#', '#')
end
-- Main function, called by {{#invoke:Color|main}}
function p.main(frame)
local args
if type(frame.getParent) == 'function' then
local parent = frame:getParent()
args = parent and parent.args or frame.args
else
args = frame.args
end
local colorName = args[1]
local text_to_style = args[2]
-- Check if parameter 2 contains actual, non-whitespace text
if not isNilOrEmpty(text_to_style) then
-- IF YES: Build and return the fully styled <span> element
local element = mw.html.create('span')
element:wikitext(text_to_style) -- Use wikitext to allow links, etc. in the text
-- Get the color code with the HTML entity
local fgColorEntity = getColorCode(colorName)
if fgColorEntity ~= '' then
-- Convert entity to raw hash only for the CSS function
local fgColorCss = fgColorEntity:gsub('#', '#')
element:css('color', fgColorCss)
end
-- Check for and set the background color from the 'bg' parameter
if not isNilOrEmpty(args.bg) then
-- Get the bg color code with the HTML entity
local bgColorEntity = getColorCode(args.bg)
if bgColorEntity ~= '' then
-- Convert entity to raw hash ONLY for the CSS function
local bgColorCss = bgColorEntity:gsub('#', '#')
element:css('background-color', bgColorCss)
end
end
local styledText = tostring(element)
-- Finally, check if it needs to wrap the whole thing in a link
if not isNilOrEmpty(args.link) then
-- Build a MediaWiki link: [[link_target|display_text]]
return ('[[%s|%s]]'):format(args.link, styledText)
else
-- If no link, just return the styled text
return styledText
end
else
-- IF NO (parameter 2 is empty, whitespace, or nil)
-- Return only the raw color code for parameter 1, with the entity
return getColorCode(colorName)
end
end
-- =================================================================
-- Cheatsheet Generator
-- =================================================================
-- Helper function to determine if text should be black or white based on background
local function get_text_color(hex_color)
-- Return black for invalid or non-existent hex codes
if not hex_color or hex_color:len() < 6 then return 'black' end
local hex = hex_color:gsub('#', '')
-- Handle shorthand hex like #F0C -> #FF00CC
if hex:len() == 3 then
hex = hex:sub(1,1):rep(2) .. hex:sub(2,2):rep(2) .. hex:sub(3,3):rep(2)
end
local r = tonumber(hex:sub(1, 2), 16) or 0
local g = tonumber(hex:sub(3, 4), 16) or 0
local b = tonumber(hex:sub(5, 6), 16) or 0
-- Using the W3C/WCAG luminance formula
-- A threshold of 128 is a common midpoint
local luminance = (0.299 * r + 0.587 * g + 0.114 * b)
if luminance > 80 then
return 'black'
else
return 'white'
end
end
-- Function to generate the cheatsheet wikitext
-- Called via {{#invoke:Color|cheatsheet}}
function p.cheatsheet(frame)
local wikitext_lines = {}
for _, group in ipairs(cheatsheet_layout) do
-- Start a new wikitext table for each group
table.insert(wikitext_lines, '{|cellspacing="0" style="width:100%; font-weight:bold; text-align:center; border:5px solid #C6C6C6;"')
-- Add the title row
if group.title then
table.insert(wikitext_lines, '|-')
local header_style = 'height:30px; background:#2D2D2D; color:white;'
table.insert(wikitext_lines, ('! colspan="%d" style="%s" | %s'):format(group.items_per_row, header_style, group.title))
end
-- Build the data rows
for _, row_items in ipairs(group.items) do
table.insert(wikitext_lines, '|- style="height:50px;"')
local cells = {}
for _, item_name in ipairs(row_items) do
local key_lower = mw.ustring.lower(item_name)
-- Get the hex code with the entity from data table
local entity_hex = color_data[key_lower] or '#FFFFFF'
-- Create a "clean" version with a real '#' for internal processing and display
local clean_hex = entity_hex:gsub('#', '#')
-- Get the correct text color
-- local text_color = get_text_color(clean_hex)
local text_color = "white"
if mw.ustring.find(key_lower,"%l2") then
text_color = "black"
else
if not mw.ustring.find(key_lower,"%l") and tonumber(key_lower) >= 2 then
text_color = "black"
end
end
-- Build the style attribute string using the clean hex code
local cell_style = ('width:%.1f%%; background:%s; color:%s;'):format(100 / group.items_per_row, clean_hex, text_color)
-- Build the cell content
local cell_content = ('%s<br />%s'):format(item_name, clean_hex:upper())
-- Assemble the full wikitext for the cell
table.insert(cells, ('| style="%s" | %s'):format(cell_style, cell_content))
end
table.insert(wikitext_lines, table.concat(cells, '\n'))
end
-- Close the wikitext table
table.insert(wikitext_lines, '|}')
end
-- Join all the lines together with newlines and return the final wikitext string
return table.concat(wikitext_lines, '\n')
end
return p