September
---
2004




Determining RGB Color from a wavelength in C#

While working on a software project with Flow Cytometers, I had to find the specific color for a specific laser wavelength and could really find much online. I decided to write a quick app that would accept an integer (wavelength) and returns a System.Color object. With a little modification you can have it return either of the three RGB values.

WavelengthToRGB.zip (5kb)

image1 image2

Here is the graphical representation of Wavelength in nm to RGB colors

colors0
colors1
colors2

To convert a particular wavelength of light into a colour that can be displayed on a computer monitor, an algorithm is necessary to generate RGB values (the amplitude of Red, Green and Blue signals) used by the computer display. The code is based on an algorithm from Dan Bruton's Color Science Page.  The conversion process from wavelength to RGB values is shown graphicallybelow, together with the resulting display of spectrum from 380 to 700 nm. The Bruton algorithm is obviously a simplification of a complex process.  More complex algorithms have been investigated, such as those developed by CIE (Commission Internationale Eclairage - International Lighting Commission) but none of them produce an aesthetically pleasing spectrum! As the coloured depictions of optical phenomena depend crucially on the relationship between wavelength and colour, more work is required in this area. [Phillip Laven]

aspx.cs code

private Color getColorFromWaveLength(int Wavelength){
  double Gamma = 1.00;
  int IntensityMax = 255;
  double Blue;
  double Green;
  double Red;
  double Factor;

  if(Wavelength >= 350 && Wavelength <= 439){
   Red	= -(Wavelength - 440d) / (440d - 350d);
   Green = 0.0;
   Blue	= 1.0;
  }else if(Wavelength >= 440 && Wavelength <= 489){
   Red	= 0.0;
   Green = (Wavelength - 440d) / (490d - 440d);
   Blue	= 1.0;
  }else if(Wavelength >= 490 && Wavelength <= 509){
   Red = 0.0;
   Green = 1.0;
   Blue = -(Wavelength - 510d) / (510d - 490d);
  }else if(Wavelength >= 510 && Wavelength <= 579){    Red = (Wavelength - 510d) / (580d - 510d);    Green = 1.0;    Blue = 0.0;   }else if(Wavelength >= 580 && Wavelength <= 644){    Red = 1.0;    Green = -(Wavelength - 645d) / (645d - 580d);    Blue = 0.0;   }else if(Wavelength >= 645 && Wavelength <= 780){    Red = 1.0;    Green = 0.0;    Blue = 0.0;   }else{    Red = 0.0;    Green = 0.0;    Blue = 0.0;   }   if(Wavelength >= 350 && Wavelength <= 419){    Factor = 0.3 + 0.7*(Wavelength - 350d) / (420d - 350d);   }else if(Wavelength >= 420 && Wavelength <= 700){      Factor = 1.0;   }else if(Wavelength >= 701 && Wavelength <= 780){    Factor = 0.3 + 0.7*(780d - Wavelength) / (780d - 700d);   }else{    Factor = 0.0;  }   int R = this.factorAdjust(Red, Factor, IntensityMax, Gamma);   int G = this.factorAdjust(Green, Factor, IntensityMax, Gamma);   int B = this.factorAdjust(Blue, Factor, IntensityMax, Gamma);   return Color.FromArgb(R, G, B); } private int factorAdjust(double Color,  double Factor,  int IntensityMax,  double Gamma){   if(Color == 0.0){     return 0;    }else{     return (int) Math.Round(IntensityMax * Math.Pow(Color * Factor, Gamma));    } } private void trackBar1_Scroll(object sender, EventArgs e) {    this.panel1.BackColor = getColorFromWaveLength(this.trackBar1.Value);    this.label1.Text = this.trackBar1.Value.ToString() + " nm";    this.label2.Text = "R: " +    ((Color) getColorFromWaveLength(this.trackBar1.Value)).R.ToString();    this.label3.Text = "G: " +    ((Color) getColorFromWaveLength(this.trackBar1.Value)).G.ToString();    this.label4.Text = "B: " +    ((Color) getColorFromWaveLength(this.trackBar1.Value)).B.ToString(); }