Module:Color

-- --Start Module local p = {} --Get Other Required Modules local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local wrapHtml = require('Module:String').wrapHtml local checkType = require('Module:LibraryUtil').checkType local string = require('Module:String') local table = require('Module:Table')

- -- Category:Single Color templates -- -- Changes text color to a fixed color - local aliases = { ['bl']='black', ['blck']='black', ['black']='black', ['db']='dark_blue', ['drk_blu']='dark_blue', ['drkblu']='dark_blue', ['darkblue']='dark_blue', ['dark_blue']='dark_blue', ['dg']='dark_green', ['drk_grn']='dark_green', ['drkgrn']='dark_green', ['darkgreen']='dark_green', ['dark_green']='dark_green', ['da']='dark_aqua', ['drk_aqu']='dark_aqua', ['drkaqu']='dark_aqua', ['darkaqua']='dark_aqua', ['dark_aqua']='dark_aqua', ['dr']='dark_red', ['drkrd']='dark_red', ['drk_rd']='dark_red', ['darkred']='dark_red', ['dark_red']='dark_red', ['dp']='dark_purple', ['drkprpl']='dark_purple', ['drk_prpl']='dark_purple', ['darkpurple']='dark_purple', ['dark_purple']='dark_purple', ['gld']='gold', ['gd']='gold', ['gold']='gold', ['gry']='gray', ['grey']='gray', ['gray']='gray', ['dgry']='dark_gray', ['drkgry']='dark_gray', ['drk_gry']='dark_gray', ['darkgrey']='dark_gray', ['dark_grey']='dark_gray', ['darkgray']='dark_gray', ['dark_gray']='dark_gray', ['b']='blue', ['blu']='blue', ['blue']='blue', ['g']='green', ['gr']='green', ['grn']='green', ['green']='green',

['aq']='aqua', ['aqu']='aqua', ['aqua']='aqua', ['r']='red', ['rd']='red', ['red']='red',

['lp']='light_purple', ['lghtprpl']='light_purple', ['lght_prpl']='light_purple', ['lightpurple']='light_purple', ['light_purple']='light_purple', ['y']='yellow', ['ye']='yellow', ['yel']='yellow', ['yllw']='yellow', ['yellow']='yellow', ['w']='white', ['wh']='white', ['wht']='white', ['whte']='white', ['white']='white',

['s']='silver', ['slvr']='silver', ['slv']='silver', ['silver']='silver', ['t']='turquoise', ['turq']='turquoise', ['trqse']='turquoise', ['turqse']='turquoise', ['turquoise']='turquoise', ['br']='bronze', ['brnze']='bronze', ['bronze']='bronze', ['o']='orange', ['or']='orange', ['ornge']='orange', ['orng']='orange', ['orange']='orange', ['orang']='orange', ['p']='pink', ['pi']='pink', ['pnk']='pink', ['pk']='pink', ['pn']='pink', }

local color_list = { --Minecraft Vanilla ['black'] = "#000000", ['dark_blue'] = "#0000AA", ['dark_green'] = "#00AA00", ['dark_aqua'] = "#00AAAA", ['dark_red'] = "#AA0000", ['dark_purple'] = "#AA00AA", ['gold'] = "#FFAA00", ['gray'] = "#AAAAAA", ['dark_gray'] = "#555555", ['blue'] = "#5555FF", ['green'] = "#55FF55", ['aqua'] = "#55FFFF", ['red'] = "#FF5555", ['light_purple'] = "#FF55FF", ['yellow'] = "#FFFF55", ['white'] = "#FFFFFF", --Wiki Custom ['silver'] = "#C0C0C0", ['turquoise'] = "#00AAAA", ['bronze'] = "#803600", ['orange'] = "#FF921E", ['pink'] = "#FF55FF", }

local tooltip_formatting = { black = 0, dark_blue = 1, dark_green = 2, dark_aqua = 3, dark_red = 4, dark_purple = 5, gold = 6, gray = 7, dark_gray = 8, blue = 9, green = 'a', aqua = 'b', red = 'c', light_purple = 'd', yellow = 'e', white = 'f', bold = 'l', underline = 'n', strikethrough = 'm', italic = 'o', reset = 'r', }

function p.colorTemplates(frame) local args = getArgs(frame) local text = args[1] or '' local color = args["color"] return string.pcall(p._colorTemplates, color, text) end

function p._colorTemplates(color, text) checkType('_colorTemplates', 1, color, { 'string', 'number', 'table' }) checkType('_colorTemplates', 2, text, { 'string', 'table', 'number' }) if type(color) == "table" and #color >= 3 then color = table.concat{ #color == 3 and 'rgb(' or 'rgba(', table.concat(color, ', '), ')' }	end	color = color_list[string.gsub(aliases[string.lower(color)] or string.lower(color), ' ', '_')] or color	return wrapHtml(text, 'font', { style={ color=color } }) end p.makeColor = p._colorTemplates p.colorText = p._colorTemplates p.wrapColor = p._colorTemplates

