Module:Ctb

From blackwiki
Revision as of 14:02, 25 August 2014 by blackwiki>Vladimir891
Jump to navigation Jump to search
-- Toolbox for various effects
-- More features are being added

-- For function 'm'
 -- arg[1] = comma-delimited list of parameters
 -- arg[2] = comma-delimited list of attributes
 
 -- if arg[1] = "Cbr" then it draws a smooth, blended color progression between the two specified colors.
 --             Parameters (arg[2]) : Color1_Red, Color1_Green, Color1_Blue,  Color2_Red, Color2_Green, Color2_Blue, Width (em), Units (increments), Height (em)
 -- if arg[1] = "Spc" then it draws a smooth, blended spectrum progressing from red to yellow to green to blue to purple,  adjusted based on specified parameters
 --             Parameters (arg[2]) :  Pastel_Red, Pastel_Green, Pastel_Blue,  Unused_Red, Unused_Green, Unused_Blue, Width (em), Units (increments), Height (em) 
 
 -- For function 'GetColor'
  -- arg[1] = name of color (e.g., "Old Moss") ; returns hexadecimal string for that color which can be used for HTML output
 
 local p = {}
 
 
 
function p.m( frame )
	local args=frame.args
	local list = args[1]
	local attr = args[2]
 
	output = ""
 
    local Elems = 0
	if list == nil then list = "" end    
	local Dlist = mw.text.split( list, ",")
	if attr == nil then attr = "" end
	local Dattr = mw.text.split( attr, ",")	
 
    for i = 1, #Dlist do
		if 	Dlist[i] == "Cbr" then  -- Color bar with a progression between two colors
			local ClR = tonumber(Dattr[1]) or 0xFF
			local ClG = tonumber(Dattr[2]) or 0xFF
			local ClB = tonumber(Dattr[3]) or 0xFF
			local ClR2 = tonumber(Dattr[4]) or 0x00
			local ClG2 = tonumber(Dattr[5]) or 0x00
			local ClB2 = tonumber(Dattr[6]) or 0x00
			local BarWidth = tonumber(Dattr[7]) or 22.0  -- default to 22.0em			
			local Units = tonumber(Dattr[8]) or 20	
			local BarHeight = tonumber(Dattr[9]) or 1.0 -- default to 1.0em
			local UnitWidth = BarWidth / Units
			local DR = (ClR2 - ClR) / Units 
			local DG = (ClG2 - ClG) / Units 
			local DB = (ClB2 - ClB) / Units 
			output=string.format('<table style="width:%dem;height:%dem;border:0px;border-spacing:0px;padding:0px;"><tr>',BarWidth,BarHeight)			
			for x=1,Units do  
				output=output..string.format('<td style="width:%dem;height:%dem;background-color:#%02x%02x%02x;"></td>',UnitWidth,BarHeight,ClR,ClG,ClB)
				ClR=ClR+DR
				ClG=ClG+DG
				ClB=ClB+DB
			end	
			output=output.."</tr></table>"
		end	
		if 	Dlist[i] == "Spc" then  -- Color bar with a smooth spectrum 
			local PsR = tonumber(Dattr[1]) or 0.0  -- pastel weighting factors ( float value between 0 - 1; determines how pastel the color spectrum is ( 0 = none; 1 = fully pastel)
			local PsG = tonumber(Dattr[2]) or 0.0
			local PsB = tonumber(Dattr[3]) or 0.0
			local ClR2 = tonumber(Dattr[4]) or 0.0 -- currently unused (future expansion)
			local ClG2 = tonumber(Dattr[5]) or 0.0
			local ClB2 = tonumber(Dattr[6]) or 0.0
			local BarWidth = tonumber(Dattr[7]) or 22.0  -- default to 22.0em			
			local Units = tonumber(Dattr[8]) or 20	
			local BarHeight = tonumber(Dattr[9]) or 1.0 -- default to 1.0em
			local UnitWidth = BarWidth / Units
			local ThisUnits = Units / 4
			
			colors = {}
			table.insert(colors, {255.0, 0.0, 0.0})
			table.insert(colors, {255.0, 255.0, 0.0})
			table.insert(colors, {0.0, 255.0, 0.0})
			table.insert(colors, {0.0, 0.0, 255.0})
			table.insert(colors, {255.0, 0.0, 255.0})
			
			local function draw(color1, color2)
				local ThisR = color1[1] + (color1[1] == 0.0 and PsR*0.5 or 0.0)
				local ThisG = color1[2] + (color1[2] == 0.0 and PsG*0.5 or 0.0)
				local ThisB = color1[3] + (color1[3] == 0.0 and PsB*0.5 or 0.0)
				local ThisR2 = color2[1] + (color2[1] == 0.0 and PsR*0.5 or 0.0)
				local ThisG2 = color2[2] + (color2[2] == 0.0 and PsG*0.5 or 0.0)
				local ThisB2 = color2[3] + (color2[3] == 0.0 and PsB*0.5 or 0.0)
				local results = {}
				local DR = (ThisR2 - ThisR) / ThisUnits 
				local DG = (ThisG2 - ThisG) / ThisUnits
				local DB = (ThisB2 - ThisB) / ThisUnits 			
				for x=1,ThisUnits do  
					local ThisRD = math.floor(ThisR)
					local ThisGD = math.floor(ThisG)
					local ThisBD = math.floor(ThisB)
					table.insert(
						results,
						string.format(
							'<td style="width:%dem;height:%dem;background-color:#%02x%02x%02x;"></td>',
							UnitWidth,BarHeight,ThisRD,ThisGD,ThisBD
						)
					)
					ThisR=ThisR+DR
					ThisG=ThisG+DG
					ThisB=ThisB+DB
				end	
				return table.concat(results, '')
			end
			
			results = {}
			for i = 1, #colors - 1 do
				table.insert(results, draw(colors[i], colors[i + 1]))
			end
			output = string.format(
				'<table style="width:%dem;height:%dem;border:0px;border-spacing:0px;padding:0px;"><tr>',
				BarWidth,BarHeight
			) .. table.concat(results, '') .. '</tr></table>'
		end			
	end
 
    return output  
 
end

 -- return hexadecimal string for a color based on name
 --     function looks up name in an array and returns the numerical RGB values packed into a hexadecimal string suitable for HTML output
function p.GetColor( frame )
	-- These two arrays store the names and hexcodes for a list of colors.
	-- (WORK IN PROGRESS. A lot more will be added as time permits)
	-- To keep these two arrays manageable for future expansion, use tabs to line entries up so that the color name in the first array is vertically aligned with the corresponding hexcode 
	-- in the second array. Otherwise, it becomes very difficult to find the corresponding entry
	ColorNameList = {"White",	"Red",		"Yellow-Green",			"Green",	"Olive",	"Olivine",	"Dark Olive",	"Olive Drab",	"Olive Drab Camo",	"Black Olive",	"Old Moss",	"Green-Yellow",	"Lime Green",	"Lime",		"Lawn Green",	"Chartreuse",	"Spring Green",	"Medium Spring Green",	"LightGreen",	"Pale Green",	"Blue"}
	HexStringList = {"FFFFFF",	"FF0000",	"ADFF2F",				"00FF00",	"808000",	"9AB973",	"556B2F",		"6B8E23",		"544F3D",			"3B3C36",		"52652F",	"9ACD32",		"32CD32",		"00FF00",	"7CFC00",		"7FFF00",		"00 FF 7F",		"00FA9A",				"90EE90",		"98FB98",		"0000FF"}
	local args=frame.args
	local ColorName = args[1]
	local HexString = "000000"
 
	output = ""
 
	if ColorName == nil then ColorName = ""   
	else
		for i = 1, #ColorNameList do
  			if ColorNameList[i] == ColorName then
    			HexString = HexStringList[i]
    			break
  			end
		end
	end

	return HexString

end

return p