Module:HexShade
Documentation for this module may be created at Module:HexShade/doc
-- This module implements {{HexShade}}
local p = {}
local function rgbdec2hex(r, g, b, d)
if (r >= 0 and g >= 0 and b >= 0 ) then
local baseconvert = require('Module:BaseConvert')
local s1 = baseconvert.convert({n = math.floor(r+0.5), base = 16})
local s2 = baseconvert.convert({n = math.floor(g+0.5), base = 16})
local s3 = baseconvert.convert({n = math.floor(b+0.5), base = 16})
s1 = mw.ustring.gsub(s1, '^([0-9A-Fa-f])$', '0%1')
s2 = mw.ustring.gsub(s2, '^([0-9A-Fa-f])$', '0%1')
s3 = mw.ustring.gsub(s3, '^([0-9A-Fa-f])$', '0%1')
return (s1 .. s2 .. s3):upper()
end
return d
end
local function rgbdec2hsl(r, g, b)
local H, S, L
-- hue
if ((r == g) and (g == b)) then
H = 0
elseif ((g >= r) and (g >= b)) then
if (r > b) then
H = 120 - 60 * (r - b) / (g - b)
else
H = 120 + 60 * (b - r) / (g - r)
end
elseif ((b >= r) and (b >= g)) then
if (g > r) then
H = 240 - 60 * (g - r) / (b - r)
else
H = 240 + 60 * (r - g) / (b - g)
end
else
if (b > g) then
H = 360 - 60 * (b - g) / (r - g)
else
H = 60 * (g - b) / (r - b)
end
end
-- saturation
if ((r == g) and (g == b)) then
S = 0
elseif ((g >= r) and (g >= b)) then
if (r > b) then
if ((g + b) > 255) then
S = (g - b) / (510 - g - b)
else
S = (g - b) / (g + b)
end
else
if ((g + r) > 255) then
S = (g - r) / (510 - g - r)
else
S = (g - r) / (g + r)
end
end
elseif ((r >= b) and (r >= g)) then
if (b > g) then
if ((r + g) > 255) then
S = (r - g) / (510 - r - g)
else
S = (r - g) / (r + g)
end
else
if ((r + b) > 255) then
S = (r - b) / (510 - r - b)
else
S = (r - b) / (r + b)
end
end
else
if (g > r) then
if ((b + r) > 255) then
S = (b - r) / (510 - b - r)
else
S = (b - r) / (b + r)
end
else
if ((b + g) > 255) then
S = (b - g) / (510 - b - g)
else
S = (b - g) / (b + g)
end
end
end
-- luminosity
L = (math.max(r, math.max(g, b)) + math.min(r, math.min(g, b))) / (255 * 2)
return H, S, L
end
local function hsl2rgbdec(h, s, l)
-- This function came from [[Module:Color contrast]]
if ( 0 <= h and h < 360 and 0 <= s and s <= 1 and 0 <= l and l <= 1 ) then
local c = (1 - math.abs(2*l - 1))*s
local x = c*(1 - math.abs( math.fmod(h/60, 2) - 1) )
local m = l - c/2
local r, g, b = m, m, m
if( 0 <= h and h < 60 ) then
r = r + c
g = g + x
elseif( 60 <= h and h < 120 ) then
r = r + x
g = g + c
elseif( 120 <= h and h < 180 ) then
g = g + c
b = b + x
elseif( 180 <= h and h < 240 ) then
g = g + x
b = b + c
elseif( 240 <= h and h < 300 ) then
r = r + x
b = b + c
elseif( 300 <= h and h < 360 ) then
r = r + c
b = b + x
end
return 255*r, 255*g, 255*b
end
return -1, -1, -1
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
-- Remove nowiki and leading hash signs
local c = mw.text.unstrip(args[1] or '#f0e68c')
c = mw.ustring.match(c, '^[%s]*(.-)[%s]*$')
c = mw.ustring.gsub(c, '^[%s#]*', '')
c = mw.ustring.gsub(c, '^#[%s]*', '')
-- Lowercase
c = c:lower()
mw.log(c)
-- Expand short 3 hex for to 6 hex form
c = mw.ustring.gsub(c, '^([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$', '%1%1%2%2%3%3')
mw.log(c)
-- Get new value for luminosity
local Lnew = tonumber(args[2] or '-1') or 0.6
-- Default return
local res = c
-- Adjust shading
local cs = mw.text.split(c or '', '')
if( #cs == 6 ) then
-- Convert to RGBdec
local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2])
local G = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4])
local B = 16*tonumber('0x' .. cs[5]) + tonumber('0x' .. cs[6])
mw.log(R, G, B)
-- Convert RGBdec to HSL
local H, S, L = rgbdec2hsl(R, G, B)
mw.log(H, S, L)
-- Convert back to RGBdec
local R, G, B = hsl2rgbdec(H, S, Lnew)
-- Convert back to RGBhex
res = rgbdec2hex(R, G, B, c)
end
res = res:upper()
if args[3] == '#' and mw.ustring.match(res, '^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$') then
return '#' .. res
end
return res
end
return p