Jump to content

Module:Sports color/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Frietjes moved page Module:Basketball color/sandbox to Module:Sports color/sandbox without leaving a redirect: per TfD
sync
 
Line 1: Line 1:
--
--
-- This module implements
-- This module implements
-- {{Basketball leagues color}}, {{Basketball color cell}}, {{Basketball color cell2}},
-- {{NBA color cell}}, {{NBA color cell2}}
-- {{NBA color}}, {{NBA color cell}}, {{NBA color cell2}}
-- {{Euroleague color}}, {{Euroleague color cell}}, {{Euroleague color cell2}},
-- {{Euroleague color}}, {{Euroleague color cell}}, {{Euroleague color cell2}},
-- {{NBL colour}}, {{NBL colour cell}}, {{NBL colour cell2}},
-- {{NBL colour}}, {{NBL colour cell}}, {{NBL colour cell2}},
-- {{PBA color}}, {{PBA color cell}}, {{PBA color cell2}},
-- {{PBA color}}, {{PBA color cell}}, {{PBA color cell2}},
-- {{European leagues color}}, {{European leagues color cell}}, {{European leagues color cell2}},
-- {{KBL color}}, {{KBL color cell}}, {{KBL color cell2}}
-- {{KBL color}}, {{KBL color cell}}, {{KBL color cell2}}
--
-- {{Baseball color}}, {{Baseball secondary color}},
-- {{Baseball primary style}}, {{Baseball secondary style}}
--
--
local p = {}
local p = {}


local data_module = "Module:Basketball color/data/sandbox"
local data_module = 'Module:Sports color/basketball'


local function stripwhitespace(text)
local function stripwhitespace(text)
return text:match("^%s*(.-)%s*$")
return text:match("^%s*(.-)%s*$")
end

local function bordercss(c, w)
local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 #' .. c
.. ', inset -' .. w .. 'px -' .. w .. 'px 0 #' .. c
return 'box-shadow: ' .. s .. ';'
end
end


Line 29: Line 38:
if ( team and use_default[team:lower()] ) then
if ( team and use_default[team:lower()] ) then
colors = {"DCDCDC", "000000", "000000", "FFFFFF"}
colors = {"DCDCDC", "000000", "DCDCDC", "000000"}
else
else
local all_colors = mw.loadData(data_module)
local all_colors = mw.loadData(data_module .. '/sandbox')
colors = all_colors[team]
colors = all_colors[team]
if ( colors and type(colors) == 'string' ) then
if ( colors and type(colors) == 'string' ) then
Line 39: Line 48:


return colors or unknown
return colors or unknown
end

local function team_check(team, unknown)
local colors = get_colors(team, unknown)
if type(colors) == 'table' then
return 'known'
else
return unknown
end
end
end


Line 52: Line 70:
end
end


local function team_colorcell(team, borderwidth, bg, fg, bd)
local function team_colorcell(team, borderwidth, bg, fg, bd, simple)
local colors = get_colors(team, nil)
local colors = get_colors(team, nil)
local border = ''
local border = ''
Line 58: Line 76:
if (borderwidth ~= '') then
if (borderwidth ~= '') then
border = 'border:' .. borderwidth .. 'px solid #' .. stripwhitespace(colors[bd]) .. ';'
local bordercolor = stripwhitespace(colors[bd]) or 'FFFFFF'
if bordercolor ~= 'FFFFFF' then
if simple then
border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
else
border = bordercss(bordercolor, borderwidth)
end
end
end
end
return 'background-color:#' .. stripwhitespace(colors[bg]) .. ';color:#' .. stripwhitespace(colors[fg]) .. ';' .. border
return 'background-color: #' .. stripwhitespace(colors[bg]) .. '; color: #' .. stripwhitespace(colors[fg]) .. '; ' .. border
end
end


local function team_check(team, unknown)
local function team_titlestripe(team, topc, botc, borderwidth)
local colors = get_colors(team, unknown)
local colors = get_colors(team, nil)
local border = ''
if type(colors) == 'table' then
borderwidth = borderwidth or ''
return 'known'
else
return unknown
end
end


