Skip to content

Instantly share code, notes, and snippets.

@lnacquaroli
Last active September 3, 2019 02:16
Show Gist options
  • Save lnacquaroli/a941482c0de2b18ec2408a66cea3124f to your computer and use it in GitHub Desktop.
Save lnacquaroli/a941482c0de2b18ec2408a66cea3124f to your computer and use it in GitHub Desktop.
RGB matrix-color based on the wavelength
"""
Returns an RGB matrix-color based on the wavelength input in nm.
RBG = wavelength2rgb(λ)
Wavelength range: 380nm and 780nm, black otherwise.
Source: https://stackoverflow.com/questions/3407942/rgb-values-of-visible-spectrum
"""
function wavelength2rgb(λ::AbstractArray{<:Number})
Γ = 0.8
# warm up
R = Array{Float64, 1}(undef, length(λ))
G = similar(R)
B = similar(R)
intensityCorrection = similar(R)
R[380 .<= λ .<= 440] = -(λ[380 .<= λ .<= 440] - 440.) ./ (440. - 380.)
G[380 .<= λ .<= 440] = 0. * λ[380 .<= λ .<= 440]
B[380 .<= λ .<= 440] = 1. + (0. * λ[380 .<= λ .<= 440])
R[440 .< λ .<= 490] = 0.
G[440 .< λ .<= 490] = ((λ[440 .< λ .<= 490] - 440.) ./ (490. - 440.))
B[440 .< λ .<= 490] = 1.
R[490 .< λ .<= 510] = 0.
G[490 .< λ .<= 510] = 1.
B[490 .< λ .<= 510] = (-(λ[490 .< λ .<= 510] - 510.) ./ (510. - 490.))
R[510 .< λ .<= 580] = ((λ[510 .< λ .<= 580] - 510.) ./ (580. - 510.))
G[510 .< λ .<= 580] = 1.
B[510 .< λ .<= 580] = 0.
R[580 .< λ .<= 645] = 1.
G[580 .< λ .<= 645] = (-(λ[580 .< λ .<= 645] - 645.) ./ (645. - 580.))
B[580 .< λ .<= 645] = 0.
R[645 .< λ .<= 780] = 1.
G[645 .< λ .<= 780] = 0.
B[645 .< λ .<= 780] = 0.
R[λ .> 780] = 0.
R[λ .< 380] = 0.
G[λ .> 780] = 0.
G[λ .< 380] = 0.
B[λ .> 780] = 0.
B[λ .< 380] = 0.
# LET THE INTENSITY SSS FALL OFF NEAR THE VISION LIMITS
intensityCorrection[λ .> 700] = 0.3 + 0.7 * (780. - λ[λ .> 700]) ./ (780. - 700.)
intensityCorrection[λ .< 420] = 0.3 + 0.7 * (λ[λ .< 420] - 380.) ./ (420. - 380.)
intensityCorrection[420 .< λ .< 700]= 1.
# GAMMA ADJUST
R = (intensityCorrection .* R).^Γ
G = (intensityCorrection .* G).^Γ
B = (intensityCorrection .* B).^Γ
# # Multiply by 255
# R = R * 255
# G = G * 255
# B = B * 255
return [R G B]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment