Content deleted Content added
Dissident93 (talk | contribs) No edit summary Tag: Reverted |
sync |
||
Line 3:
-- {{CollegePrimaryColorLink}}, {{CollegeSecondaryHex}},
-- {{CollegeSecondaryStyle}}, {{CollegeSecondaryColorLink}}, and {{NCAA color}}
--
local p = {} local data_module = "Module:College color/data"
local function stripwhitespace(text)
end
local function ucfirst(s)
end
local function bordercss(c, w)
return 'box-shadow: ' .. 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)
return team
end
local function get_colors(team, unknown)
local use_default = {
["free agent"] = 1,
}
if ( team and use_default[team:lower()] ) then
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)
num2 = tonumber(num2:match('[1-3]') or '0')
if ( num )
return colors[num] or colors[num2] or '' end
local function team_style1(team, borderwidth, fontcolor)
local color = '#' .. (colors[3] or colors[2] or '')
if fontcolor and fontcolor == 'auto' then
-- compute the luminosity of the 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 = 'background-color:#' .. (colors[1] or '') .. ';color:' .. (fontcolor or color) .. ';'
-- remove the border if it's nearly white
if ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) then
borderwidth = '0'
end
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. bordercss(color, borderwidth)
end
return style
end
local function team_style2(team, borderwidth, fontcolor)
local color = '#' .. (colors[1] or '')
if fontcolor and fontcolor == 'auto' then
-- compute the luminosity of the 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 = 'background-color:#' .. (colors[3] or colors[2] or '') .. ';color:' .. (fontcolor or color) .. ';'
-- remove the border if it's nearly white
if ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) then
borderwidth = '0'
end
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. bordercss(color, borderwidth)
end
return style
end
local function team_header1(team, borderwidth)
-- 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
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 = (colors[3] or colors[2] 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)
-- 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:none;color:#' .. fontcolor .. ';'
else
return 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
end
end
local function team_table_head(args, team, ctype)
-- set the
local background = (ctype == 'p') and
(colors[3] or colors[2] or 'FFFFFF'):upper()
-- now pick a font color
local fontcolor = ''
-- 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 borderwidth > 0 then
local bc = (ctype == 'p') and
(colors[3] or colors[2] or '') or (colors[1] or '')
if bc ~= 'FFFFFF' then
s = s .. bordercss('#' .. bc, borderwidth)
end
end
local res = '|-\n'
for i=1,50 do
if( args[i] ~= nil ) then
local cstyle = 'scope="col" style="' .. s .. '"'
if
cstyle = cstyle .. ' colspan=' .. args['col' .. i .. 'span']
end
if args['class' .. i ] ~= nil then
cstyle = cstyle .. ' class="' .. args['class' .. i] .. '"'
end
res = res .. '! ' .. cstyle .. ' |' .. args[i] .. '\n'
else
return res .. '|-\n'
end
end
return res .. '<span class="error">Error!</span>\n|-\n'
end
local function team_stripe1(team, borderwidth)
-- set the default scheme
local fontcolor = colors[2] or ''
local bordercolor
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
if( bcontrast > wcontrast
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 < 1.25) then
bordercolor = background
local fontcontrast = (1 + 0.05)/(color2lum(colors[2] or 'FFFFFF') + 0.05)
if (fontcontrast < 1.25) then
fontcolor = colors[2] or 'FFFFFF'
end
end
style = style .. ' border:' .. borderwidth .. 'px solid #' .. bordercolor .. ';'
style = style .. ' border-left: none; border-right: none;'
style = style .. ' box-shadow: inset 0 2px 0 #FEFEFE, inset 0 -2px 0 #FEFEFE;'
end
style = 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';' .. style
return style
end
local function team_boxes(frame, team, order, sep)
local colors = get_colors(team, 'unknown')
if type(colors) ~= 'table' then
return ''
end
local colorboxes = {}
local colororder = {'1','2','3','4','5'}
if order == '' then
order =
namecheck = 1
if order ~= '' then
colororder = mw.text.split(order, '')
end
for k,v in pairs(colororder) do
local i = tonumber(v) or 0
if( namecheck == 0 or colors['name' .. i]) then
if colors[i] then
table.insert(colorboxes,colorbox(colors[i]))
end
end
if (#colorboxes > 0) then
return frame:extensionTag{ name = 'templatestyles', args = { src = 'Legend/styles.css'} } .. table.concat(colorboxes, sep)
end
return ''
end
local function team_list(frame, team, num1, num2, num3, num4, num5, sep)
local colors = get_colors(team, 'unknown')
if type(colors) ~= 'table' then
return ''
end
local nums = {
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 ( nums[i] > 0 ) then
if( colors['name' .. nums[i]]) then
table.insert(colornames,colors['name' .. nums[i]])
table.insert(colorboxes,colorbox(colors[nums[i]] or ''))
end
end
local res = ''
if (#colornames > 0) then
end
res = mw.text.listToText(
colornames,
', ',
#colornames == 2 and ' and ' or ', and '
)
if
res = res .. frame:preprocess('<ref>' .. colors['cite'] .. '</ref>')
end
if (colors['ref']) then
res = res .. '[' .. colors['ref'] .. ']'
end
if (colors['ref2']) then
if (#colornames > 0) then
res = res .. sep
end
if (#colorboxes > 0) then
res = res .. frame:extensionTag{ name = 'templatestyles', args = { src = 'Legend/styles.css'} }
res = res .. table.concat(colorboxes, ' ')
end
return res
end
local function team_check(team, unknown)
end
function p.color(frame)
end
function p.color1(frame)
end
function p.color32(frame)
end
function p.style1(frame)
end
function p.style2(frame)
end
function p.header1(frame)
end
function p.header2(frame)
end
function p.tablehead1(frame)
end
function p.tablehead2(frame)
end
function p.stripe1(frame)
end
function p.boxes(frame)
args['order'] or '', args['sep'] or ' end
function p.list(frame)
args[2] or '1', args[3] or '2', args[4] or '3', args[5] or '4', args[6] or '5', args['sep'] or '') end
function p.check(frame)
end
function p.check_data()
-- In a sandbox, preview {{#invoke:college color|check_data}}
local results = {'Problems in [[Module:College color/data]]:'}
local function problems(msg)
if msg then
table.insert(results, msg)
elseif results[2] then
return table.concat(results, '\n*')
else
return 'No problems detected.'
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 = data[key]
if not (type(key) == 'string' and (type(val) == 'table' or type(val) == 'string')) then
problems('Invalid type for "' .. tostring(key) .. '"')
end
if type(val) == 'table' then
if not (2 <= #val and #val <= 4) then
problems('Invalid number of numbered parameters for "' .. tostring(key) .. '"')
end
for i, v in ipairs(val) do
if not tostring(v):match('^%x%x%x%x%x%x$') then
problems('Parameter [' .. i .. '] should be a 6-hex-digit color but is "' .. tostring(v) .. '" for "' .. 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('Invalid numbered parameter for "' .. tostring(key) .. '"')
end
elseif type(k) == 'string' then
if not (
k:match('^name[1-4]$') or
k:match('^cite2?$') or
k:match('^order$')
) then
problems('Unexpected key in table for "' .. tostring(key) .. '"')
end
else
problems('Invalid key type in table for "' .. tostring(key) .. '"')
end
end
elseif data[val] == nil then
problems('Undefined alias for "' .. tostring(key) .. '"')
elseif type(data[val]) ~= 'table' then
problems('Alias is not a table for "' .. tostring(key) .. '"')
end
end
return problems()
end
function p.testtable(frame)
local contrasttable_mod = require("Module:College color/contrast")
return contrasttable_mod._testtable(frame.args)
end
|