---
-- This module implements , ,
-- , ,
-- , , and
---
local p = {}  
local data_module = "Module:College color/data"  
local function stripwhitespace(text)
return text:match("^%s*(.-)%s*$")
end
local function sRGB ( v )
if (v <= 0.03928) then
v = v / 12.92
else
v = math.pow((v+0.055)/1.055, 2.4)
end
return v
end
local function color2lum( origc )
local c = stripwhitespace(origc or ''):lower  
-- remove leading # (if there is one)
c = mw.ustring.match(c, '^[#]*([a-f0-9]*)$')  
-- split into rgb
local cs = mw.text.split(c or *, *)
if( #cs == 6 ) then
local R = sRGB( (16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2]))/255 )
local G = sRGB( (16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4]))/255 )
local B = sRGB( (16*tonumber('0x' .. cs[5]) + tonumber('0x' .. cs[6]))/255 )  
return 0.2126 * R + 0.7152 * G + 0.0722 * B
elseif ( #cs == 3 ) then
local R = sRGB( (16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[1]))/255 )
local G = sRGB( (16*tonumber('0x' .. cs[2]) + tonumber('0x' .. cs[2]))/255 )
local B = sRGB( (16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[3]))/255 )  
return 0.2126 * R + 0.7152 * G + 0.0722 * B
end  
-- failure
error('Invalid hex color ' .. origc, 2)
end  
local function remove_sport(team)
team = mw.ustring.gsub(team, "%s*<[Bb][Rr][^<>]*>%s*", ' ');
team = mw.ustring.gsub(team, " [Tt]eam$", '')
team = mw.ustring.gsub(team, " [Bb]asketball$", '')
team = mw.ustring.gsub(team, " [Bb]aseball$", '')
team = mw.ustring.gsub(team, " [Cc]ross [Cc]ountry$", '')
team = mw.ustring.gsub(team, " [Ff]ield [Hh]ockey$", '')
team = mw.ustring.gsub(team, " [Ff]ootball$", '')
team = mw.ustring.gsub(team, " [Gg]olf$", '')
team = mw.ustring.gsub(team, " [Gg]ymnastics$", '')
team = mw.ustring.gsub(team, " [Ii]ce [Hh]ockey$", '')
team = mw.ustring.gsub(team, " [Ll]acrosse$", '')
team = mw.ustring.gsub(team, " [Rr]owing$", '')
team = mw.ustring.gsub(team, " [Ss]ki$", '')
team = mw.ustring.gsub(team, " [Ss]occer$", '')
team = mw.ustring.gsub(team, " [Ss]oftball$", '')
team = mw.ustring.gsub(team, " [Ss]wim$", '')
team = mw.ustring.gsub(team, " [Tt]ennis$", '')
team = mw.ustring.gsub(team, " [Tt]rack [Aa]nd [Ff]ield$", '')
team = mw.ustring.gsub(team, " [Vv]olleyball$", '')
team = mw.ustring.gsub(team, " [Ww]restling$", '')
team = mw.ustring.gsub(team, " [Ww]omen's$", '')
team = mw.ustring.gsub(team, " [Mm]en'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_default[team:lower] ) then
colors = {"DCDCDC", "000000"}
else
local all_colors = mw.loadData(data_module)
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_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 colors[num] or colors[num2] or ''
else
return ''
end
end  
local function team_style1(team, borderwidth, fontcolor)
local colors = get_colors(team, nil)  
local color = '#' .. (colors[3] or colors[2] or '')
local .. (colors[1] or '') .. ';color:' .. (fontcolor or color) .. ';'
-- remove the if it's nearly white
if ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) then
border
end
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. ' :' .. borderwidth .. 'px solid ' .. color .. ';'
end  
return style
end  
local function team_style2(team, borderwidth, fontcolor)
local colors = get_colors(team, nil)  
local color = '#' .. (colors[1] or '')
local .. (colors[3] or colors[2] or '') .. ';color:' .. (fontcolor or color) .. ';'
-- remove the if it's nearly white
if ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) then
border
end
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. ' :' .. borderwidth .. 'px solid ' .. color .. ';'
end  
return style
end  
local function team_header1(team, borderwidth)
local colors = get_colors(team, nil)
-- set the default background
local background = (colors[1] or 'FFFFFF'):upper
-- set background to white if it's nearly white
if ((1 + 0.05)/(color2lum(background) + 0.05) < 1.25) then
background = 'FFFFFF'
end
-- 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 style
if( background == 'FFFFFF' ) then .. fontcolor .. ';'
else .. background .. ';color:#' .. fontcolor .. ';'
end  
if borderwidth then
borderwidth = tonumber(borderwidth or '2') or 0
local bordercolor = (colors[3] or colors[2] or 'FFFFFF'):upper
if (borderwidth > 0 and bordercolor ~= 'FFFFFF') then
-- do not add a if it's nearly white
if ((1 + 0.05)/(color2lum(bordercolor) + 0.05) >= 1.25) then
style = style .. ' :' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
end
end
end
return style
end  
local function team_header2(team)
local colors = get_colors(team, nil)
-- set the default background
local background = (colors[3] or colors[2] or 'FFFFFF'):upper
-- set background to white if it's nearly white
if ((1 + 0.05)/(color2lum(background) + 0.05) < 1.25) then
background = 'FFFFFF'
end
-- if the background is white, then use the primary background instead
if( background == 'FFFFFF' ) then
background = (colors[1] or 'FFFFFF'):upper
end
-- 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
if( background == 'FFFFFF' ) then
return 'background-color:;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
(colors[1] or 'FFFFFF'):upper or
(colors[3] or colors[2] 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 = '|-\n'
for i=1,50 do
if( args[i] ~= nil ) then
local c
if args['col' .. i .. 'span'] ~= nil then
cstyle = cstyle .. 'col' .. i .. 'span']
end
res = res .. '! ' .. cstyle .. ' |' .. args[i] .. '\n'
else
return res .. '|-\n'
end
end
return res .. '<span>Error!</span>\n|-\n'  
end  
local function team_stripe1(team, borderwidth)
local colors = get_colors(team, nil)  
-- set the default scheme
local background = colors[1] or ''
local fontcolor = colors[2] or ''
local bordercolor = (colors[3] or colors[2] or ''):upper
borderwidth = tonumber(borderwidth or '3') or 0  
-- if there is no tertiary color, then pick a font color
if (colors[3] == nil) then
-- compute the luminosity of the background
local lum = color2lum(colors[1])
-- 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