Jump to content

Module:College color/contrast

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 14:13, 26 March 2015 (style). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This submodule is used to generate the complete color contrast table in
-- the documentation for [[Module:College color]]
local contrast = {}
local all_colors = {}

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_default[team:lower()] ) then
		colors = {"DCDCDC", "000000"}
	else
		colors = all_colors[team]
		if ( colors and type(colors) == 'string' ) then
			colors = all_colors[colors]
		end
	end
 
	return colors or unknown
end

function contrast._testtable(args)
	local teamlist = {}
	local style = args['style']
	local data_module = args['data'] or "Module:College color/data"
	local contrast_mod = require("Module:Color contrast")
	all_colors = mw.loadData(data_module)
	
    -- helper function
	local function table_row(t, c)
		local res = mw.html.create('')
		if( c[1] and c[2] ) then
			res:tag('td'):wikitext(t)
		else
			res:tag('td'):wikitext(t .. ' <span class=error>ERROR</span>')
		end
		for i=1,3 do
			res:tag('td'):css('background', c[i] and ('#' .. c[i]) or 'transparent')
		end
		for i=1,3 do
			local j = math.fmod(i,3) + 1
			if( c[i] and c[j] ) then
				local r = contrast_mod._ratio({'#' .. c[i], '#' .. c[j], error = 0})
				if( r > 0 ) then
					r = (r > 1) and r or (1/r)
					r = math.floor(r * 100 + 0.5) / 100
					res:tag('td'):wikitext( (r > 1) and r or (1/r) )
				else
					res:tag('td'):wikitext('')
				end
			else
				res:tag('td'):wikitext('')
			end
		end
		return tostring(res)
	end

    -- list of teams
	if( args and args[1] ) 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
			table.insert(teamlist, team)
		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)
	local row = root:tag('tr')
	row:tag('th')
		:attr('rowspan',2)
		:wikitext('Team')
	for i=1,3 do
		row:tag('th')
			:addClass('unsortable')
			:attr('rowspan',2)
			:wikitext(i)
	end
	row:tag('th')
		:attr('colspan', 3)
		:wikitext('Contrast')
	row = root:tag('tr')
	row:tag('th'):wikitext('1/2')
	row:tag('th'):wikitext('2/3')
	row:tag('th'):wikitext('3/1')
	
	for k, team in pairs( teamlist ) do
		row = root:tag('tr')
		row:wikitext(table_row(team, get_colors(team)))
	end
	
	return tostring(root)
end

function contrast.testtable(frame)
	return contrast._testtable(frame.args)
end

return contrast