if (borderwidth ~= '') then
local function team_testtable(args, style, data)
local bordercolor = stripwhitespace(colors[topc]) or 'FFFFFF'
local teamlist = {}
if bordercolor ~= 'FFFFFF' then
if ( data ) then data_module = data end
border = border .. 'border-top: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'

-- helper function
local function table_row(t, c)
local res = mw.html.create('')
if( c[1] and c[2] and c[3] and c[4] ) then
res:tag('td'):wikitext(t)
else
res:tag('td'):wikitext(t .. ' <span class=error>ERROR</span>')
end
end
bordercolor = stripwhitespace(colors[botc]) or bordercolor
for i=1,4 do
if bordercolor ~= 'FFFFFF' then
res:tag('td'):css('background-color', '#' .. stripwhitespace(c[i] or 'ERROR'))
border = border .. 'border-bottom: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
end
end

return tostring(res)
end
end


return 'background-color: #FFFFFF; ' .. border
-- list of teams
end
if( args and args[1] ) then

for k, team in pairs(args) do
local function team_cellborder(team, pri, sec, ter, borderwidth, simple)
if type(k) == 'number' then
local colors = get_colors(team, nil)
table.insert(teamlist, team)
local border = ''
borderwidth = borderwidth or ''

if (borderwidth ~= '') then
local bordercolor = stripwhitespace(colors[pri] or colors[sec] or colors[ter]) or 'FFFFFF'
if bordercolor ~= 'FFFFFF' then
if simple then
border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
else
border = bordercss(bordercolor, borderwidth)
end
end
end
end
else
local all_colors = mw.loadData(data_module)
for team, colors in pairs( all_colors ) do
table.insert(teamlist, team)
end
table.sort(teamlist)
table.insert(teamlist, 'Free agent')
table.insert(teamlist, 'Retired')
end
end

return 'background-color: #FFFFFF; ' .. border
-- build table
end
local root = mw.html.create('table')

root:addClass('wikitable sortable')
function p.check(frame)
:css('font-size', '90%')
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
:css('line-height', '100%')
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
:cssText(style)
return team_check(args[1] or '', args[2] or '')
local row = root:tag('tr')
row:tag('th'):wikitext('Team')
for i=1,4 do
row:tag('th')
:addClass('unsortable')
:wikitext(i)
end
for k, team in pairs( teamlist ) do
row = root:tag('tr')
row:wikitext(table_row(team, get_colors(team)))
end
return tostring(root)
end
end


