<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3AColor</id>
	<title>Module:Color - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3AColor"/>
	<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:Color&amp;action=history"/>
	<updated>2026-04-05T01:50:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://the-democratika.com/wiki/index.php?title=Module:Color&amp;diff=6308&amp;oldid=prev</id>
		<title>&gt;Hike395: protect against junk value</title>
		<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:Color&amp;diff=6308&amp;oldid=prev"/>
		<updated>2024-09-23T02:01:10Z</updated>

		<summary type="html">&lt;p&gt;protect against junk value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- Introduction: https://colorspace.r-forge.r-project.org/articles/color_spaces.html&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
local function isempty(v)&lt;br /&gt;
	return v == nil or v == &amp;#039;&amp;#039;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function hexToRgb(color)&lt;br /&gt;
	local cleanColor = color:gsub(&amp;quot;&amp;amp;#35;&amp;quot;, &amp;quot;#&amp;quot;):match(&amp;#039;^[%s#]*(.-)[%s;]*$&amp;#039;)&lt;br /&gt;
	if (#cleanColor == 6) then&lt;br /&gt;
		return {&lt;br /&gt;
			r = tonumber(string.sub(cleanColor, 1, 2), 16),&lt;br /&gt;
			g = tonumber(string.sub(cleanColor, 3, 4), 16),&lt;br /&gt;
			b = tonumber(string.sub(cleanColor, 5, 6), 16)&lt;br /&gt;
		}&lt;br /&gt;
	elseif (#cleanColor == 3) then&lt;br /&gt;
		return {&lt;br /&gt;
			r = 17 * tonumber(string.sub(cleanColor, 1, 1), 16),&lt;br /&gt;
			g = 17 * tonumber(string.sub(cleanColor, 2, 2), 16),&lt;br /&gt;
			b = 17 * tonumber(string.sub(cleanColor, 3, 3), 16)&lt;br /&gt;
		}&lt;br /&gt;
	end&lt;br /&gt;
	error(&amp;quot;Invalid hexadecimal color &amp;quot; .. cleanColor, 1)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function round(v)&lt;br /&gt;
	if (v &amp;lt; 0) then&lt;br /&gt;
		return math.ceil(v - 0.5)&lt;br /&gt;
	else&lt;br /&gt;
		return math.floor(v + 0.5)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function rgbToHex(r, g, b)&lt;br /&gt;
	return string.format(&amp;quot;%02X%02X%02X&amp;quot;, round(r), round(g), round(b))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function rgbToCmyk(r, g, b)&lt;br /&gt;
	if (r &amp;gt; 255 or g &amp;gt; 255 or b &amp;gt; 255 or r &amp;lt; 0 or g &amp;lt; 0 or b &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Color level out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	local c = 1 - r / 255&lt;br /&gt;
	local m = 1 - g / 255&lt;br /&gt;
	local y = 1 - b / 255&lt;br /&gt;
	local k = math.min(c, m, y)&lt;br /&gt;
	if (k == 1) then&lt;br /&gt;
		c = 0&lt;br /&gt;
		m = 0&lt;br /&gt;
		y = 0&lt;br /&gt;
	else&lt;br /&gt;
		local d = 1 - k&lt;br /&gt;
		c = (c - k) / d&lt;br /&gt;
		m = (m - k) / d&lt;br /&gt;
		y = (y - k) / d&lt;br /&gt;
	end&lt;br /&gt;
	return { c = c * 100, m = m * 100, y = y * 100, k = k * 100 }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function rgbToHsl(r, g, b)&lt;br /&gt;
	if (r &amp;gt; 255 or g &amp;gt; 255 or b &amp;gt; 255 or r &amp;lt; 0 or g &amp;lt; 0 or b &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Color level out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	local channelMax = math.max(r, g, b)&lt;br /&gt;
	local channelMin = math.min(r, g, b)&lt;br /&gt;
	local range = channelMax - channelMin&lt;br /&gt;
	local h, s&lt;br /&gt;
	if (range == 0) then&lt;br /&gt;
		h = 0&lt;br /&gt;
	elseif (channelMax == r) then&lt;br /&gt;
		h = 60 * ((g - b) / range)&lt;br /&gt;
		if (h &amp;lt; 0) then&lt;br /&gt;
			h = 360 + h&lt;br /&gt;
		end&lt;br /&gt;
	elseif (channelMax == g) then&lt;br /&gt;
		h = 60 * (2 + (b - r) / range)&lt;br /&gt;
	else&lt;br /&gt;
		h = 60 * (4 + (r - g) / range)&lt;br /&gt;
	end&lt;br /&gt;
	local L = channelMax + channelMin&lt;br /&gt;
	if (L == 0 or L == 510) then&lt;br /&gt;
		s = 0&lt;br /&gt;
	else&lt;br /&gt;
		s = 100 * range / math.min(L, 510 - L)&lt;br /&gt;
	end&lt;br /&gt;
	return { h = h, s = s, l = L * 50 / 255 }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function rgbToHsv(r, g, b)&lt;br /&gt;
	if (r &amp;gt; 255 or g &amp;gt; 255 or b &amp;gt; 255 or r &amp;lt; 0 or g &amp;lt; 0 or b &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Color level out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	local channelMax = math.max(r, g, b)&lt;br /&gt;
	local channelMin = math.min(r, g, b)&lt;br /&gt;
	local range = channelMax - channelMin&lt;br /&gt;
	local h, s&lt;br /&gt;
	if (range == 0) then&lt;br /&gt;
		h = 0&lt;br /&gt;
	elseif (channelMax == r) then&lt;br /&gt;
		h = 60 * ((g - b) / range)&lt;br /&gt;
		if (h &amp;lt; 0) then&lt;br /&gt;
			h = 360 + h&lt;br /&gt;
		end&lt;br /&gt;
	elseif (channelMax == g) then&lt;br /&gt;
		h = 60 * (2 + (b - r) / range)&lt;br /&gt;
	else&lt;br /&gt;
		h = 60 * (4 + (r - g) / range)&lt;br /&gt;
	end&lt;br /&gt;
	if (channelMax == 0) then&lt;br /&gt;
		s = 0&lt;br /&gt;
	else&lt;br /&gt;
		s = 100 * range / channelMax&lt;br /&gt;
	end&lt;br /&gt;
	return { h = h, s = s, v = channelMax * 100 / 255 }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- c in [0, 255], condition tweaked for no discontinuity&lt;br /&gt;
-- http://entropymine.com/imageworsener/srgbformula/&lt;br /&gt;
local function toLinear(c)&lt;br /&gt;
	if (c &amp;gt; 10.314300250662591) then&lt;br /&gt;
		return math.pow((c + 14.025) / 269.025, 2.4)&lt;br /&gt;
	else&lt;br /&gt;
		return c / 3294.6&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function toNonLinear(c)&lt;br /&gt;
	if (c &amp;gt; 0.00313066844250063) then&lt;br /&gt;
		return 269.025 * math.pow(c, 1.0/2.4) - 14.025&lt;br /&gt;
	else&lt;br /&gt;
		return 3294.6 * c&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function srgbToCielchuvD65o2deg(r, g, b)&lt;br /&gt;
	if (r &amp;gt; 255 or g &amp;gt; 255 or b &amp;gt; 255 or r &amp;lt; 0 or g &amp;lt; 0 or b &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Color level out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	local R = toLinear(r)&lt;br /&gt;
	local G = toLinear(g)&lt;br /&gt;
	local B = toLinear(b)&lt;br /&gt;
	-- https://github.com/w3c/csswg-drafts/issues/5922&lt;br /&gt;
	local X = 0.1804807884018343 * B + 0.357584339383878 * G + 0.41239079926595934 * R&lt;br /&gt;
	local Y = 0.07219231536073371 * B + 0.21263900587151027 * R + 0.715168678767756 * G&lt;br /&gt;
	local Z = 0.01933081871559182 * R + 0.11919477979462598 * G + 0.9505321522496607 * B&lt;br /&gt;
	local L, C, h&lt;br /&gt;
	if (Y &amp;gt; 0.00885645167903563082) then&lt;br /&gt;
		L = 116 * math.pow(Y, 1/3) - 16&lt;br /&gt;
	else&lt;br /&gt;
		L = Y * 903.2962962962962962963&lt;br /&gt;
	end&lt;br /&gt;
	if ((r == g and g == b) or L == 0) then&lt;br /&gt;
		C = 0&lt;br /&gt;
		h = 0&lt;br /&gt;
	else&lt;br /&gt;
		d = X + 3 * Z + 15 * Y&lt;br /&gt;
		if (d == 0) then&lt;br /&gt;
			C = 0&lt;br /&gt;
			h = 0&lt;br /&gt;
		else&lt;br /&gt;
			-- 0.19783... and 0.4631... computed with extra precision from (X,Y,Z) when (R,G,B) = (1,1,1),&lt;br /&gt;
			-- in which case (u,v) ≈ (0,0)&lt;br /&gt;
			local us = 4 * X / d - 0.19783000664283678994&lt;br /&gt;
			local vs = 9 * Y / d - 0.46831999493879099801&lt;br /&gt;
			h = math.atan2(vs, us) * 57.2957795130823208768&lt;br /&gt;
			if (h &amp;lt; 0) then&lt;br /&gt;
				h = h + 360&lt;br /&gt;
			elseif (h == 0) then&lt;br /&gt;
				h = 0 -- ensure zero is positive&lt;br /&gt;
			end&lt;br /&gt;
			C = math.sqrt(us * us + vs * vs) * 13 * L&lt;br /&gt;
			if (C == 0) then&lt;br /&gt;
				C = 0&lt;br /&gt;
				h = 0&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return { L = L, C = C, h = h }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function srgbMix(t, r0, g0, b0, r1, g1, b1)&lt;br /&gt;
	if (t &amp;gt; 1 or t &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Interpolation parameter out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	if (r0 &amp;gt; 255 or g0 &amp;gt; 255 or b0 &amp;gt; 255 or r1 &amp;gt; 255 or g1 &amp;gt; 255 or b1 &amp;gt; 255 or r0 &amp;lt; 0 or g0 &amp;lt; 0 or b0 &amp;lt; 0 or r1 &amp;lt; 0 or g1 &amp;lt; 0 or b1 &amp;lt; 0) then&lt;br /&gt;
		error(&amp;quot;Color level out of bounds&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	local tc = 1 - t&lt;br /&gt;
	return {&lt;br /&gt;
		r = toNonLinear(tc * toLinear(r0) + t * toLinear(r1)),&lt;br /&gt;
		g = toNonLinear(tc * toLinear(g0) + t * toLinear(g1)),&lt;br /&gt;
		b = toNonLinear(tc * toLinear(b0) + t * toLinear(b1))&lt;br /&gt;
	}&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function formatToPrecision(value, p)&lt;br /&gt;
	return string.format(&amp;quot;%.&amp;quot; .. p .. &amp;quot;f&amp;quot;, value)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function getFractionalZeros(p)&lt;br /&gt;
	if (p &amp;gt; 0) then&lt;br /&gt;
		return &amp;quot;.&amp;quot; .. string.rep(&amp;quot;0&amp;quot;, p)&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexToRgbTriplet(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex = args[1]&lt;br /&gt;
	if (hex) then&lt;br /&gt;
		local rgb = hexToRgb(hex)&lt;br /&gt;
		return rgb.r .. &amp;#039;, &amp;#039; .. rgb.g .. &amp;#039;, &amp;#039; .. rgb.b&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.rgbTripletToHex(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local r = tonumber(args[1])&lt;br /&gt;
	local g = tonumber(args[2])&lt;br /&gt;
	local b = tonumber(args [3])&lt;br /&gt;
	if (isempty(r) or isempty(g) or isempty(b)) then&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		return rgbToHex(r,g,b)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexToCmyk(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex = args[1]&lt;br /&gt;
	if (hex) then&lt;br /&gt;
		local p = tonumber(args.precision) or 0&lt;br /&gt;
		local s = args.pctsign or &amp;quot;1&amp;quot;&lt;br /&gt;
		local rgb = hexToRgb(hex)&lt;br /&gt;
		local cmyk = rgbToCmyk(rgb.r, rgb.g, rgb.b)&lt;br /&gt;
		local fk = formatToPrecision(cmyk.k, p)&lt;br /&gt;
		local fc, fm, fy&lt;br /&gt;
		local fracZeros = getFractionalZeros(p)&lt;br /&gt;
		if (fk == 100  .. fracZeros) then&lt;br /&gt;
			local fZero = 0 .. fracZeros&lt;br /&gt;
			fc = fZero&lt;br /&gt;
			fm = fZero&lt;br /&gt;
			fy = fZero&lt;br /&gt;
		else&lt;br /&gt;
			fc = formatToPrecision(cmyk.c, p)&lt;br /&gt;
			fm = formatToPrecision(cmyk.m, p)&lt;br /&gt;
			fy = formatToPrecision(cmyk.y, p)&lt;br /&gt;
		end&lt;br /&gt;
		if (s ~= &amp;quot;0&amp;quot;) then&lt;br /&gt;
			return fc .. &amp;quot;%, &amp;quot; .. fm .. &amp;quot;%, &amp;quot; .. fy .. &amp;quot;%, &amp;quot; .. fk .. &amp;quot;%&amp;quot;&lt;br /&gt;
		else&lt;br /&gt;
			return fc .. &amp;quot;, &amp;quot; .. fm .. &amp;quot;, &amp;quot; .. fy .. &amp;quot;, &amp;quot; .. fk&lt;br /&gt;
		end&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexToHsl(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex = args[1]&lt;br /&gt;
	if (hex) then&lt;br /&gt;
		local p = tonumber(args.precision) or 0&lt;br /&gt;
		local rgb = hexToRgb(hex)&lt;br /&gt;
		local hsl = rgbToHsl(rgb.r, rgb.g, rgb.b)&lt;br /&gt;
		local fl = formatToPrecision(hsl.l, p)&lt;br /&gt;
		local fs, fh&lt;br /&gt;
		local fracZeros = getFractionalZeros(p)&lt;br /&gt;
		local fZero = 0 .. fracZeros&lt;br /&gt;
		if (fl == fZero or fl == 100 .. fracZeros) then&lt;br /&gt;
			fs = fZero&lt;br /&gt;
			fh = fZero&lt;br /&gt;
		else&lt;br /&gt;
			fs = formatToPrecision(hsl.s, p)&lt;br /&gt;
			if (fs == fZero) then&lt;br /&gt;
				fh = fZero&lt;br /&gt;
			else&lt;br /&gt;
				fh = formatToPrecision(hsl.h, p)&lt;br /&gt;
				if (fh == 360 .. fracZeros) then&lt;br /&gt;
					fh = fZero -- handle rounding to 360&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		return fh .. &amp;quot;°, &amp;quot; .. fs .. &amp;quot;%, &amp;quot; .. fl .. &amp;quot;%&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexToHsv(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex = args[1]&lt;br /&gt;
	if (hex) then&lt;br /&gt;
		local p = tonumber(args.precision) or 0&lt;br /&gt;
		local rgb = hexToRgb(hex)&lt;br /&gt;
		local hsv = rgbToHsv(rgb.r, rgb.g, rgb.b)&lt;br /&gt;
		local fv = formatToPrecision(hsv.v, p)&lt;br /&gt;
		local fs, fh&lt;br /&gt;
		local fracZeros = getFractionalZeros(p)&lt;br /&gt;
		local fZero = 0 .. fracZeros&lt;br /&gt;
		if (fv == fZero) then&lt;br /&gt;
			fh = fZero&lt;br /&gt;
			fs = fZero&lt;br /&gt;
		else&lt;br /&gt;
			fs = formatToPrecision(hsv.s, p)&lt;br /&gt;
			if (fs == fZero) then&lt;br /&gt;
				fh = fZero&lt;br /&gt;
			else&lt;br /&gt;
				fh = formatToPrecision(hsv.h, p)&lt;br /&gt;
				if (fh == 360 .. fracZeros) then&lt;br /&gt;
					fh = fZero -- handle rounding to 360&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		return fh .. &amp;quot;°, &amp;quot; .. fs .. &amp;quot;%, &amp;quot; .. fv .. &amp;quot;%&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexToCielch(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex = args[1]&lt;br /&gt;
	if (hex) then&lt;br /&gt;
		local p = tonumber(args.precision) or 0&lt;br /&gt;
		local rgb = hexToRgb(hex)&lt;br /&gt;
		local LCh = srgbToCielchuvD65o2deg(rgb.r, rgb.g, rgb.b)&lt;br /&gt;
		local fL = formatToPrecision(LCh.L, p)&lt;br /&gt;
		local fC, fh&lt;br /&gt;
		local fracZeros = getFractionalZeros(p)&lt;br /&gt;
		local fZero = 0 .. fracZeros&lt;br /&gt;
		if (fL == fZero or fL == 100 .. fracZeros) then&lt;br /&gt;
			fC = fZero&lt;br /&gt;
			fh = fZero&lt;br /&gt;
		else&lt;br /&gt;
			fC = formatToPrecision(LCh.C, p)&lt;br /&gt;
			if (fC == fZero) then&lt;br /&gt;
				fh = fZero&lt;br /&gt;
			else&lt;br /&gt;
				fh = formatToPrecision(LCh.h, p)&lt;br /&gt;
				if (fh == 360 .. fracZeros) then&lt;br /&gt;
					fh = fZero -- handle rounding to 360&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		return fL .. &amp;quot;, &amp;quot; .. fC .. &amp;quot;, &amp;quot; .. fh .. &amp;quot;°&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hexMix(frame)&lt;br /&gt;
	local args = frame.args or frame:getParent().args&lt;br /&gt;
	local hex0 = args[1]&lt;br /&gt;
	local hex1 = args[2]&lt;br /&gt;
	if (isempty(hex0) or isempty(hex1)) then&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local t = tonumber(args[3])&lt;br /&gt;
	if not t then&lt;br /&gt;
		t = 0.5&lt;br /&gt;
	else&lt;br /&gt;
		local min = tonumber(args.min) or 0&lt;br /&gt;
		local max = tonumber(args.max) or 100&lt;br /&gt;
		if (min &amp;gt;= max) then&lt;br /&gt;
			error(&amp;quot;Minimum proportion greater than or equal to maximum&amp;quot;)&lt;br /&gt;
		elseif (t &amp;lt; min) then&lt;br /&gt;
			t = 0&lt;br /&gt;
		elseif (t &amp;gt; max) then&lt;br /&gt;
			t = 1&lt;br /&gt;
		else&lt;br /&gt;
			t = (t - min) / (max - min)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	local rgb0 = hexToRgb(hex0)&lt;br /&gt;
	local rgb1 = hexToRgb(hex1)&lt;br /&gt;
	local rgb = srgbMix(t, rgb0.r, rgb0.g, rgb0.b, rgb1.r, rgb1.g, rgb1.b)&lt;br /&gt;
	return rgbToHex(rgb.r, rgb.g, rgb.b)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>&gt;Hike395</name></author>
	</entry>
</feed>