Module:LuminanceChroma to sRGB

-- 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 "&#35;" .. 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.

  1. 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:
  2. 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.
  3. 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.
  4. 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.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.