Module:Utilities: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m get_slot_color colors can now have prefix/postfix removed, allowing for dynamically editing the color
Sur (talk | contribs)
m introduced a bug; temp reverting. Undo revision 13806 by Sur (talk)
Tag: Undo
Line 102: Line 102:
end
end


-- Hash for color values
local slots_data = {
local slot_colors = {
["Weapon"] = {
["Weapon"] = {
hex = "c97a03",
hex = "#c97a03",
rgb = "201,122,3",
rgb = "rgb(201, 122, 3)",
hsl = "35,97%,40%",
hsl = "hsl(35, 97%, 40%)",
cmyk = "0%,39%,98%,21%"
cmyk = "cmyk(0%, 39%, 98%, 21%)"
},
},
["Armor"] = {
["Armor"] = {
hex = "659818",
hex = "#659818",
rgb = "101,152,24",
rgb = "rgb(101, 152, 24)",
hsl = "82,73%,34%",
hsl = "hsl(82, 73%, 34%)",
cmyk = "34%,0%,84%,40%"
cmyk = "cmyk(34%, 0%, 84%, 40%)"
},
},
["Tech"] = {
["Tech"] = {
hex = "8b56b4",
hex = "#8b56b4",
rgb = "139,86,180",
rgb = "rgb(139, 86, 180)",
hsl = "274,36%,52%",
hsl = "hsl(274, 36%, 52%)",
cmyk = "23%,52%,0%,29%"
cmyk = "cmyk(23%, 52%, 0%, 29%)"
}
}
}
}
 
function p.get_slot_color(slot, color_format, debug_mode)
-- 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_slot_color(slot, color_format, no_wrap, debug_mode)
if type(slot) == 'table' and slot.args then
if type(slot) == 'table' and slot.args then
frame = slot
frame = slot
slot = frame.args[1]
slot = frame.args[1]
color_format = frame.args[2]
color_format = frame.args[2]
no_wrap = frame.args[3]
debug_mode = frame.args["debug_mode"]
debug_mode = frame.args["debug_mode"]
end
end
-- Validate arguments
-- Validate arguments
if slot == nil then return "'slot' parameter must be provided" end
if slot == nil then  
if color_format == nil then color_format = "hex" end
return "'slot' parameter must be provided"
if no_wrap == nil then no_wrap = false end
end
if debug_mode == nil then debug_mode = false end
if color_format == nil then  
color_format = "hex"
end
local slot_data = slot_colors[slot]
local slot_data = slots_data[slot]
if slot_data == nil then return "slot '" .. slot .. "' was not in slots_data map" end
if slot_data == nil then
return "slot '" .. slot .. "' was not in slots_data map"
end
--Retrieve the color
--Retrieve the color
Line 167: Line 149:
end
end
-- Add prefix and postfix wrapping
-- Return result
if no_wrap then
if debug_mode == nil then
-- Retrieve prefix and postfix
debug_mode = false
prefix = color_formats[color_format]['prefix']
postfix = color_formats[color_format]['postfix']
-- Add to color
color = prefix .. color .. postfix
end
end
-- Return result
if debug_mode then
if debug_mode then
return " " .. color
return " " .. color

Revision as of 22:07, 1 November 2024

Overview

Utility functions that serve any miscellaneous purpose

Functions

get_slot_color

Retrieve's the color of a certain slot/category in any color format (default hex), formerly known in english as Weapon, Vitality, and Spirit, though their unlocalized names are Weapon, Armor, and Tech. Utilized by Template:SlotColor

Parameters

  • slot - slot key, options are Weapon, Armor, and Tech
  • 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:SlotColor


