PowerShell is a generic script language so you can do all kinds of stuff with it. Below is a function that takes a light wavelength and converts it to the appropriate RGB color value:
function Convert-WavelengthToRgb { # derived from http://www.physics.sfasu.edu/astro/color/spectra.html param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [ValidateRange(380,780)] [int] $Wavelength, [double] $Gamma = 0.8, [int] [ValidateRange(1,255)] $Intensity = 255, [switch] $AdjustHumanSensitivity ) process { #from: https://www.codedrome.com/exploring-the-visible-spectrum-in-python/ $factor = 0 $result = [PSCustomObject]@{ Wavelength = $Wavelength R = 0 G = 0 B = 0 } switch($Wavelength) { { $_ -lt 420 } { $result.R = -($_ - 440) / 60 $result.B = 1 $factor = 0.3 + 0.7 * ($_ - 380) / 40 break } { $_ -lt 440 } { $result.R = -($_ - 440) / 60 $result.B = 1 $factor = 1 break } { $_ -lt 490 } { $result.G = ($_ - 440) / 50 $result.B = 1 $factor = 1 break } { $_ -lt 510 } { $result.B = -($_ - 510) / 20 $result.G = 1 $factor = 1 break } { $_ -lt 580 } { $result.R = ($_ - 510) / 70 $result.G = 1 $factor = 1 break } { $_ -lt 645 } { $result.G = -($_ - 645) / 65 $result.R = 1 $factor = 1 break } { $_ -le 700 } { $result.R = 1 $factor = 1 break } { $_ -le 780 } { $result.R = 1 $factor = 0.3 + 0.7 * (780 - $_) / 80 break } } if ($AdjustHumanSensitivity.IsPresent -eq $false) { $factor = 1 } $result.R = [Math]::Pow( ($result.R * $factor), $gamma) * $Intensity -as [int] $result.G = [Math]::Pow( ($result.G * $factor), $gamma) * $Intensity -as [int] $result.B = [Math]::Pow( ($result.B * $factor), $gamma) * $Intensity -as [int] return $result } }
Now it’s simple to convert the entire visible light spectrum to the corresponding RGB values:
# Calculate RGB values for the visible light spectrum (wavelengths) 380..780 | Convert-WavelengthToRgb | Out-GridView -Title 'Without Correction'
Since the human eye sensitivity is different for different colors, the function can even take into account this sensitivity and automatically apply a correction:
380..780 | Convert-WavelengthToRgb -AdjustHumanSensitivity | Out-GridView -Title 'With Correction'