function p.color(frame)
function p.color(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
return team_color(args[1] or '', args[2] or '')
return team_color(args[1] or '', args[2] or '')
end
end
Line 137: Line 141:
function p.colorcell(frame)
function p.colorcell(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_colorcell(args[1] or '', args['border'] or '', 1, 2, 3)
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
return team_colorcell(args[1] or '', args['border'] or '', 1, 2, 3, args['simple'] and 1 or nil )
end
end


function p.colorcell2(frame)
function p.colorcell2(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1)
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1, args['simple'] and 1 or nil )
end
end


function p.check(frame)
function p.titlestripe(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_check(args[1] or '', args[2] or '')
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
return team_titlestripe(args[1] or '', 1, 3, args['width'] or '5')
end
end


function p.testtable(frame)
function p.cellborder(frame)
return team_testtable((frame.args[1] ~= nil) and frame.args or frame:getParent().args, frame.args['style'] or '', frame.args['data'])
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
return team_cellborder(args[1] or '', 3, 1, 4, args['width'] or '2', args['simple'] and 1 or nil )
end

function p.cellborder2(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
return team_cellborder(args[1] or '', 4, 3, 1, args['width'] or '2', args['simple'] and 1 or nil )
end
end



Latest revision as of 15:39, 13 April 2024

--
-- This module implements
-- {{NBA color cell}}, {{NBA color cell2}}
-- {{Euroleague color}}, {{Euroleague color cell}}, {{Euroleague color cell2}}, 
-- {{NBL colour}}, {{NBL colour cell}}, {{NBL colour cell2}},
-- {{PBA color}}, {{PBA color cell}}, {{PBA color cell2}},
-- {{European leagues color}}, {{European leagues color cell}}, {{European leagues color cell2}},
-- {{KBL color}}, {{KBL color cell}}, {{KBL color cell2}}
--
-- {{Baseball color}}, {{Baseball secondary color}}, 
-- {{Baseball primary style}}, {{Baseball secondary style}}
--
local p = {}

local data_module = 'Module:Sports color/basketball'

local function stripwhitespace(text)
	return text:match("^%s*(.-)%s*$")
end

local function bordercss(c, w)
	local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 #' .. c 
		.. ', inset -' .. w .. 'px -' .. w .. 'px 0 #' .. c
	return 'box-shadow: ' .. s .. ';'
end

local function get_colors(team, unknown)
	team = stripwhitespace(team or '')
	unknown = unknown or {"DCDCDC", "000000", "000000", "FFFFFF"}
	
	local use_default = {
		[""] = 1,
		["retired"] = 1,
		["free agent"] = 1,
	}
	
	local colors = nil
	
	if ( team and use_default[team:lower()] ) then
		colors = {"DCDCDC", "000000", "DCDCDC", "000000"}
	else
		local all_colors = mw.loadData(data_module .. '/sandbox')
		colors = all_colors[team]
		if ( colors and type(colors) == 'string' ) then
			colors = all_colors[colors]
		end
	end

	return colors or unknown
end

local function team_check(team, unknown)
	local colors = get_colors(team, unknown)
	if type(colors) == 'table' then
		return 'known'
	else
		return unknown
	end
end

local function team_color(team, num)
	local colors = get_colors(team, nil)

	num = tonumber(num:match('[1-4]') or '0')
	if ( num ) then
		return colors[num]
	else
		return ''
	end
end

local function team_colorcell(team, borderwidth, bg, fg, bd, simple)
	local colors = get_colors(team, nil)
	local border = ''
	borderwidth = borderwidth or ''
	
	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[bd]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			if simple then
				border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
			else
				border = bordercss(bordercolor, borderwidth)
			end
		end
	end
	
	return 'background-color: #' .. stripwhitespace(colors[bg]) .. '; color: #' .. stripwhitespace(colors[fg]) .. '; ' .. border
end

local function team_titlestripe(team, topc, botc, borderwidth)
	local colors = get_colors(team, nil)
	local border = ''
	borderwidth = borderwidth or ''

	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[topc]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			border = border .. 'border-top: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
		end
		bordercolor = stripwhitespace(colors[botc]) or bordercolor
		if bordercolor ~= 'FFFFFF' then
			border = border .. 'border-bottom: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
		end
	end

	return 'background-color: #FFFFFF; ' .. border
end

local function team_cellborder(team, pri, sec, ter, borderwidth, simple)
	local colors = get_colors(team, nil)
	local border = ''
	borderwidth = borderwidth or ''

	if (borderwidth ~= '') then
		local bordercolor = stripwhitespace(colors[pri] or colors[sec] or colors[ter]) or 'FFFFFF'
		if bordercolor ~= 'FFFFFF' then
			if simple then
				border = 'border: ' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
			else
				border = bordercss(bordercolor, borderwidth)
			end
		end
	end

	return 'background-color: #FFFFFF; ' .. border
end

function p.check(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
	return team_check(args[1] or '', args[2] or '')
end

function p.color(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
	return team_color(args[1] or '', args[2] or '')
end

function p.colorcell(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
	return team_colorcell(args[1] or '', args['border'] or '', 1, 2, 3, args['simple'] and 1 or nil )
end

function p.colorcell2(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'basketball')
	return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1, args['simple'] and 1 or nil )
end

function p.titlestripe(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
	return team_titlestripe(args[1] or '', 1, 3, args['width'] or '5')
end

function p.cellborder(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
	return team_cellborder(args[1] or '', 3, 1, 4, args['width'] or '2', args['simple'] and 1 or nil )
end

function p.cellborder2(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	data_module = 'Module:Sports color/' .. (frame.args['sport'] or frame:getParent().args['sport'] or 'ice hockey')
	return team_cellborder(args[1] or '', 4, 3, 1, args['width'] or '2', args['simple'] and 1 or nil )
end

return p