stringtranslate.com

Módulo:Color universitario

---- 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*$")endlocal function ucfirst(s)local first = s:sub(1, 1)local others = s:sub(2, -1)return first:upper() .. othersendlocal function bordercss(c, w)local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 ' .. c .. ', inset -' .. w .. 'px -' .. w .. 'px 0 ' .. creturn 'box-shadow: ' .. s .. ' !important;'endlocal function sRGB ( v )if (v <= 0.03928) thenv = v / 12.92elsev = math.pow((v+0.055)/1.055, 2.4)endreturn vendlocal 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 rgblocal cs = mw.text.split(c or '', '')if( #cs == 6 ) thenlocal 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 * Belseif ( #cs == 3 ) thenlocal 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 * Bend-- failureerror('Invalid hex color ' .. origc, 2)endlocal 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 teamendlocal 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 = nilif ( team and use_default[team:lower()] ) thencolors = {"DCDCDC", "000000"}elselocal all_colors = mw.loadData(data_module)colors = all_colors[team]if ( colors and type(colors) == 'string' ) thencolors = all_colors[colors]endendreturn colors or unknownendlocal 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 ) thenreturn colors[num] or colors[num2] or ''elsereturn ''endendlocal function team_style1(team, borderwidth, fontcolor)local colors = get_colors(team, nil)local color = '#' .. (colors[3] or colors[2] or '')if fontcolor and fontcolor == 'auto' then-- compute the luminosity of the backgroundlocal lum = color2lum(colors[1] or '')-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '#000000 !important;'elsefontcolor = '#FFFFFF !important;'endendlocal style = 'background-color:#' .. (colors[1] or '') .. ' !important;' .. 'color:' .. (fontcolor or color) .. ' !important;'-- remove the border if it's nearly whiteif ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) thenborderwidth = '0'endborderwidth = tonumber(borderwidth or '2') or 0if (borderwidth > 0 and color ~= '#FFFFFF') thenstyle = style .. bordercss(color, borderwidth)endreturn styleendlocal function team_style2(team, borderwidth, fontcolor)local colors = get_colors(team, nil)local color = '#' .. (colors[1] or '')if fontcolor and fontcolor == 'auto' then-- compute the luminosity of the backgroundlocal lum = color2lum(colors[3] or colors[2] or '')-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '#000000 !important;'elsefontcolor = '#FFFFFF !important;'endendlocal style = 'background-color:#' .. (colors[3] or colors[2] or '') .. ' !important;' .. 'color:' .. (fontcolor or color) .. ' !important;'-- remove the border if it's nearly whiteif ((1 + 0.05)/(color2lum(color) + 0.05) < 1.25) thenborderwidth = '0'endborderwidth = tonumber(borderwidth or '2') or 0if (borderwidth > 0 and color ~= '#FFFFFF') thenstyle = style .. bordercss(color, borderwidth)endreturn styleendlocal function team_header1(team, borderwidth)local colors = get_colors(team, nil)-- set the default backgroundlocal background = (colors[1] or 'FFFFFF'):upper()-- set background to white if it's nearly whiteif ((1 + 0.05)/(color2lum(background) + 0.05) < 1.25) thenbackground = 'FFFFFF'end-- now pick a font colorlocal fontcolor = '000000'-- compute the luminosity of the backgroundlocal lum = color2lum(background)-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '000000'elsefontcolor = 'FFFFFF'endlocal styleif( background == 'FFFFFF' ) thenstyle = 'background-color:none !important;color:#' .. fontcolor .. ' !important;'elsestyle = 'background-color:#' .. background .. ' !important;' .. 'color:#' .. fontcolor .. ' !important;'endif borderwidth thenborderwidth = tonumber(borderwidth or '2') or 0local 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 whiteif ((1 + 0.05)/(color2lum(bordercolor) + 0.05) >= 1.25) thenstyle = style .. bordercss('#' .. bordercolor, borderwidth)endendendreturn styleendlocal function team_header2(team)local colors = get_colors(team, nil)-- set the default backgroundlocal background = (colors[3] or colors[2] or 'FFFFFF'):upper()-- set background to white if it's nearly whiteif ((1 + 0.05)/(color2lum(background) + 0.05) < 1.25) thenbackground = 'FFFFFF'end-- if the background is white, then use the primary background insteadif( background == 'FFFFFF' ) thenbackground = (colors[1] or 'FFFFFF'):upper()end-- now pick a font colorlocal fontcolor = '000000'-- compute the luminosity of the backgroundlocal lum = color2lum(background)-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '000000'elsefontcolor = 'FFFFFF'endif( background == 'FFFFFF' ) thenreturn 'background-color:none !important;color:#' .. fontcolor .. ' !important;'elsereturn 'background-color:#' .. background .. ' !important;' .. 'color:#' .. fontcolor .. ' !important;'endendlocal function team_table_head(args, team, ctype)local colors = get_colors(team, nil)local borderwidth = tonumber(args['border']) or 0-- set the default backgroundlocal background = (ctype == 'p') and(colors[1] or 'FFFFFF'):upper() or(colors[3] or colors[2] or 'FFFFFF'):upper()-- now pick a font colorlocal fontcolor = ''-- compute the luminosity of the backgroundlocal lum = color2lum(background)-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '#000000 !important;'elsefontcolor = '#FFFFFF !important;'endlocal s = 'background-color:#' .. background .. ' !important;' .. 'color:' .. (args['color'] or fontcolor) .. ' !important;'if borderwidth > 0 thenlocal bc = (ctype == 'p') and (colors[3] or colors[2] or '') or (colors[1] or '')if bc ~= 'FFFFFF' thens = s .. bordercss('#' .. bc, borderwidth)endendlocal res = '|-\n'for i=1,50 doif( args[i] ~= nil ) thenlocal cstyle = 'scope="col" style="' .. s .. '"'if args['col' .. i .. 'span'] ~= nil thencstyle = cstyle .. ' colspan=' .. args['col' .. i .. 'span']endif args['class' .. i ] ~= nil thencstyle = cstyle .. ' class="' .. args['class' .. i] .. '"'endres = res .. '! ' .. cstyle .. ' |' .. args[i] .. '\n'elsereturn res .. '|-\n'endendreturn res .. '<span class="error">Error!</span>\n|-\n'endlocal function team_stripe1(team, borderwidth)local colors = get_colors(team, nil)-- set the default schemelocal 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 colorif (colors[3] == nil) then-- compute the luminosity of the backgroundlocal lum = color2lum(colors[1])-- compute the contrast with white and blacklocal wcontrast = (1 + 0.05)/(lum + 0.05)local bcontrast = (lum + 0.05)/(0 + 0.05)-- select the text color with the best contrastif( bcontrast > wcontrast + 1.25 ) thenfontcolor = '000000'elsefontcolor = 'FFFFFF'endend-- finally build the style stringlocal style = ''if (borderwidth > 0) then-- use the primary as the border if the border is white or close to whitelocal bordercontrast = (1 + 0.05)/(color2lum(bordercolor) + 0.05)if (bordercontrast < 1.25) thenbordercolor = backgroundlocal fontcontrast = (1 + 0.05)/(color2lum(colors[2] or 'FFFFFF') + 0.05)if (fontcontrast < 1.25) thenfontcolor = colors[2] or 'FFFFFF'endend style = style .. ' border:' .. borderwidth .. 'px solid #' .. bordercolor .. ' !important;' style = style .. ' border-left: none !important; border-right: none !important;' style = style .. ' box-shadow: inset 0 2px 0 #FEFEFE, inset 0 -2px 0 #FEFEFE !important;'endstyle = 'background-color:#' .. background .. ' !important;' .. 'color:#' .. fontcolor .. ' !important;' .. stylereturn styleendlocal function team_boxes(frame, team, order, sep)local function colorbox( h )local r = mw.html.create('')r:tag('span'):addClass('legend-color'):css('background-color', '#' .. (h or '') .. ' !important'):wikitext('&nbsp;')return tostring(r)endlocal colors = get_colors(team, 'unknown')if type(colors) ~= 'table' thenreturn ''endlocal colorboxes = {}local colororder = {'1','2','3','4','5'}local namecheck = 0if order == '' thenorder = colors['order'] or ''namecheck = 1endif order ~= '' thencolororder = mw.text.split(order, '')endfor k,v in pairs(colororder) dolocal i = tonumber(v) or 0if(namecheck == 0 or colors['name' .. i]) thenif colors[i] thentable.insert(colorboxes,colorbox(colors[i]))endendendif (#colorboxes > 0) thenreturn frame:extensionTag{ name = 'templatestyles', args = { src = 'Legend/styles.css'} } .. table.concat(colorboxes, sep)endreturn ''endlocal function team_list(frame, team, num1, num2, num3, num4, num5, sep)local function colorbox( h )local r = mw.html.create('')r:tag('span'):addClass('legend-color'):css('background-color', '#' .. (h or '') .. ' !important'):wikitext('&nbsp;')return tostring(r)endlocal colors = get_colors(team, 'unknown')if type(colors) ~= 'table' thenreturn ''endlocal 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 ~= '') thencolororder = mw.text.split(order, '')endfor k,v in pairs(colororder) dolocal i = tonumber(v) or 0if ( nums[i] > 0 ) thenif(colors['name' .. nums[i]]) thentable.insert(colornames,colors['name' .. nums[i]])table.insert(colorboxes,colorbox(colors[nums[i]] or ''))endendendlocal res = ''if (#colornames > 0) thencolornames[1] = ucfirst(colornames[1])endres = mw.text.listToText(colornames,',&nbsp;',#colornames == 2 and '&nbsp;and&nbsp;' or ',&nbsp;and&nbsp;')if (colors['cite']) thenres = res .. frame:preprocess('<ref>' .. colors['cite'] .. '</ref>')endif (colors['ref']) thenres = res .. '[' .. colors['ref'] .. ']'endif (colors['ref2']) thenres = res .. '[' .. colors['ref2'] .. ']'endif (#colornames > 0) thenres = res .. sependif (#colorboxes > 0) thenres = res .. frame:extensionTag{ name = 'templatestyles', args = { src = 'Legend/styles.css'} }res = res .. table.concat(colorboxes, '&nbsp;')endreturn resendlocal function team_check(team, unknown)local colors = get_colors(team, unknown)if type(colors) == 'table' thenreturn 'known'elsereturn unknownendendfunction p.color(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_color(remove_sport(args[1] or ''), args[2] or '', args[3] or '')endfunction p.color1(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_color(remove_sport(args[1] or ''), '1', '')endfunction p.color32(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_color(remove_sport(args[1] or ''), '3', '2')endfunction p.style1(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_style1(remove_sport(args[1] or ''), args['border'], args['color'])endfunction p.style2(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_style2(remove_sport(args[1] or ''), args['border'], args['color'])endfunction p.header1(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_header1(remove_sport(args[1] or ''), args['border'])endfunction p.header2(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_header2(remove_sport(args[1] or ''))endfunction p.tablehead1(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_table_head(args, remove_sport(args['team'] or ''), 'p')endfunction p.tablehead2(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_table_head(args, remove_sport(args['team'] or ''), 's')endfunction p.stripe1(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_stripe1(remove_sport(args[1] or ''), args['border'])endfunction p.boxes(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_boxes(frame, remove_sport(args[1] or ''),args['order'] or '', args['sep'] or '&nbsp;')endfunction p.list(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_list(frame, remove_sport(args[1] or ''),args[2] or '1', args[3] or '2', args[4] or '3', args[5] or '4', args[6] or '5', args['sep'] or '')endfunction p.check(frame)local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().argsreturn team_check(remove_sport(args[1] or ''), args[2] or '')endfunction 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 thentable.insert(results, msg)elseif results[2] thenreturn table.concat(results, '\n*')elsereturn 'No problems detected.'endendlocal data = require(data_module)local keys = {}for k, _ in pairs(data) dotable.insert(keys, k)endtable.sort(keys)for _, key in ipairs(keys) dolocal val = data[key]if not (type(key) == 'string' and (type(val) == 'table' or type(val) == 'string')) thenproblems('Invalid type for "' .. tostring(key) .. '"')endif type(val) == 'table' thenif not (2 <= #val and #val <= 4) thenproblems('Invalid number of numbered parameters for "' .. tostring(key) .. '"')endfor i, v in ipairs(val) doif not tostring(v):match('^%x%x%x%x%x%x$') thenproblems('Parameter [' .. i .. '] should be a 6-hex-digit color but is "' .. tostring(v) .. '" for "' .. tostring(key) .. '"')endendfor k, v in pairs(val) doif type(k) == 'number' thenif not (1 <= k and k <= 4) thenproblems('Invalid numbered parameter for "' .. tostring(key) .. '"')endelseif type(k) ==  'string'  entonces si no ( k : match ( '^name[1-4]$' ) o k : match ( '^cite2?$' ) o k : match ( '^order$' ) ) entonces problemas ( 'Clave inesperada en la tabla para "' .. tostring ( key ) .. '"' ) fin de lo contrario problemas ( 'Tipo de clave no válido en la tabla para "' .. tostring ( key ) .. '"' ) fin fin de lo contrario si datos [ val ] == nil entonces problemas ( 'Alias ​​no definido para "' .. tostring ( key ) .. '"' ) de lo contrario si tipo ( datos [ val ]) ~= 'tabla' entonces problemas ( 'El alias no es una tabla para "' .. tostring ( key ) .. '"' ) fin fin de retorno problemas () fin de la función p . testtable ( frame ) local contrasttable_mod = require ( " Módulo : Color / contraste universitario" ) return contrasttable_mod._testtable ( frame.args ) end return p