# A program/Java applet that will show me the color of a wavelength?

**URL:** https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748
**Category:** Factual Questions
**Created:** [May 30, 2006, 4:08pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748 "2006-05-30T16:08:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![threemae](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@threemae](https://boards.straightdope.com/u/threemae)
#### Post date: [May 30, 2006, 4:08pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/1 "2006-05-30T16:08:49Z")

</div>

Now that I’ve started working in a lab over the summer I’ve run across a few things such as this:

“After staining, gels are destained and visualized using a standard UV light box, or any of a variety of laser-based gel scanners. The excitation wavelengths are 300 and 480 nm and the emission wavelength is 618 nm.”

Of course, it might be awesome to know, ya’ know, what 618 nm wavelength light actually looks like in terms of color. My Google-fu has provided me with many links to spectrums that generally mark where different visible colors lie along the spectrum such as this:

[http://en.wikipedia.org/wiki/Color](http://en.wikipedia.org/wiki/Color)

However, what I would really like is some sort of website with a Java applett or even a small download where I could type in a wavelength or even three or four distinct wavelengths at varrying intensity and the program would spit out a nice, big color “swatch” for me to look at and know what 618 nm light actually looks like.

So, Dopers, where can I find something like that?

---

<div class="post-metadata">

### Author: ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)
#### Post date: [May 31, 2006, 3:21am UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/2 "2006-05-31T03:21:13Z")

</div>

> [@threemae](#):
>
> However, what I would really like is some sort of website with a Java applett or even a small download where I could type in a wavelength or even three or four distinct wavelengths at varrying intensity and the program would spit out a nice, big color “swatch” for me to look at and know what 618 nm light actually looks like.

First, it’s important to note that the spectrum of pure monochromatic colors and the colorspaces commonly used for computer monitors (RGB, etc.) are not really convertible: in each color space there are colors which are perceived differently from any color in the other (as seen in CIE charts like [this one](http://www.ronfell.com/images/lightin/cie.jpg)). You can approximate the spectrum with some mapping from the monochromatic colors to (r,g,b) values, as is done in [this Fortran code](http://www.physics.sfasu.edu/astro/color/spectra.html).

Here is a quick-and-dirty DHTML program to color the HTML background with a color approximating the given wavelength (approximately implementing the Fortran code linked above; I didn’t bother with gamma). setcolor() is the function that does the conversion; it will obviously need to be tweaked for each individual monitor-eye pair to give really good results. Tested on Mozilla only.

```auto

<html>
<head>
<title>Spectral colors</title>
<script>
var reg;
var cin;
var cout;
function init() {
  reg = document.getElementById("region");
  cin = document.getElementById("input");
  cout= document.getElementById("output");
}

function tohex( x ) {
  x = Math.floor(x*255);
  x = x.toString(16);
  if ( x.length == 1 ) x = "0"+x;
  return x;
}

function setcolor( ) { // implement Dan Bruton's wavelength->RGB mapping
  var nm, r, g, b, rgb, s;

  nm = cin.value;
  if ( nm < 380 || nm > 780 ) { cout.value = "Not in [380,780] nm."; return; }
  else if ( nm < 440 ) { r = (440-nm)/(440-380); g = 0; b = 1; }
  else if ( nm < 490 ) { r = 0; g = (nm-440)/(490-440); b = 1; }
  else if ( nm < 510 ) { r = 0; g = 1; b = (510-nm)/(510-490); }
  else if ( nm < 580 ) { r = (nm-510)/(580-510); g = 1; b = 0; }
  else if ( nm < 645 ) { r = 1; g = (645-nm)/(645-580); b = 0; }
  else { /* nm <=780 */ r = 1; g = 0; b = 0; }

  if ( nm > 700 ) { s = 0.3 + 0.7*(780-nm)/(780-700); r *= s; g *= s; b *= s; }
  if ( nm < 420 ) { s = 0.3 + 0.7*(nm-380)/(420-780); r *= s; g *= s; b *= s; }

  rgb = tohex(r) + tohex(g) + tohex(b);
  cout.value = rgb;
  reg.style.backgroundColor = "#" + rgb;
}
</script>
</head>
<body id="region" style="color: #fff; background-color: #000" onload="init()">
<table>
<tr><td>Input wavelength in nm:</td>
    <td><input id="input" type=text onchange="setcolor()"></td></tr>
<tr><td>Output color (#RRGGBB):</td>
    <td><input id="output" type=text readonly></td></tr>
</table>
</body>
</html>

```

You could easily modify this code to accept input via HTML query strings, to have multiple colored regions, etc.

---

<div class="post-metadata">

### Author: ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)
#### Post date: [May 31, 2006, 3:25am UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/3 "2006-05-31T03:25:30Z")

</div>

> [@Omphaloskeptic](#):
>
> First, it’s important to note that the spectrum of pure monochromatic colors and the colorspaces commonly used for computer monitors (RGB, etc.) are not really convertible: in each color space there are colors which are perceived differently from any color in the other (as seen in CIE charts like [this one](http://www.ronfell.com/images/lightin/cie.jpg)).

Well, that was maybe a bad choice of CIE chart, since it doesn’t show a sample RGB colorspace. [Here](http://pioneer.jp/ppd/product/modules/img/key/cie.jpg)’s one that shows some sample RGB color triangles.

---

<div class="post-metadata">

### Author: ![threemae](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@threemae](https://boards.straightdope.com/u/threemae)
#### Post date: [May 31, 2006, 7:14pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/4 "2006-05-31T19:14:59Z")

</div>

Well, **Omphalo** , I say good enough for government work. It’s not perfect but it gives me a very good idea of what color to look for and I do appreciate the effort.

Thank you again,  
**Omphalo**

---

<div class="post-metadata">

### Author: ![threemae](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@threemae](https://boards.straightdope.com/u/threemae)
#### Post date: [May 31, 2006, 7:17pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/5 "2006-05-31T19:17:56Z")

</div>

Ah, damn it all.

My own inclination and talent towards executing such programming is now painfully on display.

---

<div class="post-metadata">

### Author: ![jawdirk](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@jawdirk](https://boards.straightdope.com/u/jawdirk)
#### Post date: [May 31, 2006, 11:16pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/6 "2006-05-31T23:16:19Z")

</div>

No slight, but there is a bug in the code I think. Check out the difference in display between 419 and 420 wavelink inputs.

---

<div class="post-metadata">

### Author: ![jawdirk](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@jawdirk](https://boards.straightdope.com/u/jawdirk)
#### Post date: [May 31, 2006, 11:20pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/7 "2006-05-31T23:20:44Z")

</div>

Yeah,

if ( nm \< 420 ) { s = 0.3 + 0.7\*(nm-380)/(420-780); r \*= s; g \*= s; b \*= s; }

should be

if ( nm \< 420 ) { s = 0.3 + 0.7\*(nm-380)/(420-380); r \*= s; g \*= s; b \*= s; }

---

<div class="post-metadata">

### Author: ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)
#### Post date: [May 31, 2006, 11:46pm UTC](https://boards.straightdope.com/t/a-program-java-applet-that-will-show-me-the-color-of-a-wavelength/358748/8 "2006-05-31T23:46:38Z")

</div>

> [@jawdirk](#):
>
> should be
> 
> if ( nm \< 420 ) { s = 0.3 + 0.7\*(nm-380)/(420-380); r \*= s; g \*= s; b \*= s; }

Oops. Thanks.
