모듈:Sports color/contrast

-- This submodule is used to generate the complete color contrast table in
-- the documentation for Module:Sports color
local contrast = {}
local all_colors = {}
local contrast_mod = require("Module:Color contrast")
local function stripwhitespace(text)
	return text:match("^%s*(.-)%s*$")
end
local function get_colors(team, unknown)
	team = stripwhitespace(team or )
	unknown = unknown or {"DCDCDC", "000000"}

	local use_default = {
		"" = 1,
		"retired" = 1,
		"free agent" = 1,
	}

	local colors = nil

	if ( team and use_defaultteam:lower() ) then
		colors = {"DCDCDC", "000000"}
	else
		colors = all_colorsteam
		if ( colors and type(colors) == 'string' ) then
			colors = all_colorscolors
		end
	end

	return colors or unknown
end
local function color_contrast_ratio(hex1, hex2)
	if( hex1 and hex2) then
		local r = contrast_mod._ratio({'#' .. hex1, '#' .. hex2, error = 0})
		if( r > 0 ) then
			r = (r > 1) and r or (1/r)
			r = math.floor(r * 100 + 0.5) / 100
			return  (r > 1) and r or (1/r)
		end
	end
	return 
end
function contrast._testtable(args)
	local teamlist = {}
	local aliaslist = {}
	local style = args'style'
	local data_module = args'data' or ('Module:Sports color/' .. (args'sport' or 'basketball'))
	all_colors = mw.loadData(data_module)
	-- helper function
	local function table_row(t, c)
		local res = mw.html.create()
		if( c1 ) then
			res:tag('td'):wikitext(t)
		else
			res:tag('td'):wikitext(t .. ' ERROR')
		end
		for i=1,4 do
			res:tag('td')
				:css('background', ci and ('#' .. ci) or 'transparent')
				:wikitext(ci and  or 'X')
		end
		res:tag('td'):wikitext(color_contrast_ratio(c1,c2))
		res:tag('td'):wikitext(color_contrast_ratio(c4,c3))
		return tostring(res)
	end
-- list of teams
	if( args and args1 ) then
		for k, team in pairs(args) do
			if type(k) == 'number' then
				table.insert(teamlist, team)
			end
		end
	else
		for team, colors in pairs( all_colors ) do
			if type(colors) == 'string' then
				aliaslistcolors = (aliaslistcolors or ) .. 'Also known as ' .. team
			else
				table.insert(teamlist, team)
			end
		end
		table.sort(teamlist)
		table.insert(teamlist, 'Free agent')
		table.insert(teamlist, 'Retired')
	end
	
	-- build table
	local root = mw.html.create('table')
	root:addClass('wikitable sortable')
		:css('background', 'transparent')
		:css('font-size', '90%')
		:css('line-height', '100%')
		:cssText(style)
	if args'caption' then
		root:tag('caption'):wikitext(args'caption')
	end
	local row = root:tag('tr')
	row:tag('th')
		:attr('rowspan',2)
		:wikitext('Team')
	for i=1,4 do
		row:tag('th')
			:addClass('unsortable')
			:attr('rowspan',2)
			:wikitext(i)
	end
	row:tag('th')
		:attr('colspan', 2)
		:wikitext('Contrast')
	row = root:tag('tr')
	row:tag('th'):wikitext('1/2')
	row:tag('th'):wikitext('4/3')
	for k, team in pairs( teamlist ) do
		row = root:tag('tr')
		row:wikitext(table_row(team .. (aliaslistteam or ), get_colors(team)))
	end
	if style and style ~=  then
		style = 'font-size:90%; padding:0.4em; width:32em; border:1px #aaa solid;' .. style
	end
	local note = nil
	if args'note' then
		note = args'note'
	else
		note = 'The numeric columns are the calculated contrast ratio for the'
		.. ' first/second and the fourth/third colors. Anything lower than 3 is'
		.. ' very poor contrast and should be changed in'
		.. ' the data module. For more'
		.. ' information, see Template:Color contrast ratio.'
	end
	if note ~=  then
		note = mw.html.create('div'):cssText(style):wikitext(note)
	end
	return tostring(root) .. tostring(note)
end
function contrast.testtable(frame)
	return frame:preprocess(contrast._testtable(frame.args))
end
return contrast