Module:LuminanceChroma to sRGB
Module:LuminanceChroma to sRGB
Converts:
- Luminance (percent)
- Chroma angle (degrees)
- Chroma (percent or signed custom range)
into sRGB hexadecimal color.
The module uses an HSL-style cylindrical model and preserves the specified luminance value.
Syntax
{{#invoke:LuminanceChroma to sRGB|convert
| L =
| H =
| C =
| offset =
| Cmax =
| format =
}}
Parameters
| Parameter | Required | Description |
|---|---|---|
L |
Yes | Luminance (0–100%) |
H |
Yes | Chroma angle (degrees) |
C |
Yes | Chroma (percent or signed value) |
offset |
No | Hue rotation offset in degrees |
Cmax |
No | Maximum absolute chroma input (enables signed chroma scaling) |
format |
No | Output format: hex (default) or swatch
|
Basic Example
{{#invoke:LuminanceChroma to sRGB|convert
| L = 50
| H = 120
| C = 60
}}
Output:
#339933
Hue Offset Example
If your hue system defines 103° as pure red:
{{#invoke:LuminanceChroma to sRGB|convert
| L = 50
| H = 103
| C = 100
| offset = -103
}}
The offset rotates the hue internally:
effective hue = (H + offset) mod 360
Signed Chroma Example
To use a signed chroma system such as ±48:
{{#invoke:LuminanceChroma to sRGB|convert
| L = 50
| H = 120
| C = 48
| Cmax = 48
}}
Behavior:
|C| = Cmax→ 100% chroma|C| = Cmax / 2→ 50% chroma- Negative values are treated as magnitude only
Example mapping:
| Input C | Cmax | Internal chroma |
|---|---|---|
| 48 | 48 | 100% |
| -48 | 48 | 100% |
| 24 | 48 | 50% |
Swatch Output
{{#invoke:LuminanceChroma to sRGB|convert
| L = 50
| H = 120
| C = 60
| format = swatch
}}
Displays a color square followed by the hex code.
Notes
- Luminance and chroma values are clamped to valid ranges.
- Hue values are normalized modulo 360°.
- The module operates in gamma-domain sRGB.
- Negative chroma does not rotate hue.
- This is an HSL-style approximation, not CIELCh or OKLCH.
-- Module:LuminanceChroma to sRGB
-- Converts:
-- Luminance (%)
-- Chroma angle (degrees)
-- Chroma (% or signed custom range)
-- to sRGB hex
--
-- Features:
-- • Preserves luminance (HSL model)
-- • Hue offset support
-- • Signed chroma scaling via Cmax
-- • format = hex (default)
-- • format = swatch
local p = {}
------------------------------------------------
-- Clamp helper
------------------------------------------------
local function clamp(x, min, max)
if x < min then return min end
if x > max then return max end
return x
end
------------------------------------------------
-- Convert sRGB (0–1) to hex
------------------------------------------------
local function rgbToHex(r, g, b)
r = clamp(math.floor(r * 255 + 0.5), 0, 255)
g = clamp(math.floor(g * 255 + 0.5), 0, 255)
b = clamp(math.floor(b * 255 + 0.5), 0, 255)
return string.format("#%02X%02X%02X", r, g, b)
end
------------------------------------------------
-- HSL-style cylindrical model
------------------------------------------------
local function hslToRgb(L, C, H)
H = H % 360
local Hp = H / 60
local X = C * (1 - math.abs((Hp % 2) - 1))
local r1, g1, b1 = 0, 0, 0
if Hp >= 0 and Hp < 1 then
r1, g1, b1 = C, X, 0
elseif Hp < 2 then
r1, g1, b1 = X, C, 0
elseif Hp < 3 then
r1, g1, b1 = 0, C, X
elseif Hp < 4 then
r1, g1, b1 = 0, X, C
elseif Hp < 5 then
r1, g1, b1 = X, 0, C
else
r1, g1, b1 = C, 0, X
end
local m = L - C / 2
return r1 + m, g1 + m, b1 + m
end
------------------------------------------------
-- Main conversion
------------------------------------------------
function p.convert(frame)
local args = frame.args
local L = tonumber(args.L)
local H = tonumber(args.H)
local C = tonumber(args.C)
local offset = tonumber(args.offset) or 0
local Cmax = tonumber(args.Cmax)
local format = args.format or "hex"
if not L or not H or not C then
return "Error: L, H, and C must be numeric."
end
------------------------------------------------
-- Normalize luminance
------------------------------------------------
L = clamp(L, 0, 100) / 100
------------------------------------------------
-- Handle chroma
------------------------------------------------
if Cmax then
-- Signed chroma range [-Cmax, Cmax]
C = clamp(C, -Cmax, Cmax)
C = math.abs(C) / Cmax * 100
end
C = clamp(C, 0, 100) / 100
------------------------------------------------
-- Apply hue offset
------------------------------------------------
H = (H + offset) % 360
------------------------------------------------
-- Convert
------------------------------------------------
local R, G, B = hslToRgb(L, C, H)
R = clamp(R, 0, 1)
G = clamp(G, 0, 1)
B = clamp(B, 0, 1)
local hex = rgbToHex(R, G, B)
------------------------------------------------
-- Output
------------------------------------------------
if format == "swatch" then
return string.format(
'<span style="display:inline-block;width:1.5em;height:1.5em;border:1px solid #000;background-color:%s;vertical-align:middle;margin-right:0.4em;"></span><code>%s</code>',
hex,
hex
)
end
return "#" .. string.sub(hex, 2)
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.
- 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:
- 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.
- 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.
- 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.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.