模組:College color
外观
![]() | 此模块被引用於約2,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
![]() | 此模块已评为beta版,可广泛使用。因其新近完成,请谨慎使用,以确保输出结果符合预期。 |
![]() | 此模块使用Lua语言: |
This module implements the colors for college athletic programs and is used by
- {{GotCollegeColorsFor}} - to check if colors are known for the given team.
- {{CollegePrimaryHex}}, {{CollegePrimaryStyle}}, {{CollegePrimaryColorLink}}, {{CollegePrimaryHeader}}
- {{CollegeSecondaryHex}}, {{CollegeSecondaryStyle}}, {{CollegeSecondaryColorLink}}, {{CollegeSecondaryHeader}}
- {{NCAA color}}, {{NCAA color cell}}, {{NCAA secondary color cell}}
- {{Infobox baseball biography/style}}
- {{Infobox basketball biography/style}}
- {{CBB roster/Header}}
- {{College color list}}, {{College color boxes}}
Usage
{{#invoke:College color|color}}
in the NCAA color template{{#invoke:College color|color1}}
in the primary color template{{#invoke:College color|color32}}
in the secondary color template{{#invoke:College color|style1}}
in the primary style template{{#invoke:College color|style2}}
in the secondary style template{{#invoke:College color|header1}}
in the primary header style of an infobox{{#invoke:College color|header2}}
in the secondary header style of an infobox{{#invoke:College color|tablehead1}}
in the primary header style of a wikitable{{#invoke:College color|tablehead2}}
in the secondary header style of a wikitable{{#invoke:College color|stripe1}}
in the primary header style of an infobox{{#invoke:College color|list}}
in the|colors=
field of an infobox{{#invoke:College color|boxes}}
in the|colors=
field of an infobox{{#invoke:College color|check}}
to check to see if a team has defined colors
Updating team colors
See Module:college color/data.
测试用表
--
-- This module implements {{CollegePrimaryHex}}, {{CollegePrimaryStyle}},
-- {{CollegePrimaryColorLink}}, {{CollegeSecondaryHex}},
-- {{CollegeSecondaryStyle}}, {{CollegeSecondaryColorLink}}, and {{NCAA color}}
--
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( c )
c = stripwhitespace(c 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 )
L = 0.2126 * R + 0.7152 * G + 0.0722 * B
return L
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 )
L = 0.2126 * R + 0.7152 * G + 0.0722 * B
return L
end
-- failure, return blank
return ''
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 style = 'background-color:#' .. (colors[1] or '') .. ';color:' .. (fontcolor or color) .. ';'
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. ' border:' .. 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 style = 'background-color:#' .. (colors[3] or colors[2] or '') .. ';color:' .. (fontcolor or color) .. ';'
borderwidth = tonumber(borderwidth or '2') or 0
if (borderwidth > 0 and color ~= '#FFFFFF') then
style = style .. ' border:' .. borderwidth .. 'px solid ' .. color .. ';'
end
return style
end
local function team_header1(team)
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
if( background == 'FFFFFF' ) then
return 'background-color:none;color:#' .. fontcolor .. ';'
else
return 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
end
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:none;color:#' .. fontcolor .. ';'
else
return 'background-color:#' .. background .. ';color:#' .. fontcolor .. ';'
end
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
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 < 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_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.args[1] ~= nil) and frame.args or frame:getParent().args
return team_color(args[1] or '', args[2] or '', args[3] or '')
end
function p.color1(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_color(args[1] or '', '1', '')
end
function p.color32(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_color(args[1] or '', '3', '2')
end
function p.style1(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_style1(args[1] or '', args['border'], args['color'])
end
function p.style2(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_style2(args[1] or '', args['border'], args['color'])
end
function p.header1(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_header1(args[1] or '')
end
function p.header2(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_header2(args[1] or '')
end
function p.stripe1(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_stripe1(args[1] or '', args['border'])
end
function p.check(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_check(args[1] or '', args[2] or '')
end
function p.testtable(frame)
local contrasttable_mod = require("Module:College color/contrast")
return contrasttable_mod._testtable(frame.args)
end
return p