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 = "Manga Parts", items_per_row = 4, items = {
{'PB', 'PB2', 'PB3', 'PB4'},
{'BT', 'BT2', 'BT3', 'BT4'},
{'SC', 'SC2', 'SC3', 'SC4'},
{'DU', 'DU2', 'DU3', 'DU4'},
{'VA', 'VA2', 'VA3', 'VA4'},
{'SO', 'SO2', 'SO3', 'SO4'},
{'SBR', 'SBR2', 'SBR3', 'SBR4'},
{'JJL', 'JJL2', 'JJL3', 'JJL4'},
{'TJL', 'TJL2', 'TJL3', 'TJL4'}
}},
{ title = "Spin-Offs", items_per_row = 4, items = {
{'TSKR', 'TSKR2', 'TSKR3', 'TSKR4'},
{'CDDH', 'CDDH2', 'CDDH3', 'CDDH4'},
{'COOLSHOCKBT', 'COOLSHOCKBT2', 'COOLSHOCKBT3', 'COOLSHOCKBT4'},
{'BAOH', 'BAOH2', 'BAOH3', 'BAOH4'},
{'IRENE', 'IRENE2', 'IRENE3', 'IRENE4'},
{'UEUJ', 'UEUJ2', 'UEUJ3', 'UEUJ4'},
{'ECCENTRICS', 'ECCENTRICS2', 'ECCENTRICS3', 'ECCENTRICS4'}
}},
{ title = "Novels", items_per_row = 4, items = {
{'NOVEL', 'NOVEL2', 'NOVEL3', 'NOVEL4'},
{'THEBOOK', 'THEBOOK2', 'THEBOOK3', 'THEBOOK4'},
{'GHGR', 'GHGR2', 'GHGR3', 'GHGR4'},
{'PHF', 'PHF2', 'PHF3', 'PHF4'},
{'OH', 'OH2', 'OH3', 'OH4'},
{'JORGE', 'JORGE2', 'JORGE3', 'JORGE4'},
{'EA', 'EA2', 'EA3', 'EA4'},
{'ITC', 'ITC2', 'ITC3', 'ITC4'},
{'IGGY', 'IGGY2', 'IGGY3', 'IGGY4'}
}},
{ title = "Miscellaneous", items_per_row = 4, items = {
{'GAME', 'GAME2', 'GAME3', 'GAME4'},
{'JOJOmag', 'JOJOmag2', 'JOJOmag3', 'JOJOmag4'},
{'LLC', 'LLC2', 'LLC3', 'LLC4'},
{'JJBA', 'JJBA2', 'JJBA3', 'JJBA4'},
{'HFTF', 'HFTF2', 'HFTF3', 'HFTF4'},
{'JOSTARS', 'JOSTARS2', 'JOSTARS3', 'JOSTARS4'},
{'JOJOWORLD', 'JOJOWORLD2', 'JOJOWORLD3', 'JOJOWORLD4'},
{'PDB', 'PDB2', 'PDB3', 'PDB4'},
{'ML', 'ML2', 'ML3', 'ML4'},
{'JJBE', 'JJBE2', 'JJBE3', 'JJBE4'}
}},
{ title = "Weekly Shonen Jump", items_per_row = 4, items = {
{'WSJ', 'WSJ2', 'WSJ3', 'WSJ4'},
{'WSJSeventies', 'WSJSeventies2', 'WSJSeventies3', 'WSJSeventies4'},
{'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