Module:LightDarkColor
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
![]() | This module depends on the following other modules: |
Description | Given a color given in light or dark mode, provide CSS that makes it dark mode compatible. |
---|---|
Author(s) | Awesome Aasim |
Code source | LightDarkColor |
Status | Alpha |
Dependencies |
Given a color given in light or dark mode, provide CSS that makes it dark mode compatible.
Documentation
Package items
lightdarkcolor.invertRGB(rgb)
(function)- Inverts RGB
- Parameter:
rgb
rgb table (table) - Returns: Inverted RGB table
lightdarkcolor._RGB(rgb)
(function)- Converts from (R, G, B) to (r, g, b, a) while preserving actual color in light mode
- Parameter:
rgb
rgb table (table) - Returns: Appropriate RGBA
lightdarkcolor.RGB(frame)
(function)- Entrypoint for RGB related functions
- Parameter:
frame
invocation frame (table) - Returns: Wikitext output
lightdarkcolor.Hex(frame)
(function)- Converts from #RRGGBB to #rrggbbaa while preserving actual color
- Parameter:
frame
invocation frame (table) - Returns: Wikitext output
lightdarkcolor.HSL(frame)
(function)- Converts from HSL to hsla while preserving actual color
- Parameter:
frame
invocation frame (table) - Returns: Wikitext output
- TODO: fill in function
lightdarkcolor.main(frame)
(function)- Entrypoint for module
- Parameter:
frame
invocation frame (table) - Returns: Wikitext output
--- 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._RGB
-- @param {table} rgb rgb table
-- @return Appropriate RGBA
function p._RGB(rgb)
-- get the largest value for RGB
local maxRGB = math.max(rgb[1], rgb[2], rgb[3])
local minRGB = math.min(rgb[1], rgb[2], rgb[3])
local rgbaFraction1 = 255 / maxRGB
local rgbaFraction2 = 255 / (255 - minRGB)
rgb[1] = rgb[1] * rgbaFraction1 / rgbaFraction2
rgb[2] = rgb[2] * rgbaFraction1 / rgbaFraction2
rgb[3] = rgb[3] * rgbaFraction1 / rgbaFraction2
rgb[4] = 1 / (rgbaFraction1 / rgbaFraction2)
return rgb
end
--- Entrypoint for RGB related functions
-- @function p.RGB
-- @param {table} frame invocation frame
-- @return Wikitext output
function p.RGB(frame)
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 = p._RGB(rgb)
return table.concat(rgba, ',')
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)
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 = p._RGB(rgb)
rH = dec2hex(rgb[1])
gH = dec2hex(rgb[2])
bH = dec2hex(rgb[3])
aH = dec2hex(rgb[4] * 255)
return rH .. gH .. bH .. aH
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