Jump to content

Module:LightDarkColor

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Awesome Aasim (talk | contribs) at 05:15, 19 June 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--- Given a color given in light or dark mode, provide CSS that makes it dark
--  mode compatible.
--
-- @module		lightdarkcolor
-- @alias		p
-- @release		alpha
-- @author		Awesome Aasim
-- @require		Module:Arguments
-- @require		Module:Entrypoint

local p = {}
local getArgs = require("Module:Arguments").getArgs

--- Converts hex to dec
-- @function		hex2dec
-- @param	{string}	hex		hex to convert
-- @return						Decimal value
local function hex2dec(hex)
	return tonumber('0x' .. hex)
end

--- Converts dec to hex
-- @function		dec2hex
-- @param	{string}	dec		dec to convert
-- @return						Hexadecimal value
local function dec2hex(dec)
	return mw.ustring.format("%x", dec)
end

--- Inverts RGB
-- @function		p.invertRGB
-- @param	{table}		rgb		rgb table
-- @return						Inverted RGB table
function p._invertRGB(rgb)
	rgb[1] = 255 - rgb[1]
	rgb[2] = 255 - rgb[2]
	rgb[3] = 255 - rgb[3]
	return rgb
end

--- Converts from (R, G, B) to (r, g, b, a) while preserving actual color in light mode
-- @function			p._lightRGB
-- @param	{table}		rgb		rgb table
-- @return						Appropriate RGBA
function p._lightRGB(rgb)
	-- get the largest value for RGB
	local maxRGB = math.max(rgb[1], rgb[2], rgb[3])
	local rgbaFraction = 255 / maxRGB
	rgb[1] = rgb[1] * rgbaFraction
	rgb[2] = rgb[2] * rgbaFraction
	rgb[3] = rgb[3] * rgbaFraction
	rgb[4] = 1 / rgbaFraction
	return rgb
end

--- Converts from (R, G, B) to (r, g, b, a) while preserving actual color in dark mode
-- @function			p._lightRGB
-- @param	{table}		rgb			rgb table
-- @return							Appropriate RGBA
function p._darkRGB(rgb)
	rgb = p._invertRGB(rgb)
	rgba = p._lightRGB(rgb)
	rgba = p._invertRGB(rgba)
	return rgba
end

--- Entrypoint for RGB related functions
-- @function			p.RGB
-- @param	{table} 	frame		invocation frame
-- @return							Wikitext output
function p.RGB(frame, dark)
	local args = getArgs(frame)
	local rgbStr = args[1] or error("RGB not specified", 1)
	rgbStr = mw.text.trim(rgbStr)
	local rgb = mw.text.split(rgbStr, ",")
	rgb[1] = tonumber(rgb[1])
	if not rgb[2] and not rgb[3] then
		rgb[2] = rgb[1]
		rgb[3] = rgb[1]
	end
	rgb[2] = tonumber(rgb[2])
	rgb[3] = tonumber(rgb[3])
	local rgba = dark and p._darkRGB(rgb) or p._lightRGB(rgb)
	return table.concat(rgba, ',')
end

function p.lightRGB(frame)
	return p.RGB(frame, false)
end

function p.darkRGB(frame)
	return p.RGB(frame, true)
end


--- Converts from #RRGGBB to #rrggbbaa while preserving actual color
-- @function			p.Hex
-- @param	{table}		frame	invocation frame
-- @return						Wikitext output
function p.Hex(frame, dark)
	local args = getArgs(frame)
	local hexStr = args[1] or error("Hex not specified", 1)
	hexStr = mw.text.trim(hexStr)
	local hexChars = mw.text.split(hexStr, '')
	local rgb = {}
	local rH = hexChars[1] .. hexChars[2]
	local gH = (hexChars[3] or '') .. (hexChars[4] or '')
	local bH = (hexChars[5] or '') .. (hexChars[6] or '')
	local aH = ''
	rgb[1] = hex2dec(rH)
	if gH == '' and bH == '' then
		rgb[2] = rgb[1]
		rgb[3] = rgb[1]
	end
	rgb[2] = hex2dec(gH)
	rgb[3] = hex2dec(bH)
	local rgba = dark and p._darkRGB(rgb) or p._lightRGB(rgb)
	rgb[4] = math.floor(rgb[4] * 255)
	rH = dec2hex(rgb[1])
	gH = dec2hex(rgb[2])
	bH = dec2hex(rgb[3])
	aH = dec2hex(rgb[4])
	return 
end

function p.lightHex(frame)
	return p.Hex(frame, false)
end

function p.darkHex(frame)
	return p.Hex(frame, true)
end

--- Converts from HSL to hsla while preserving actual color
-- @function			p.HSL
-- @param	{table}		frame	invocation frame
-- @return						Wikitext output
-- @todo fill in function
function p.HSL(frame, dark)
	
end

--- Entrypoint for module
-- @function			p.main
-- @param	{table}		frame	invocation frame
-- @return						Wikitext output
p.main = require("Module:Entrypoint")(p)

return p