Jump to content

Module:Color/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ftrebien (talk | contribs) at 03:00, 10 January 2022 (Initial version with basic functions). 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 function hexToRgb(color)
	if (#color == 6) then
		return {
			r = tonumber(string.sub(color, 1, 2), 16),
			g = tonumber(string.sub(color, 3, 4), 16),
			b = tonumber(string.sub(color, 5, 6), 16)
		}
	elseif (#color == 3) then
		return {
			r = 17 * tonumber(string.sub(color, 1, 1), 16),
			g = 17 * tonumber(string.sub(color, 2, 2), 16),
			b = 17 * tonumber(string.sub(color, 3, 3), 16)
		}
	end
	error("Invalid hexadecimal color " .. color, 1)
end

local function toLinear(c)
	if (c <= 10.314300250662591) then
		return c / 3294.6
	else
		return math.pow((c + 14.025) / 269.025, 2.4)
	end
end

local function rgbToHsl(r, g, b)
	channelMax = math.max(r, g, b)
	channelMin = math.min(r, g, b)
	range = channelMax - channelMin
	if (range == 0) then
		h = 0
	elseif (channelMax == r) then
		h = 60 * ((g - b) / range)
	elseif (channelMax == g) then
		h = 60 * (2 + (b - r) / range)
	else
    	h = 60 * (4 + (r - g) / range)
	end
    if (h < 0) then
    	h = 360 + h
    end
    L = channelMax + channelMin
	if (L == 0 or L == 510) then
		s = 0
	else
		s = 100 * range / math.min(L, 510 - L)
	end
	return { h = h, s = s, l = L * 50 / 255 }
end

local function rgbToHsv(r, g, b)
	channelMax = math.max(r, g, b)
	channelMin = math.min(r, g, b)
	range = channelMax - channelMin
	if (range == 0) then
		h = 0
	elseif (channelMax == r) then
		h = 60 * ((g - b) / range)
	elseif (channelMax == g) then
		h = 60 * (2 + (b - r) / range)
	else
		h = 60 * (4 + (r - g) / range)
	end
    if (h < 0) then
    	h = 360 + h
    end
	if (channelMax == 0) then
		s = 0
	else
		s = 100 * range / channelMax
	end
	return { h = h, s = s, v = channelMax * 100 / 255 }
end

local function rgbhexToRgb(frame)
	local args = frame.args
	local hex = args[1]
	local p = args.precision or 0
	if (hex) then
		local rgb = hexToRgb(hex)
		return string.format("%." .. p .. "f°, %." .. p .. "f%%, %." .. p .. "f%%", rgb.r, rgb.g, rgb.b)
	else
		return ""
	end
end

function p.rgbhexToHsl(frame)
	local args = frame.args
	local hex = args[1]
	local p = args.precision or 0
	if (hex) then
		local rgb = hexToRgb(hex)
		local hsl = rgbToHsl(rgb.r, rgb.g, rgb.l)
		return string.format("%." .. p .. "f°, %." .. p .. "f%%, %." .. p .. "f%%", hsl.h, hsl.s, hsl.l)
	else
		return ""
	end
end

function p.rgbhexToHsv(frame)
	local args = frame.args
	local hex = args[1]
	local p = args.precision or 0
	if (hex) then
		local rgb = hexToRgb(hex)
		local hsv = rgbToHsv(rgb.r, rgb.g, rgb.l)
		return string.format("%." .. p .. "f°, %." .. p .. "f%%, %." .. p .. "f%%", hsv.h, hsv.s, hsv.v)
	else
		return ""
	end
end