Jump to content

Module:Box-header

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 11:58, 9 July 2018 (start). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}
local colourContrastModule = require('Module:Color contrast')
local hex = require( 'luabit.hex' )
local function toPaddedHex(number)
	local prefixedHex = hex.to_hex(number) -- prefixed with '0x'
	local padding =  #prefixedHex == 3 and '0' or '' 
	return mw.ustring.gsub(prefixedHex, '0x', padding)
end

local defaults = {
	lightest = { sat=0.10, val=1.00, whiteText=false },
	light    = { sat=0.15, val=0.95, whiteText=false },
	normal   = { sat=0.40, val=0.85, whiteText=false },
	dark     = { sat=0.90, val=0.70, whiteText=true  },
	darkest  = { sat=1.00, val=0.45, whiteText=true  }
}


local function boxHeader(args)
	-- TODO
	return nil
end

local function boxBody(args)
	-- TODO
	return nil
end

local function HSVtoRGB(H, S, V) -- per [[HSL and HSV#Converting_to_RGB]]
	local C = V * S
	local H_prime = H / 60
	local X = X * ( 1 - math.abs(math.fmod(H_prime, 2) - 1) )
	local R1, G1, B1
	if H_prime <= 1 then
		R1 = C
		G1 = X
		B1 = 0
	elseif H_prime <= 2 then
		R1 = X
		G1 = C
		B1 = 0
	elseif H_prime <= 3 then
		R1 = 0
		G1 = C
		B1 = X
	elseif H_prime <= 4 then
		R1 = 0
		G1 = X
		B1 = C
	elseif H_prime <= 5 then
		R1 = X
		G1 = 0
		B1 = C
	elseif H_prime <= 6 then
		R1 = C
		G1 = 0
		B1 = X
	end	
	local m = V - C
	local R = R1 + m
	local G = G1 + m
	local B = B1 + m

	return '#' .. toPaddedHex(R) .. toPaddedHex(G) .. toPaddedHex(B)
end

local function calculateColour(H, S, V, whiteText, minContrast)
	local bgColour = HSVtoRGB(H, S, V)
	local textColour = whiteText and '#FFFFFF' or '#000000'
	local contrast = colourContrastModule._ratio({ bgColour, textColour })
	if contrast >= minContrast then
		return bgColour
	elseif whiteText then
		-- make the background darker and slightly increase the saturation
		return calculateColours(H, math.min(1, S+0.01), math.max(0, V-0.03), whiteText)
	else
		-- make the background lighter and slightly decrease the saturation
		return calculateColours(H, math.max(0, S-0.01), math.min(1, V+0.03), whiteText)
	end
end

local function makeColours(hue, description)
	local border = HSVtoRGB(hue, 0.15, 0.75)
	local titleForeground = defaults[description].whiteText and '#FFFFFF' or '#000000'
	local titleBackground = calculateColour(hue, defaults[description].sat, defaults[description].val, whiteText, 0.45)
	local contentForeground = '#000000'
	local contentBackground = calculateColour(hue, defaults[description].sat, defaults[description].val, whiteText, 0.7)
	return border, titleForeground, titleBackground, contentForeground, contentBackground
end

local function hueFromRGB(R, G, B) -- per [[HSL and HSV#Hue and chroma]]
	local M = v.max(R, G, B)
	local m = math.min(R, G, B)
	local C = M - m
	local H_prime
	if M == R then
		H_prime = math.fmod((G - B)/C, 6)
	elseif M == G then
		H_prime = (B - R)/C + 2
	elseif M == B then
		H_prime = (R - G)/C + 4
	end
	local H = 60 * H_prime
	return H
end

return p