Module:Element button

From Sandboxels Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Element button/doc

p = {}

p.format = function(frame)
	local text = ""
	local spaceFlag = true
	mw.log(frame.args[1])
	for i in string.gmatch(frame.args[1], "[^.]") do
		if string.find(i, "%s") then
			spaceFlag = true
		else
			if spaceFlag == true then
				spaceFlag = false
				text = text .. string.upper(i)
			else
				text = text .. i
			end
		end
	end
	mw.log(text)
	return text
end

p.getTextColor = function(frame)
	local length = 0
	local colors = {}
	local pickedColor
	
	elemColor = require("Module:Element color")
	hex = require("Module:Hex")

	-- get all colors
	for i in string.gmatch(elemColor.getColor(frame), "[^,]+") do
		table.insert(colors, i)
		length = length + 1	
	end
	-- get middle color
	pickedColor = colors[math.ceil(length/2)]
	
	-- convert to RGB
	
	local r = hex.hexToDec(string.sub(pickedColor, 2, 3))
	local g = hex.hexToDec(string.sub(pickedColor, 4, 5))
	local b = hex.hexToDec(string.sub(pickedColor, 6, 7))

	-- determine color
	if (r+g+b)/3 > 150 then
		mw.log("black")
		return "color: black;"
	else
		mw.log("white")
		return "color: white;"
	end
end

return p