Module:Color

From Wiki of ZZT
Revision as of 05:53, 11 January 2021 by Quantum (talk | contribs) (WIP: Writing a module for generating color swatches)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}

local ega_hex_codes = {
	"#000",
	"#00a",
	"#0a0",
	"#0aa",
	"#a00",
	"#a0a",
	"#a50",
	"#aaa",
	"#555",
	"#55f",
	"#5f5",
	"#5ff",
	"#f55",
	"#f5f",
	"#ff5",
	"#fff"
}

local ega_color_names = {
    "black",
    "dark blue",
    "dark green",
    "dark cyan",
    "dark red",
    "dark purple",
    "dark brown",
    "light gray",
    "dark gray",
    "blue",
    "green",
    "cyan",
    "red",
    "purple",
    "yellow",
    "white"
}

function index_to_name(i)
    if i ~= math.floor(i) or i < 0 or i > 15 then
        return "???"
    end
    return ega_color_names[i + 1]
end

function combo_name(bg_index, fg_index)
    blinking = false
    if bg_index >= 8 then
        blinking = true
        bg_index = bg_index - 8
    end
    result = index_to_name(fg_index) .. " on " .. index_to_name(bg_index)
    if blinking then
        result = "blinking " .. result
    end
    return result
end

function p.swatch(frame)
    bg = frame.args.bg
    fg = frame.args.fg
	return combo_name(bg, fg)
end

return p