Jump to content

Module:Gridiron color/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 02:57, 7 June 2018 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--
-- This module implements
-- {{Gridiron primary color}} -- {{Gridiron primary color raw}} -- {{Gridiron primary style}}
-- {{Gridiron secondary color}} -- {{Gridiron secondary color raw}}
-- {{Gridiron tertiary color raw}}
-- {{Gridiron alt primary color}} -- {{Gridiron alt secondary color}}
--

local p = {}

local color_data = mw.loadData("Module:Gridiron color/data")

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

local function get_year(colors, year)
	if colors[6] and type(colors[6] == 'table') then
		for team, year_colors in pairs(colors[6]) do
			if mw.ustring.find(team, "%d%d%d%dthru%d%d%d%d$") then
				local start_year, end_year = mw.ustring.match(team, "(%d%d%d%d)thru(%d%d%d%d)$")
				if (tonumber(start_year) <= year) and (year <= tonumber(end_year)) then
					return year_colors
				end
			end
		end
	end
	
	return colors
end

local function get_colors(team, unknown, year)
	team = stripwhitespace(team or '')
	unknown = unknown or color_data["#default"] or {"#DCDCDC", "", "", "", ""}
	
	local use_default = {
		[""] = 1,
		["retired"] = 1,
		["free agent"] = 1,
	}
	
	local colors = nil
	
	if ( team and use_default[team:lower()] ) then
		colors = unknown
	else
		if (not tonumber(year) or tonumber(year) <= 0)
			and mw.ustring.find(team, "%d?%d?%d%dthru%d?%d?%d%d$") then
			team, year = mw.ustring.match(team, "^(.-) -(%d?%d?%d%d)thru%d?%d?%d%d$")
			year = tonumber(year)
			
			if year >= 20 and year < 100 then -- Two-digit years were deprecated in 2018
				year = year + 1900
			elseif year < 20 then
				year = year + 2000
			elseif year < 1000 then
				year = nil
			end
		end
		
		if tonumber(year) and tonumber(year) > 0 then
			--code for handling year parameter
			colors = get_year(color_data[team], year)
		else
			colors = color_data[team]
		end
		
		if ( colors and type(colors) == 'string' ) then
			-- follow alias recursively
			return  get_colors (colors, unknown, year)
		end
	end
	
	return colors or unknown
end

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

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


local function bordercss(c, w)
	local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 #' .. c 
		.. ', inset -' .. w .. 'px -' .. w .. 'px 0 #' .. c
	return '-moz-box-shadow: ' .. s .. '; -webkit-box-shadow: ' .. s .. '; box-shadow: ' .. s .. ';'
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
		if simple then
			border = 'border:' .. borderwidth .. 'px solid #' .. stripwhitespace(colors[bd]) .. ';'
		else
			border = bordercss(stripwhitespace(colors[bd]), borderwidth)
		end
	end
	
	return 'background-color:#' .. stripwhitespace(colors[bg]) .. ';color:#' .. stripwhitespace(colors[fg]) .. ';' .. border
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

function p.test(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	local team = args.team
	local year = args.year
	return '{ "' .. team_color(team, 1, year) .. '", '.. team_color(team, 2, year) .. '", '.. team_color(team, 3, year) .. '", '.. team_color(team, 4, year) .. '", '.. team_color(team, 5, year) .. '"}'
end
	

function p.color(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	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
	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
	return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1, args['simple'] and 1 or nil )
end

function p.check(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	return team_check(args[1] or '', args[2] or '')
end

return p