Mô đun:College color

--
-- Mô đun này thực hiện , ,
-- , ,
-- , , và 
--
local p = {}
local data_module = "Mô đun:College color/data"
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 sRGB ( v )
	if (v *>%s*", ' ');
	team = mw.ustring.gsub(team, " Tteam$", )
	team = mw.ustring.gsub(team, " Bbasketball$", )
	team = mw.ustring.gsub(team, " Bbaseball$", )
	team = mw.ustring.gsub(team, " Ccross Ccountry$", )
	team = mw.ustring.gsub(team, " Ffield Hhockey$", )
	team = mw.ustring.gsub(team, " Ffootball$", )
	team = mw.ustring.gsub(team, " Ggolf$", )
	team = mw.ustring.gsub(team, " Ggymnastics$", )
	team = mw.ustring.gsub(team, " Iice Hhockey$", )
	team = mw.ustring.gsub(team, " Llacrosse$", )
	team = mw.ustring.gsub(team, " Rrowing$", )
	team = mw.ustring.gsub(team, " Sski$", )
	team = mw.ustring.gsub(team, " Ssoccer$", )
	team = mw.ustring.gsub(team, " Ssoftball$", )
	team = mw.ustring.gsub(team, " Sswim$", )
	team = mw.ustring.gsub(team, " Ttennis$", )
	team = mw.ustring.gsub(team, " Ttrack Aand Ffield$", )
	team = mw.ustring.gsub(team, " Vvolleyball$", )
	team = mw.ustring.gsub(team, " Wwrestling$", )
	team = mw.ustring.gsub(team, " Wwomen's$", )
	team = mw.ustring.gsub(team, " Mmen's$", )
	return team
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
		local all_colors = mw.loadData(data_module)
		colors = all_colorsteam
		if ( colors and type(colors) == 'string' ) then
			colors = all_colorscolors
		end
	end
	return colors or unknown
end
local function team_color(team, num, num2)
	local colors = get_colors(team, nil)
	num = tonumber(num:match('1-3') or '0')
	num2 = tonumber(num2:match('1-3') or '0')
	if ( num ) then
		return colorsnum or colorsnum2 or 
	else
		return 
	end
end
local function team_style1(team, borderwidth, fontcolor)
	local colors = get_colors(team, nil)
	local color = '#' .. (colors3 or colors2 or )
	local style = 'background-color:#' .. (colors1 or ) .. ';color:' .. (fontcolor or color) .. ';'
	-- remove the border if it's nearly white
	if ((1 + 0.05)/(color2lum(color) + 0.05)  0 and color ~= '#FFFFFF') then
		style = style .. bordercss(color, borderwidth)
	end
	return style
end
local function team_style2(team, borderwidth, fontcolor)
	local colors = get_colors(team, nil)
	local color = '#' .. (colors1 or )
	local style = 'background-color:#' .. (colors3 or colors2 or ) .. ';color:' .. (fontcolor or color) .. ';'
	-- remove the border if it's nearly white
	if ((1 + 0.05)/(color2lum(color) + 0.05)  0 and color ~= '#FFFFFF') then
		style = style .. bordercss(color, borderwidth)
	end
	return style
end
local function team_header1(team, borderwidth)
	local colors = get_colors(team, nil)
	-- set the default background
	local background = (colors1 or 'FFFFFF'):upper()
	-- set background to white if it's nearly white
	if ((1 + 0.05)/(color2lum(background) + 0.05)  wcontrast + 1.25 ) then
		fontcolor = '000000'
	else
		fontcolor = 'FFFFFF'
	end
	local style
	if( background == 'FFFFFF' ) then
		style = 'background-color:none;color:#' .. fontcolor .. ';'
	else
		style = 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
	end
	if borderwidth then
		borderwidth = tonumber(borderwidth or '2') or 0
		local bordercolor = (colors3 or colors2 or 'FFFFFF'):upper()
		if (borderwidth > 0 and bordercolor ~= 'FFFFFF') then
			-- do not add a border if it's nearly white
			if ((1 + 0.05)/(color2lum(bordercolor) + 0.05) >= 1.25) then
				style = style .. bordercss('#' .. bordercolor, borderwidth)
			end
		end
	end
	return style
end
local function team_header2(team)
	local colors = get_colors(team, nil)
	-- set the default background
	local background = (colors3 or colors2 or 'FFFFFF'):upper()
	-- set background to white if it's nearly white
	if ((1 + 0.05)/(color2lum(background) + 0.05)  wcontrast + 1.25 ) then
		fontcolor = '000000'
	else
		fontcolor = 'FFFFFF'
	end
	if( background == 'FFFFFF' ) then
		return 'background-color:none;color:#' .. fontcolor .. ';'
	else
		return 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
	end
end
local function team_table_head(args, team, ctype)
	local colors = get_colors(team, nil)
	-- set the default background
	local background = (ctype == 'p') and
		(colors1 or 'FFFFFF'):upper() or
		(colors3 or colors2 or 'FFFFFF'):upper()
	-- now pick a font color
	local fontcolor = '000000'
	-- compute the luminosity of the background
	local lum = color2lum(background)
	-- compute the contrast with white and black
	local wcontrast = (1 + 0.05)/(lum + 0.05)
	local bcontrast = (lum + 0.05)/(0 + 0.05)
	-- select the text color with the best contrast
	if( bcontrast > wcontrast + 1.25 ) then
		fontcolor = '000000'
	else
		fontcolor = 'FFFFFF'
	end
	local s = 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
	local res = '
	for i=1,50 do
		if( argsi ~= nil ) then
			local cstyle = 'scope="col" style="' .. s .. '"'
			if args'col' .. i .. 'span' ~= nil then
				cstyle = cstyle .. ' colspan=' .. args'col' .. i .. 'span'
			end
			res = res .. '! ' .. cstyle .. ' |' .. argsi .. '\n'
		else
			return res .. '
		end
	end
	return res .. 'Lỗi!\n
end
local function team_stripe1(team, borderwidth)
	local colors = get_colors(team, nil)
	-- set the default scheme
	local background = colors1 or 
	local fontcolor = colors2 or 
	local bordercolor = (colors3 or colors2 or ):upper()
	borderwidth = tonumber(borderwidth or '3') or 0
	-- if there is no tertiary color, then pick a font color
	if (colors3 == nil) then
		-- compute the luminosity of the background
		local lum = color2lum(colors1)
		-- compute the contrast with white and black
		local wcontrast = (1 + 0.05)/(lum + 0.05)
		local bcontrast = (lum + 0.05)/(0 + 0.05)
		-- select the text color with the best contrast
		if( bcontrast > wcontrast + 1.25 ) then
			fontcolor = '000000'
		else
			fontcolor = 'FFFFFF'
		end
	end
	-- finally build the style string
	local style = 
	if (borderwidth > 0) then
		-- use the primary as the border if the border is white or close to white
		local bordercontrast = (1 + 0.05)/(color2lum(bordercolor) + 0.05)
		if (bordercontrast  0) then
		return table.concat(colorboxes, sep)
	end
	return 
end
local function team_list(frame, team, num1, num2, num3, num4, num5, sep)
	local function colorbox( h )
		local r = mw.html.create()
		r:tag('span')
			:css('background-color', '#' .. (h or ))
			:css('border', '1px solid #000')
			:wikitext('    ')
		return tostring(r)
	end
	local colors = get_colors(team, 'unknown')
	if type(colors) ~= 'table' then
		return 
	end
	local nums = {
		tonumber(num1:match('1-5') or '0') or 0,
		tonumber(num2:match('1-5') or '0') or 0,
		tonumber(num3:match('1-5') or '0') or 0,
		tonumber(num4:match('1-5') or '0') or 0,
		tonumber(num5:match('1-5') or '0') or 0}
	local colorboxes = {}
	local colornames = {}
	local colororder = {'1','2','3','4','5'}
	local order = colors'order' or 
	if(order ~= ) then
		colororder = mw.text.split(order, )
	end
	for k,v in pairs(colororder) do
		local i = tonumber(v) or 0
		if ( numsi > 0 ) then
			if(	colors'name' .. numsi) then
				table.insert(colornames,colors'name' .. numsi)
				table.insert(colorboxes,colorbox(colorsnumsi or ))
			end
		end
	end
	local res = 
	if (#colornames == 1) then
		res = colornames1
	elseif (#colornames == 2) then
		res = colornames1 .. ' và ' .. colornames2
	elseif (#colornames > 2) then
		res = colornames1
		for i=2,#colornames do
			if( i  0) then
		res = res .. sep
	end
	if (#colorboxes > 0) then
		res = res .. table.concat(colorboxes, ' ')
	end
	return res
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.color(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_color(remove_sport(args1 or ), args2 or , args3 or )
end
function p.color1(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_color(remove_sport(args1 or ), '1', )
end
function p.color32(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_color(remove_sport(args1 or ), '3', '2')
end
function p.style1(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_style1(remove_sport(args1 or ), args'border', args'color')
end
function p.style2(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_style2(remove_sport(args1 or ), args'border', args'color')
end
function p.header1(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_header1(remove_sport(args1 or ), args'border')
end
function p.header2(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_header2(remove_sport(args1 or ))
end
function p.tablehead1(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_table_head(args, remove_sport(args'team' or ), 'p')
end
function p.tablehead2(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_table_head(args, remove_sport(args'team' or ), 's')
end
function p.stripe1(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_stripe1(remove_sport(args1 or ), args'border')
end
function p.boxes(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_boxes(frame, remove_sport(args1 or ),
		args'order' or , args'sep' or ' ')
end
function p.list(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_list(frame, remove_sport(args1 or ),
		args2 or '1', args3 or '2', args4 or '3', args5 or '4', args6 or '5', args'sep' or )
end
function p.check(frame)
	local args = (frame.args1 ~= nil) and frame.args or frame:getParent().args
	return team_check(remove_sport(args1 or ), args2 or )
end
function p.check_data()
	-- In a sandbox, preview check_data
	local results = {'Các vấn đề trong Mô đun:College color/data:'}
	local function problems(msg)
		if msg then
			table.insert(results, msg)
		elseif results2 then
			return table.concat(results, '\n*')
		else
			return 'Không có vấn đề được phát hiện.'
		end
	end
	local data = require(data_module)
	local keys = {}
	for k, _ in pairs(data) do
		table.insert(keys, k)
	end
	table.sort(keys)
	for _, key in ipairs(keys) do
		local val = datakey
		if not (type(key) == 'string' and (type(val) == 'table' or type(val) == 'string')) then
			problems('Loại không hợp lệ cho "' .. tostring(key) .. '"')
		end
		if type(val) == 'table' then
			if not (2 <= #val and #val <= 4) then
				problems('Số lượng tham số đánh số không hợp lệ cho "' .. tostring(key) .. '"')
			end
			for i, v in ipairs(val) do
				if not tostring(v):match('^%x%x%x%x%x%x$') then
					problems('Tham số ' .. i .. ' nên có màu 6 ký tự hex nhưng là "' .. tostring(v) .. '" cho "' .. tostring(key) .. '"')
				end
			end
			for k, v in pairs(val) do
				if type(k) == 'number' then
					if not (1 <= k and k <= 4) then
						problems('Tham số đánh số không hợp lệ cho "' .. tostring(key) .. '"')
					end
				elseif type(k) == 'string' then
					if not (
							k:match('^name1-4$') or
							k:match('^cite2?$') or
							k:match('^order$')
							) then
						problems('Khóa không mong đợi trong bảng cho "' .. tostring(key) .. '"')
					end
				else
					problems('Loại khóa không hợp lệ trong bảng cho "' .. tostring(key) .. '"')
				end
			end
		elseif dataval == nil then
			problems('Bí danh chưa định nghĩa cho "' .. tostring(key) .. '"')
		elseif type(dataval) ~= 'table' then
			problems('Bí danh không phải là một bảng cho "' .. tostring(key) .. '"')
		end
	end
	return problems()
end
function p.testtable(frame)
	local contrasttable_mod = require("Mô đun:College color/contrast")
	return contrasttable_mod._testtable(frame.args)
end
return p