local p = {};
	
	-- "Default %hero_name% Build" --> "Default  Build", or "Default Build" with -1 or +1 for spaces
	function p.remove_var(input, spaces)
		-- spaces = 
		-- -1: also remove prefixed character
		-- 0 or nil: no character
		-- 1: postfixed character
		-- Remove text between % and % including the %
	    local result = input:gsub("%%.-%%", "")
	    
	    -- If spaces == -1, remove the character before the removed section
	    if spaces ~= nil and spaces == -1 then
	        result = result:gsub("%s+%s*", "", 1) -- Removes the preceding space or character
	    -- If spaces == 1, remove the character after the removed section
	    elseif spaces ~= nil and spaces == 1 then
	        result = result:gsub("%s+", "", 1) -- Removes the postfixed space
	    end
	    
	    -- Trim any extra whitespace
	    result = result:gsub("^%s*(.-)%s*$", "%1")
	    
	    return result
	end

	--round_to_significant_figures(12345.6789, 3)  -- Output: 12300
	--round_to_significant_figures(0.0012345, 2)   -- Output: 0.0012
	--round_to_significant_figures(-98765, 4)      -- Output: -98760
	function p.round_to_sig_fig(num, n)
	    if num == 0 then
	        return 0
	    end
	
	    -- Calculate the order of magnitude (log10 returns the logarithm base 10)
	    local order_of_magnitude = math.floor(math.log10(math.abs(num)))
	    
	    -- Scale the number to move the significant digits into the integer part
	    local scale = math.pow(10, n - 1 - order_of_magnitude)
	    
	    -- Round the scaled number and then scale it back
	    local rounded_num = math.floor(num * scale + 0.5) / scale
	    
	    return rounded_num
	end
	
	-- If the image exists exists, return it back enclosed in brackets, else return a blank string
	function p.get_image_file(image_file_name, px, link)
		if (px == nil) then px = 15 end --default
		if (link == nil) then link = "" end --default
		
		image_file = mw.title.new(image_file_name)
		if image_file and image_file.exists then
		    image_file_name = "[[" .. image_file_name .. "|" .. px .. "px|link=" .. link .. "]]"
		else
		    image_file_name = ''
		end
		
		return image_file_name
	end
	
	function p.string_endswith(str, ending)
	    return ending == "" or str:sub(-#ending) == ending
	end
	
	-- Add a space before each capital letter that is not the first character
	-- i.e. BulletVelocity > Bullet Velocity
	-- when a string doesn't have localization, it can outputted as add_space_before_cap(unlocalized_key)
	function p.add_space_before_cap(str)
	    local result = str:gsub("(%l)(%u)", "%1 %2")
	    return result:gsub("(%u)(%u%l)", " %1%2")
	end
	
	--Much room for expansion here, currently just replaces spaces with underscores essentially
	--so that it can be used in a url directly
	p.url_encode = function(str)
		if type(str) == 'table' and str.args then
			frame = str
			str = frame.args[1]
		end
		
		if (str == nil) then return "First parameter must be a string" end
		if (str == '') then return '' end
		-- Just replaces spaces with %20
		local result = string.gsub(str, " ", "%%20")
		return result --must assign as local result first to grab just first returned result
	end
	
	--Creates a deepCopy of a table
	function p.deep_copy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == "table" then
        copy = {}
        for key, value in pairs(orig) do
            copy[p.deep_copy(key)] = p.deep_copy(value)
        end
        setmetatable(copy, p.deep_copy(getmetatable(orig)))
    else  -- For non-table types, simply return the original value
        copy = orig
    end
    return copy
end

local slots_data = {
	["Weapon"] = {
		hex = "#c97a03",
		rgb = "rgb(201, 122, 3)",
		hsl = "hsl(35, 97%, 40%)",
		cmyk = "cmyk(0%, 39%, 98%, 21%)"
	},
	["Armor"] = {
		hex = "#659818",
		rgb = "rgb(101, 152, 24)",
		hsl = "hsl(82, 73%, 34%)",
		cmyk = "cmyk(34%, 0%, 84%, 40%)"
	},
	["Tech"] = {
		hex = "#8b56b4",
		rgb = "rgb(139, 86, 180)",
		hsl = "hsl(274, 36%, 52%)",
		cmyk = "cmyk(23%, 52%, 0%, 29%)"
	}
}
function p.get_slot_color(slot, color_format, debug_mode)
	if type(slot) == 'table' and slot.args then
		frame = slot
		slot = frame.args[1]
		color_format = frame.args[2]
		debug_mode = frame.args["debug_mode"]
	end
	
	-- Validate arguments
	if slot == nil then 
		return "'slot' parameter must be provided"
	end
	if color_format == nil then 
		color_format = "hex"
	end
	
	local slot_data = slots_data[slot]
	if slot_data == nil then
		return "slot '" .. slot .. "' was not in slots_data map"
	end
	
	--Retrieve the color
	local color = slot_data[color_format]
	if color == nil then
		return "color_format '" .. color_format .. "' is not in slots_data map"
	end
	
	-- Return result
	if debug_mode == nil then
		debug_mode = false
	end
	if debug_mode then
		return " " .. color
	end
	return color
end

return p