Module:WavelengthToRGB
Implements Template:Wavelength color.
-- Module:WavelengthToRGB
-- Converts a wavelength (380–780 nm) to sRGB hex color
-- Includes smooth edge fade and blue compression for perceptually correct colors
local p = {}
-- ============================================================================
-- CIE 1931 2° CMFs and D65 illuminant (5 nm steps, 380–780 nm)
-- ============================================================================
local data = {
{380, 0.001368, 0.000039, 0.006450, 49.98},
{385, 0.002236, 0.000064, 0.010550, 52.31},
{390, 0.004243, 0.000120, 0.020050, 54.65},
{395, 0.007650, 0.000217, 0.036210, 68.70},
{400, 0.014310, 0.000396, 0.067850, 82.75},
{405, 0.023190, 0.000640, 0.110200, 87.12},
{410, 0.043510, 0.001210, 0.207400, 91.49},
{415, 0.077630, 0.002180, 0.371300, 92.46},
{420, 0.134380, 0.004000, 0.645600, 93.43},
{425, 0.214770, 0.007300, 1.039050, 90.06},
{430, 0.283900, 0.011600, 1.385600, 86.68},
{435, 0.328500, 0.016840, 1.622960, 95.77},
{440, 0.348280, 0.023000, 1.747060,104.86},
{445, 0.348060, 0.029800, 1.782600,110.94},
{450, 0.336200, 0.038000, 1.772110,117.01},
{455, 0.318700, 0.048000, 1.744100,117.41},
{460, 0.290800, 0.060000, 1.669200,117.81},
{465, 0.251100, 0.073900, 1.528100,116.34},
{470, 0.195360, 0.090980, 1.287640,114.86},
{475, 0.142100, 0.112600, 1.041900,115.39},
{480, 0.095640, 0.139020, 0.812950,115.92},
{485, 0.057950, 0.169300, 0.616200,112.37},
{490, 0.032010, 0.208020, 0.465180,108.81},
{495, 0.014700, 0.258600, 0.353300,109.35},
{500, 0.004900, 0.323000, 0.272000,109.88},
{505, 0.002400, 0.407300, 0.212300,108.65},
{510, 0.009300, 0.503000, 0.158200,107.41},
{515, 0.029100, 0.608200, 0.111700,106.18},
{520, 0.063270, 0.710000, 0.078250,104.94},
{525, 0.109600, 0.793200, 0.057250,104.74},
{530, 0.165500, 0.862000, 0.042160,104.54},
{535, 0.225750, 0.914850, 0.029840,104.33},
{540, 0.290400, 0.954000, 0.020300,104.13},
{545, 0.359700, 0.980300, 0.013400,103.94},
{550, 0.433450, 0.994950, 0.008750,103.74},
{555, 0.512050, 1.000000, 0.005750,103.54},
{560, 0.594500, 0.995000, 0.003900,103.34},
{565, 0.678400, 0.978600, 0.002750,103.13},
{570, 0.762100, 0.952000, 0.002100,102.93},
{575, 0.842500, 0.915400, 0.001800,102.73},
{580, 0.916300, 0.870000, 0.001650,102.52},
{585, 0.978600, 0.816300, 0.001400,102.32},
{590, 1.026300, 0.757000, 0.001100,102.12},
{595, 1.056700, 0.694900, 0.001000,101.92},
{600, 1.062200, 0.631000, 0.000800,101.72},
{605, 1.045600, 0.566800, 0.000600,101.52},
{610, 1.002600, 0.503000, 0.000340,101.31},
{615, 0.938400, 0.441200, 0.000240,101.11},
{620, 0.854450, 0.381000, 0.000190,100.91},
{625, 0.751400, 0.321000, 0.000100,100.71},
{630, 0.642400, 0.265000, 0.000050,100.51},
{635, 0.541900, 0.217000, 0.000030,100.31},
{640, 0.447900, 0.175000, 0.000020,100.11},
{645, 0.360800, 0.138200, 0.000010, 99.91},
{650, 0.283500, 0.107000, 0.000000, 99.71},
{655, 0.218700, 0.081600, 0.000000, 99.51},
{660, 0.164900, 0.061000, 0.000000, 99.31},
{665, 0.121200, 0.044580, 0.000000, 99.11},
{670, 0.087400, 0.032000, 0.000000, 98.91},
{675, 0.063600, 0.023200, 0.000000, 98.71},
{680, 0.046770, 0.017000, 0.000000, 98.51},
{685, 0.032900, 0.011920, 0.000000, 98.31},
{690, 0.022700, 0.008210, 0.000000, 98.11},
{695, 0.015840, 0.005723, 0.000000, 97.91},
{700, 0.011359, 0.004102, 0.000000, 97.71},
{705, 0.008111, 0.002929, 0.000000, 97.51},
{710, 0.005790, 0.002091, 0.000000, 97.31},
{715, 0.004109, 0.001484, 0.000000, 97.11},
{720, 0.002899, 0.001047, 0.000000, 96.91},
{725, 0.002049, 0.000740, 0.000000, 96.71},
{730, 0.001440, 0.000520, 0.000000, 96.51},
{735, 0.001000, 0.000361, 0.000000, 96.31},
{740, 0.000690, 0.000249, 0.000000, 96.11},
{745, 0.000476, 0.000172, 0.000000, 95.91},
{750, 0.000332, 0.000120, 0.000000, 95.71},
{755, 0.000235, 0.000085, 0.000000, 95.51},
{760, 0.000166, 0.000060, 0.000000, 95.31},
{765, 0.000117, 0.000042, 0.000000, 95.11},
{770, 0.000083, 0.000030, 0.000000, 94.91},
{775, 0.000059, 0.000021, 0.000000, 94.71},
{780, 0.000042, 0.000015, 0.000000, 94.51},
}
-- ============================================================================
-- Utilities
-- ============================================================================
local function lerp(a, b, t) return a + (b-a)*t end
local function clamp(x) if x<0 then return 0 elseif x>1 then return 1 else return x end end
local function srgb_gamma(c)
if c <= 0.0031308 then return 12.92*c else return 1.055*(c^(1/2.4))-0.055 end
end
-- ============================================================================
-- Main conversion
-- ============================================================================
function p.fromWavelength(frame)
local wl = tonumber(frame.args[1])
if not wl or wl < 380 or wl > 780 then return "000000" end
-- find surrounding samples
local i = 1
while i < #data and data[i+1][1] < wl do i = i + 1 end
local a, b = data[i], data[i+1] or data[i]
local t = (wl - a[1]) / ((b[1]-a[1]) ~= 0 and (b[1]-a[1]) or 1)
local x = lerp(a[2], b[2], t)
local y = lerp(a[3], b[3], t)
local z = lerp(a[4], b[4], t)
local d = lerp(a[5], b[5], t)
local X, Y, Z = x*d, y*d, z*d
-- linear sRGB
local r = 3.2406*X - 1.5372*Y - 0.4986*Z
local g = -0.9689*X + 1.8758*Y + 0.0415*Z
local b = 0.0557*X - 0.2040*Y + 1.0570*Z
-- smooth edge fade
local f = 1
if wl < 420 then f = 0.5*(1 - math.cos(math.pi * math.min(1, math.max(0, (wl-380)/40)))) end
if wl > 700 then f = 0.5*(1 - math.cos(math.pi * math.min(1, math.max(0, (780-wl)/80)))) end
r, g, b = r*f, g*f, b*f
-- compress extreme blues
if wl < 470 then local s = 0.8 + 0.2*(wl-380)/(470-380); r,g,b = r*s,g*s,b*s end
-- scale to fit sRGB gamut
local maxc = math.max(r,g,b,0)
if maxc > 1 then r,g,b = r/maxc, g/maxc, b/maxc end
-- gamma + clamp
r = clamp(srgb_gamma(r))
g = clamp(srgb_gamma(g))
b = clamp(srgb_gamma(b))
return string.format("%02X%02X%02X", math.floor(r*255+0.5), math.floor(g*255+0.5), math.floor(b*255+0.5))
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.