Module:HexShade

-- 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
	
	local max = math.max(r, g, b)
	local min = math.min(r, g, b)
	-- 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
	else
		if ((max + min) > 255) then
			S = (max - min) / (510 - max - min)
		else
			S = (max - min) / (max + min)
		end
	end
	-- luminosity
	L = (max + min) / (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, '^&#35;[%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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.