Module:Color

From Deadlock Wiki
Revision as of 03:19, 2 November 2024 by Sur (talk | contribs) (ported get_slot_color from Utilities, now more generic and supports other key types)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

Color related functions for retrieving wiki-standardized colors

Functions

get_color

Retrieve's the color of a given key in any color format (default hex).

Parameters

  • key - key, options are Script error: The function "list_keys" does not exist.
  • color_format - color format - Defaults to hex. Valid options are hex, rgb, hsl, cmyk
  • no_wrap - (OPTIONAL) - Defaults to false. Set to true to remove prefixes and postfixes of '#' from hex, or 'rgb()', 'hsl()', and 'cmyk()' from the other formats. May be useful for altering the values dynamically
  • debug_mode - (OPTIONAL) - Defaults to false. Used mostly for documentation purposes

Examples

See examples at Template:Color

Previews

Script error: The function "write_previews" does not exist.


local p = {}

-- Hash for color values
local colors_data = {
	["Weapon"] = {
		hex = "c97a03",
		rgb = "201,122,3",
		hsl = "35,97%,40%",
		cmyk = "0%,39%,98%,21%"
	},
	["Armor"] = {
		hex = "659818",
		rgb = "101,152,24",
		hsl = "82,73%,34%",
		cmyk = "34%,0%,84%,40%"
	},
	["Tech"] = {
		hex = "c288f0", -- purple
		rgb = "194,136,240",
		hsl = "276,79%,74%",
		cmyk = "19%,43%,0%,6%"
	},
	["Souls"] = {
        hex = "98ffde",
        rgb = "152,255,222",
        hsl = "162,100%,80%",
        cmyk = "40%,0%,13%,0%"
    },
    ["Healing"] = {
        hex = "00FF9A",
        rgb = "0,255,154",
        hsl = "150,100%,50%",
        cmyk = "100%,0%,40%,0%"
    },
    ["Abrams"] = {
        hex = "2193AE",
        rgb = "33,147,174",
        hsl = "193,68%,41%",
        cmyk = "81%,15%,0%,32%"
    },
    ["Wraith"] = {
        hex = "954E7A",
        rgb = "149,78,122",
        hsl = "320,31%,44%",
        cmyk = "0%,48%,18%,42%"
    },
    ["Viscous"] = {
        hex = "8AC365",
        rgb = "138,195,101",
        hsl = "98,47%,58%",
        cmyk = "29%,0%,48%,24%"
    },
    ["Grey Talon"] = {
        hex = "61B083",
        rgb = "97,176,131",
        hsl = "145,32%,54%",
        cmyk = "45%,0%,26%,31%"
    },
    ["Vindicta"] = {
        hex = "87A8B8",
        rgb = "135,168,184",
        hsl = "202,28%,63%",
        cmyk = "27%,9%,0%,28%"
    },
    ["Infernus"] = {
        hex = "FF472E",
        rgb = "253,71,46",
        hsl = "7,98%,59%",
        cmyk = "0%,72%,82%,1%"
    },
    ["McGinnis"] = {
        hex = "466A9C",
        rgb = "70,106,156",
        hsl = "215,38%,44%",
        cmyk = "55%,32%,0%,39%"
    },
    ["Haze"] = {
        hex = "AE6231",
        rgb = "174,98,49",
        hsl = "25,57%,44%",
        cmyk = "0%,44%,72%,32%"
    },
    ["Lady Geist"] = {
        hex = "088A5C",
        rgb = "8,138,92",
        hsl = "162,88%,29%",
        cmyk = "94%,0%,33%,46%"
    },
    ["Mirage"] = {
        hex = "87683C",
        rgb = "135,104,60",
        hsl = "35,38%,38%",
        cmyk = "0%,23%,56%,47%"
    },
    ["Lash"] = {
        hex = "444C57",
        rgb = "68,76,87",
        hsl = "215,12%,30%",
        cmyk = "22%,13%,0%,66%"
    },
    ["Seven"] = {
        hex = "EC963F",
        rgb = "236,150,63",
        hsl = "30,83%,59%",
        cmyk = "0%,36%,73%,8%"
    },
    ["Yamato"] = {
        hex = "6F9181",
        rgb = "111,145,129",
        hsl = "150,13%,50%",
        cmyk = "23%,0%,11%,43%"
    },
    ["Warden"] = {
        hex = "566279",
        rgb = "86,98,121",
        hsl = "220,17%,41%",
        cmyk = "29%,19%,0%,53%"
    },
    ["Bebop"] = {
        hex = "A04835",
        rgb = "160,72,53",
        hsl = "11,50%,42%",
        cmyk = "0%,55%,67%,37%"
    },
    ["Paradox"] = {
        hex = "993A53",
        rgb = "153,58,83",
        hsl = "341,45%,41%",
        cmyk = "0%,62%,46%,40%"
    },
    ["Dynamo"] = {
        hex = "D0BA46",
        rgb = "208,186,70",
        hsl = "49,62%,54%",
        cmyk = "0%,11%,66%,18%"
    },
    ["Shiv"] = {
        hex = "A53D88",
        rgb = "165,61,136",
        hsl = "317,46%,44%",
        cmyk = "0%,63%,18%,35%"
    }
}

-- Hash for format configuration
local color_formats = {
	hex = {
		prefix = "#",
		postfix = ""
	},
	rgb = {
		prefix = "rgb(",
		postfix = ")"
	},
	hsl = {
		prefix = "hsl(",
		postfix = ")"
	},
	cmyk = {
		prefix = "cmyk(",
		postfix = ")"
	}
}

function p.get_color(key, color_format, no_wrap, debug_mode)
	if type(key) == 'table' and key.args then
		frame = key
		key = frame.args[1]
		color_format = frame.args[2]
		no_wrap = frame.args["no_wrap"]
		debug_mode = frame.args["debug_mode"]
	end
	
	-- Validate arguments
	if key == nil or key == "" then return "'key' parameter must be provided" end
	if color_format == nil or color_format == "" then color_format = "hex" end
	if no_wrap == nil or no_wrap == 'false' or no_wrap == "" then no_wrap = false else no_wrap = true end
	if debug_mode == nil or debug_mode == 'false' or debug_mode == "" then debug_mode = false end
	
	local color_data = colors_data[slot]
	if color_data == nil then return "slot '" .. key .. "' was not in slots_data map" end
	
	--Retrieve the color
	local color = color_data[color_format]
	if color == nil then
		return "color_format '" .. color_format .. "' is not in slots_data map"
	end
	
	-- Add prefix and postfix wrapping
	if not no_wrap then
		-- Retrieve prefix and postfix
		prefix = color_formats[color_format]['prefix']
		postfix = color_formats[color_format]['postfix']
		-- Add to color
		color = prefix .. color .. postfix
	end
	
	-- Return result
	if debug_mode then
		return " " .. color
	end
	return color
end

return p