Module:ColorSwatch: Difference between revisions

From Wiki of ZZT
Jump to navigation Jump to search
(Remove bg check for blinking because it was interfering with creating blinking colors)
(Show correct hex representation when blinking = true)
Line 16: Line 16:
local name = get_name(bg, fg, blinking)
local name = get_name(bg, fg, blinking)
local swatch = get_swatch(bg, fg, blinking)
local swatch = get_swatch(bg, fg, blinking)
local hex = get_hex(bg, fg)
local hex = get_hex(bg, fg, blinking)
return string.format("%s %s (%s)", name, tostring(swatch), hex)
return string.format("%s %s (%s)", name, tostring(swatch), hex)
end
end
Line 61: Line 61:
end
end


function get_hex(bg, fg)
function get_hex(bg, fg, blinking)
if bg < 8 and blinking then
bg = bg + 8
end
return string.format("0x%02X", bg*16 + fg)
return string.format("0x%02X", bg*16 + fg)
end
end


return p
return p

Revision as of 09:38, 14 January 2021

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

local p = {}

local color = require("Module:EGAColor")

function p.swatch(frame)
	local bg = color.to_index(frame.args.bg)
	local fg = color.to_index(frame.args.fg)
	local blinking = false
	for _, arg in ipairs(frame.args) do
		if string.lower(arg) == "blinking" then
			blinking = true
			break
		end
	end

	local name = get_name(bg, fg, blinking)
	local swatch = get_swatch(bg, fg, blinking)
	local hex = get_hex(bg, fg, blinking)
	return string.format("%s %s (%s)", name, tostring(swatch), hex)
end

function get_name(bg, fg, blinking)
	local result = color.to_name(fg) .. " on " .. color.to_name(bg)
	if blinking then
		result = "blinking " .. result
	end
	return result
end

function get_swatch(bg, fg, blinking)
	local bg = color.to_hex_color(bg)
	local fg = color.to_hex_color(fg)

	local outer = mw.html.create("span")
	outer:css({
		width = "1.25em",
		height = "1em",
		["background-color"] = bg,
		["box-shadow"] = "0 0 1px #808080",
		display = "inline-flex",
		["align-items"] = "center",
		["justify-content"] = "center"
	})
	local inner = outer:tag("span")
	inner:css({
		display = "inline-block",
		width = "0.5em",
		height = "0.5em"
	})
	if blinking then
		local gradient = "linear-gradient(135deg, {h} 0% 50%, {h}0 50% 100%)" 
		gradient = string.gsub(gradient, "{h}", fg)
		inner:css({
			background = gradient,
			["box-shadow"] = "inset 0 0 0 1px " .. fg
		})
	else
		inner:css("background-color", fg)
	end
	return outer
end

function get_hex(bg, fg, blinking)
	if bg < 8 and blinking then
		bg = bg + 8
	end
	return string.format("0x%02X", bg*16 + fg)
end

return p