Jump to content

Module:HexShade: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
 
Fix
Line 11: Line 11:
s2 = mw.ustring.gsub(s2, '^([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')
s3 = mw.ustring.gsub(s3, '^([0-9A-Fa-f])$', '0%1')
return s1 .. s2 .. s3
return (s1 .. s2 .. s3):upper()
end
end
return d
return d
Line 93: Line 93:
end
end


local function hsl2rgbdec(H, S, L)
local function hsl2rgbdec(h, s, l)
-- This function came from [[Module:Color contrast]]
local R, G, B
if ( 0 <= H and H < 360 and 0 <= S and S <= 1 and 0 <= L and L <= 1 ) then
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
-- R
if (math.fmod(H + 120, 360) > 180) then
local x = c*(1 - math.abs( math.fmod(h/60, 2) - 1) )
local m = l - c/2
if (math.fmod(H + 120, 360) > 240) then

if (L > 0.5) then
R = L - S * (1 - L)
local r, g, b = m, m, m
if( 0 <= h and h < 60 ) then
else
R = (1 - S) * L
r = r + c
end
g = g + x
elseif( 60 <= h and h < 120 ) then
else
if (L > 0.5) then
r = r + x
g = g + c
R = L - (1 - (240 - math.fmod(H + 120, 360) / 30) * S * (1 - L))
elseif( 120 <= h and h < 180 ) then
else
g = g + c
R = (1 - (1 - (240 - math.fmod(H + 120, 360) / 30) * S) * L)
end
b = b + x
elseif( 180 <= h and h < 240 ) then
end
g = g + x
else
b = b + c
if (math.fmod(H + 120, 360) > 60) then
if (L > 0.5) then
elseif( 240 <= h and h < 300 ) then
R = L + S * (1 - L)
r = r + x
else
b = b + c
R = (1 + S) * L
elseif( 300 <= h and h < 360 ) then
end
r = r + c
else
b = b + x
if (L > 0.5) then
R = L - ((1 - math.fmod(H + 120, 360) / 30) * S * (1 - L))
else
R = (1 - ((1 - math.fmod(H + 120, 360) / 30) * S) * L)
end
end
end
-- G
if (math.fmod(H,360) > 180) then
if (math.fmod(H,360) > 240) then
if (L > 0.5) then
G = L - S * (1 - L)
else
G = (1 - S) * L
end
else
if (L > 0.5) then
G = L - (1 - (240 - math.fmod(H,360)) / 30) * S * (1 - L)
else
G = (1 - (1 - (240 - math.fmod(H,360)) / 30) * S) * L
end
end
else
if (math.fmod(H,360) > 60) then
if (L > 0.5) then
G = L + S * (1 - L)
else
G = (1 + S) * L
end
else
if (L > 0.5) then
G = L - ((1 - math.fmod(H,360)) / 30) * S * (1 - L)
else
G = (1 - ((1 - math.fmod(H,360)) / 30) * S) * L
end
end
end
-- B
if (math.fmod(H - 120,360) > 180) then
if (math.fmod(H - 120,360) > 240) then
if (L > 0.5) then
B = L - S * (1 - L)
else
B = (1 - S) * L
end
else
if (L > 0.5) then
B = L - (1 - (240 - math.fmod(H - 120,360)) / 30) * S * (1 - L)
else
B = (1 - (1 - (240 - math.fmod(H - 120,360)) / 30) * S) * L
end
end
else
if (math.fmod(H - 120,360) > 60) then
if (L > 0.5) then
B = L + S * (1 - L)
else
B = (1 + S) * L
end
else
if (L > 0.5) then
B = L - (1 - math.fmod(H - 120,360) / 30) * S * (1 - L)
else
B = (1 - (1 - math.fmod(H - 120,360) / 30) * S) * L
end
end
end
end
return 255*R, 255*G, 255*B
return 255*r, 255*g, 255*b
end
end
return -1, -1, -1
return -1, -1, -1
Line 199: Line 133:
c = mw.ustring.match(c, '^[%s]*(.-)[%s]*$')
c = mw.ustring.match(c, '^[%s]*(.-)[%s]*$')
c = mw.ustring.gsub(c, '^[%s#]*', '')
c = mw.ustring.gsub(c, '^[%s#]*', '')
c = mw.ustring.gsub(c, '^&#32;[%s]*', '')
c = mw.ustring.gsub(c, '^&#35;[%s]*', '')
-- Lowercase
-- Lowercase
c = c:lower()
c = c:lower()
-- Expand short 3 hex for to 6 hex form
-- Expand short 3 hex for to 6 hex form
c = mw.ustring.gsub(c, '^([0-9a-f])([0-9a-f])([0-9a-f])$', '%1%1%2%2%3%3')
c = mw.ustring.gsub(c, '^([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$', '%1%1%2%2%3%3')
-- Get new value for luminosity
-- Get new value for luminosity
Line 229: Line 163:
end
end
res = res:upper()
if args[3] == '#' and mw.ustring.match('^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]') then
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
return '#' .. res
end
end

Revision as of 22:35, 12 December 2021

-- 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 (r > 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 '')
	c = mw.ustring.match(c, '^[%s]*(.-)[%s]*$')
	c = mw.ustring.gsub(c, '^[%s#]*', '')
	c = mw.ustring.gsub(c, '^&#35;[%s]*', '')
	-- Lowercase
	c = c:lower()
	-- 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')
	
	-- Get new value for luminosity
	local Lnew = tonumber(args[2] or '-1') or -1
	
	-- 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])

		-- Convert RGBdec to HSL
		local H, S, L = rgbdec2hsl(R, G, B)
		
		-- 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