- -- Changes formatting into tooltip color prefix -- Used in modules - function p.tooltipFormatting(color, text) checkType('_colorTemplates', 1, color, { 'string' }) checkType('_colorTemplates', 2, text, { 'string', 'table', 'number' }, true) color = tooltip_formatting[string.gsub(aliases[string.lower(color)] or string.lower(color), ' ', '_')] or color return text and '&'..color..text or '&'..color end - -- Template:Color Conversion -- -- Converts input between different coding techniques -- Can also be used in modules - function p.color_conversion(frame) local args = getArgs(frame) local output_type = args[1] local color = args[2] return mw.getCurrentFrame:preprocess(string.pcall(p._color_conversion, output_type, color, true)) end

function p._color_conversion(output_type, color, on_page) --Local variables local color_output = '' local color_digit = "" --Categorization based on output_type --hex if string.lower(output_type) == "hex" then local letters = { [10] = 'A', [11] = 'B', [12] = 'C', [13] = 'D', [14] = 'E', [15] = 'F'		} --delete everything except numbers and commas color = string.gsub(color, "rgba?%((.*)%)", "%1") --split the numbers apart color = mw.text.split( color, "%s*,%s*") --check if input is valid if tonumber(color[1]) < 0 or tonumber(color[1]) > 255 or tonumber(color[2]) < 0 or tonumber(color[2]) > 255 or tonumber(color[3]) < 0 or tonumber(color[3]) > 255 then color_output = string.error("color was invalid \"rgb(%q, %q, %q)", color[1], color[2], color[3])		else			for i=3,1,-1 do				--repeat this for all 3 numbers				for j=1,2,1 do					--repeat this 2 times for every number					color_digit = color[i] % 16					color_digit = letters[color_digit] or color_digit					color[i] = math.floor(color[i] / 16)					color_output = color_digit .. color_output				end			end			--add # to the front			if on_page then				color_output = mw.text.nowiki("#") .. color_output			else color_output = "#" .. color_output			end		end	--rgb	elseif string.match(string.lower(output_type), 'rgba?') then		local letters = {			['A'] = 10,			['B'] = 11,			['C'] = 12,			['D'] = 13,			['E'] = 14,			['F'] = 15		}		--set color_output to base value		if string.lower(output_type) == "rgba" then			color_output = "rgba("		else			color_output = "rgb("		end		--convert all letters to uppercase color = string.upper(color) --remove # character color = string.gsub(color, "#", "") if string.match(color, "%x") then if #color == 3 then for i=1,3,1 do					local color_help = {} color_help[i] = string.sub(color, i, i)					color_help[i] = letters[color_help[i]] or color_help[i] color_output = color_output .. color_help[i] + color_help[i] * 16 if i ~= 3 then color_output = color_output .. "," end end else local color_storage for i=1,6,1 do					local color_help = {} color_help[i] = string.sub(color, i, i)					color_help[i] = letters[color_help[i]] or color_help[i] if i % 2 == 1 then color_storage = color_help[i] * 16 end if i % 2 == 0 then color_storage = color_storage + color_help[i] color_output = color_output .. color_storage end if i == 3 or i == 5 then color_output = color_output .. ","					end end end --close the bracket if string.lower(output_type) == "rgba" then color_output = color_output .. ",1)"			else				color_output = color_output .. ")" end else color_output = "Template Error: color code was invalid \""..color.."\" " end elseif string.match(string.lower(output_type), 'int') then local letters = { ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15 }		if color:lower:match('rgb?') then --delete everything except numbers and commas color = string.gsub(color, "rgba?%((.*)%)", "%1") --split the numbers apart color = mw.text.split(color, "%s*,%s*") --check if input is valid if tonumber(color[1]) < 0 or tonumber(color[1]) > 255 or tonumber(color[2]) < 0 or tonumber(color[2]) > 255 or tonumber(color[3]) < 0 or tonumber(color[3]) > 255 then color_output = "Template Error: Invalid RGB color \"rgb("..color[1]..","..color[2]..","..color[3]..")\" " else color_output = tonumber(color[1]) * 65536 + tonumber(color[2]) * 256 + tonumber(color[3]) end else --convert all letters to uppercase color = string.upper(color) if string.match(color, "(#)(%x)") then --remove # character color = string.gsub(color, "#", "") if #color == 3 then local color_help = {} for i=1,3,1 do						color_help[i] = string.sub(color, i, i)						color_help[i] = letters[color_help[i]] or color_help[i] end color_output = color_help[1] * 65536 * 17 + color_help[2] * 256 * 17 + color_help[3] * 17 else local color_help = {} for i=1,6,1 do						color_help[i] = string.sub(color, i, i)						color_help[i] = letters[color_help[i]] or color_help[i] if i % 2 == 1 then color_help[i] = color_help[i] * 16 end end color_output = (color_help[1] + color_help[2]) * 65536 + (color_help[3] + color_help[4]) * 256 + (color_help[5] + color_help[6]) end else color_output = color end end --if arg[1] is invalid else color_output = string.error('Invalid Color Output %q', output_type) end return color_output end

