Module:Color/sandbox
![]() | This is the module sandbox page for Module:Color (diff). See also the companion subpage for test cases (run). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 620 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module is used primarily by {{Infobox color}}, eliminating the need for external color converters and preventing mismatch between color coordinates.
Usage
To use this module, you may use one of the above listed templates or invoke the module directly. All functions that accept hexadecimal triplets also handle the shorthand three-digit format.
To convert a hexadecimal triplet to an RGB triplet as comma-separated values:
{{#invoke:Color|hexToRgbTriplet|color}}
To convert a hexadecimal triplet to the CMYK color model without a color profile (which is a very bad idea!):
{{#invoke:Color|hexToCmyk|color|precision=?|pctsign=?}}
To convert a hexadecimal triplet to HSL or HSV:
{{#invoke:Color|hexToHsl|color|precision=?}}
{{#invoke:Color|hexToHsv|color|precision=?}}
To convert a hexadecimal triplet to the perceptual CIELChuv color space:
{{#invoke:Color|hexToCielch|color|precision=?}}
To mix two colors in the more physically correct linear RGB space:
{{#invoke:Color|hexMix|color1|color2|proportion|min=?|max=?}}
To convert an RGB triplet to a hex code:
{{#invoke:Color|rgbTripletToHex|r|g|b}}
The following parameters are optional:
precision
: defaults to0
(zero)pctsign
: set to0
(zero) to suppress percent signs in the generated outputproportion
: proportion ofcolor2
, defaults to 50min
: minimum value of proportion range, defaults to 0max
: maximum value of proportion range, defaults to 100
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