function p.test(frame) local args = getArgs(frame) local color = args[1] return string.gsub(color "(%d*,%d*,%d*),.*", "%1") end

- -- Template:Color -- -- Changes text color - function p.color(frame) local args = getArgs(frame) local text = args[2] or '' local color = args["color"] or args['c'] or args[1] return string.pcall(p._colorTemplates, color, text) end

function p._color(color, text) checkType('_color', 1, color, { 'string', 'table', 'number' }) checkType('_color', 2, text, { 'string', 'table', 'number' }) return string.wrapHtml(text, ' ', { style={ color=color } }) end - -- Template:Color Display -- -- Displays input color as HEX code and a small preview - function p.color_display(frame) local args = getArgs(frame) local color = args[1] local preserveCase = yesno(args["preserve_case"] or args["preservecase"] or args["case"] or args["pc"] or args["c"]) return p._color_display(color, preserveCase) end

function p._color_display(color, preserveCase) if color:find("rgb") then color = p._color_conversion("hex", color) end --remove # character if color:find("#") then color = color:gsub("#", "") end if not preserveCase then color = color:upper end return string.wrapHtml{ (' '):rep(4), 'span', { 			style={ ['background-color']='#'..color }		},	}..pipeline(		table.concat{ ' #'..color },		'code',		string.wrapTag	) end - -- Template:Armor Colors -- -- Creates an invisible table with armor colors. Used by - function p.armorColors(frame) local args = getArgs(frame) --create local variables local inputs = string.lower(args[1]) local all_the_same = yesno(args["all"]) --store preformated strings local preformated = { ['fairy'] = string.wrapTag(table.map({ {'A', ' ', { style={ color="#FF66B2" }}}, {'N', ' ', { style={ color="#FF3399" }}}, {'I', ' ', { style={ color="#FF33FF" }}}, {'M', ' ', { style={ color="#FF66FF" }}}, {'A', ' ', { style={ color="#9933FF" }}}, {'T', ' ', { style={ color="#FF99FF" }}}, {'E', ' ', { style={ color="#FF66B2" }}}, {'D', ' ', { style={ color="#FF99CC" }}}, }, string.wrapHtml), 'b'), ['crystal'] = string.wrapTag(table.map({ { 'C', ' ', { style={ color='#FCF3FF' }}}, { 'H', ' ', { style={ color='#E5D1ED' }}}, { 'A', ' ', { style={ color='#C9A3D4' }}}, { 'N', ' ', { style={ color='#A875BD' }}}, { 'G', ' ', { style={ color='#8E51A6' }}}, { 'I', ' ', { style={ color='#6A2C82' }}}, { 'N', ' ', { style={ color='#5D1C78' }}}, { 'G', ' ', { style={ color='#46085E' }}} }, string.wrapHtml), 'b'), }	local function makeCell(htmlObj, piece, text) if text then htmlObj:tag('tr') :tag('td') :css{ style='align:right;' } :tag('b') :wikitext(piece) :done :done :tag('td') :wikitext(p._color_display(text)) :done :done end end if preformated[inputs] then return preformated[inputs] else inputs = mw.text.split(inputs, ",") if all_the_same then return p._color_display(inputs[1]) else local ret = mw.html.create('table') local t = { 'Helmet:', 'Chestplate:', 'Leggings:', 'Boots:', }			for i, v in ipairs(t) do				makeCell(ret, v, inputs[i]) end return tostring(ret:done) end end end

-- -- -- Makes colored note sequence. Used for Harp songs.

function p.noteSequence(frame) local args = getArgs(frame) local sequence = args[1] local str = {} local colors = { "#ff78e8", "#ffff00", "#00ff00", "#008000",		"#9500a8", "#5900ff", "#00b3ff", [" "] = "#000000",	}	for i=1,#sequence,1 do		local num = tonumber(sequence:sub(i,i)) if num and num >= 1 and num <= 7 then str[i] = string.wrapHtml(p._color(colors[num], sequence:sub(i,i)), ' ').."&thinsp;" elseif sequence:sub(i,i) == " " then str[i] = "&emsp;" else str[i] = sequence:sub(i,i) end end return string.wrapHtml(		string.wrapTag(str, 'b'), 		' ', { 			style={				['background']='rgba(0, 0, 0, 0.35)';				['padding']='1.5px';				['border-radius']='3px';			}		}	) end

--Finish Module return p