static private final double Gamma = 0.80;
static private final double IntensityMax = 255;
/**
* Taken from Earl F. Glynn's web page:
* <a href="http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm">Spectra Lab Report</a>
*/
public static int[] waveLengthToRGB(double Wavelength) {
double factor;
double Red, Green, Blue;
if((Wavelength >= 380) && (Wavelength < 440)) {
Red = -(Wavelength - 440) / (440 - 380);
Green = 0.0;
Blue = 1.0;
} else if((Wavelength >= 440) && (Wavelength < 490)) {
Red = 0.0;
Green = (Wavelength - 440) / (490 - 440);
Blue = 1.0;
} else if((Wavelength >= 490) && (Wavelength < 510)) {
Red = 0.0;
Green = 1.0;
Blue = -(Wavelength - 510) / (510 - 490);
} else if((Wavelength >= 510) && (Wavelength < 580)) {
Red = (Wavelength - 510) / (580 - 510);
Green = 1.0;
Blue = 0.0;
} else if((Wavelength >= 580) && (Wavelength < 645)) {
Red = 1.0;
Green = -(Wavelength - 645) / (645 - 580);
Blue = 0.0;
} else if((Wavelength >= 645) && (Wavelength < 781)) {
Red = 1.0;
Green = 0.0;
Blue = 0.0;
} else {
Red = 0.0;
Green = 0.0;
Blue = 0.0;
}
// Let the intensity fall off near the vision limits
if((Wavelength >= 380) && (Wavelength < 420)) {
factor = 0.3 + 0.7 * (Wavelength - 380) / (420 - 380);
} else if((Wavelength >= 420) && (Wavelength < 701)) {
factor = 1.0;
} else if((Wavelength >= 701) && (Wavelength < 781)) {
factor = 0.3 + 0.7 * (780 - Wavelength) / (780 - 700);
} else {
factor = 0.0;
}
int[] rgb = new int[3];
// Don't want 0^x = 1 for x <> 0
rgb[0] = Red == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Red * factor, Gamma));
rgb[1] = Green == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Green * factor, Gamma));
rgb[2] = Blue == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Blue * factor, Gamma));
return rgb;
}
For lazy guys (like me), here is an implementation in java of the code found in @user151323 's answer (that is, just a simple translation from pascal code found in Spectra Lab Report):
static private final double Gamma = 0.80;
static private final double IntensityMax = 255;
/**
* Taken from Earl F. Glynn's web page:
* <a href="http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm">Spectra Lab Report</a>
*/
public static int[] waveLengthToRGB(double Wavelength) {
double factor;
double Red, Green, Blue;
if((Wavelength >= 380) && (Wavelength < 440)) {
Red = -(Wavelength - 440) / (440 - 380);
Green = 0.0;
Blue = 1.0;
} else if((Wavelength >= 440) && (Wavelength < 490)) {
Red = 0.0;
Green = (Wavelength - 440) / (490 - 440);
Blue = 1.0;
} else if((Wavelength >= 490) && (Wavelength < 510)) {
Red = 0.0;
Green = 1.0;
Blue = -(Wavelength - 510) / (510 - 490);
} else if((Wavelength >= 510) && (Wavelength < 580)) {
Red = (Wavelength - 510) / (580 - 510);
Green = 1.0;
Blue = 0.0;
} else if((Wavelength >= 580) && (Wavelength < 645)) {
Red = 1.0;
Green = -(Wavelength - 645) / (645 - 580);
Blue = 0.0;
} else if((Wavelength >= 645) && (Wavelength < 781)) {
Red = 1.0;
Green = 0.0;
Blue = 0.0;
} else {
Red = 0.0;
Green = 0.0;
Blue = 0.0;
}
// Let the intensity fall off near the vision limits
if((Wavelength >= 380) && (Wavelength < 420)) {
factor = 0.3 + 0.7 * (Wavelength - 380) / (420 - 380);
} else if((Wavelength >= 420) && (Wavelength < 701)) {
factor = 1.0;
} else if((Wavelength >= 701) && (Wavelength < 781)) {
factor = 0.3 + 0.7 * (780 - Wavelength) / (780 - 700);
} else {
factor = 0.0;
}
int[] rgb = new int[3];
// Don't want 0^x = 1 for x <> 0
rgb[0] = Red == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Red * factor, Gamma));
rgb[1] = Green == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Green * factor, Gamma));
rgb[2] = Blue == 0.0 ? 0 : (int)Math.round(IntensityMax * Math.pow(Blue * factor, Gamma));
return rgb;
}
Clip components to [0..1] and multiply by 255 to fit in the unsigned byte range.
Steps 1 and 2 may vary.
There are several color matching functions, available as tables or as analytic approximations (suggested by @Tarc and @Haochen Xie). Tables are best if you need a smooth preсise result.
Below is the C# code I came up with recently. It uses linear interpolation over the "CIE 1964 standard observer" table and sRGB matrix + gamma correction.
/**
* Convert a wavelength in the visible light spectrum to a RGB color value that is suitable to be displayed on a
* monitor
*
* @param wavelength wavelength in nm
* @return RGB color encoded in int. each color is represented with 8 bits and has a layout of
* 00000000RRRRRRRRGGGGGGGGBBBBBBBB where MSB is at the leftmost
*/
public static int wavelengthToRGB(double wavelength){
double[] xyz = cie1931WavelengthToXYZFit(wavelength);
double[] rgb = srgbXYZ2RGB(xyz);
int c = 0;
c |= (((int) (rgb[0] * 0xFF)) & 0xFF) << 16;
c |= (((int) (rgb[1] * 0xFF)) & 0xFF) << 8;
c |= (((int) (rgb[2] * 0xFF)) & 0xFF) << 0;
return c;
}
/**
* Convert XYZ to RGB in the sRGB color space
* <p>
* The conversion matrix and color component transfer function is taken from http://www.color.org/srgb.pdf, which
* follows the International Electrotechnical Commission standard IEC 61966-2-1 "Multimedia systems and equipment -
* Colour measurement and management - Part 2-1: Colour management - Default RGB colour space - sRGB"
*
* @param xyz XYZ values in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
* @return RGB values in a double array, in the order of R, G, B. each value in the range of [0.0, 1.0]
*/
public static double[] srgbXYZ2RGB(double[] xyz) {
double x = xyz[0];
double y = xyz[1];
double z = xyz[2];
double rl = 3.2406255 * x + -1.537208 * y + -0.4986286 * z;
double gl = -0.9689307 * x + 1.8757561 * y + 0.0415175 * z;
double bl = 0.0557101 * x + -0.2040211 * y + 1.0569959 * z;
return new double[] {
srgbXYZ2RGBPostprocess(rl),
srgbXYZ2RGBPostprocess(gl),
srgbXYZ2RGBPostprocess(bl)
};
}
/**
* helper function for {@link #srgbXYZ2RGB(double[])}
*/
private static double srgbXYZ2RGBPostprocess(double c) {
// clip if c is out of range
c = c > 1 ? 1 : (c < 0 ? 0 : c);
// apply the color component transfer function
c = c <= 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1. / 2.4) - 0.055;
return c;
}
/**
* A multi-lobe, piecewise Gaussian fit of CIE 1931 XYZ Color Matching Functions by Wyman el al. from Nvidia. The
* code here is adopted from the Listing 1 of the paper authored by Wyman et al.
* <p>
* Reference: Chris Wyman, Peter-Pike Sloan, and Peter Shirley, Simple Analytic Approximations to the CIE XYZ Color
* Matching Functions, Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 2, 1-11, 2013.
*
* @param wavelength wavelength in nm
* @return XYZ in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
*/
public static double[] cie1931WavelengthToXYZFit(double wavelength) {
double wave = wavelength;
double x;
{
double t1 = (wave - 442.0) * ((wave < 442.0) ? 0.0624 : 0.0374);
double t2 = (wave - 599.8) * ((wave < 599.8) ? 0.0264 : 0.0323);
double t3 = (wave - 501.1) * ((wave < 501.1) ? 0.0490 : 0.0382);
x = 0.362 * Math.exp(-0.5 * t1 * t1)
+ 1.056 * Math.exp(-0.5 * t2 * t2)
- 0.065 * Math.exp(-0.5 * t3 * t3);
}
double y;
{
double t1 = (wave - 568.8) * ((wave < 568.8) ? 0.0213 : 0.0247);
double t2 = (wave - 530.9) * ((wave < 530.9) ? 0.0613 : 0.0322);
y = 0.821 * Math.exp(-0.5 * t1 * t1)
+ 0.286 * Math.exp(-0.5 * t2 * t2);
}
double z;
{
double t1 = (wave - 437.0) * ((wave < 437.0) ? 0.0845 : 0.0278);
double t2 = (wave - 459.0) * ((wave < 459.0) ? 0.0385 : 0.0725);
z = 1.217 * Math.exp(-0.5 * t1 * t1)
+ 0.681 * Math.exp(-0.5 * t2 * t2);
}
return new double[] { x, y, z };
}
我的代码是用 Java 8 编写的,但将其移植到较低版本的 Java 和其他语言应该不难。
Although this is an old question and already gets a handful good answers, when I tried to implement such conversion functionality in my application I was not satisfied with the algorithms already listed here and did my own research, which gave me some good result. So I'm going to post a new answer.
After some researchs I came across this paper, Simple Analytic Approximations
to the CIE XYZ Color Matching Functions, and tried to adopt the introduced multi-lobe piecewise Gaussian fit algorithm in my application. The paper only described the functions to convert a wavelength to the corresponding XYZ values, so I implemented a function to convert XYZ to RGB in the sRGB color space and combined them. The result is fantastic and worth sharing:
/**
* Convert a wavelength in the visible light spectrum to a RGB color value that is suitable to be displayed on a
* monitor
*
* @param wavelength wavelength in nm
* @return RGB color encoded in int. each color is represented with 8 bits and has a layout of
* 00000000RRRRRRRRGGGGGGGGBBBBBBBB where MSB is at the leftmost
*/
public static int wavelengthToRGB(double wavelength){
double[] xyz = cie1931WavelengthToXYZFit(wavelength);
double[] rgb = srgbXYZ2RGB(xyz);
int c = 0;
c |= (((int) (rgb[0] * 0xFF)) & 0xFF) << 16;
c |= (((int) (rgb[1] * 0xFF)) & 0xFF) << 8;
c |= (((int) (rgb[2] * 0xFF)) & 0xFF) << 0;
return c;
}
/**
* Convert XYZ to RGB in the sRGB color space
* <p>
* The conversion matrix and color component transfer function is taken from http://www.color.org/srgb.pdf, which
* follows the International Electrotechnical Commission standard IEC 61966-2-1 "Multimedia systems and equipment -
* Colour measurement and management - Part 2-1: Colour management - Default RGB colour space - sRGB"
*
* @param xyz XYZ values in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
* @return RGB values in a double array, in the order of R, G, B. each value in the range of [0.0, 1.0]
*/
public static double[] srgbXYZ2RGB(double[] xyz) {
double x = xyz[0];
double y = xyz[1];
double z = xyz[2];
double rl = 3.2406255 * x + -1.537208 * y + -0.4986286 * z;
double gl = -0.9689307 * x + 1.8757561 * y + 0.0415175 * z;
double bl = 0.0557101 * x + -0.2040211 * y + 1.0569959 * z;
return new double[] {
srgbXYZ2RGBPostprocess(rl),
srgbXYZ2RGBPostprocess(gl),
srgbXYZ2RGBPostprocess(bl)
};
}
/**
* helper function for {@link #srgbXYZ2RGB(double[])}
*/
private static double srgbXYZ2RGBPostprocess(double c) {
// clip if c is out of range
c = c > 1 ? 1 : (c < 0 ? 0 : c);
// apply the color component transfer function
c = c <= 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1. / 2.4) - 0.055;
return c;
}
/**
* A multi-lobe, piecewise Gaussian fit of CIE 1931 XYZ Color Matching Functions by Wyman el al. from Nvidia. The
* code here is adopted from the Listing 1 of the paper authored by Wyman et al.
* <p>
* Reference: Chris Wyman, Peter-Pike Sloan, and Peter Shirley, Simple Analytic Approximations to the CIE XYZ Color
* Matching Functions, Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 2, 1-11, 2013.
*
* @param wavelength wavelength in nm
* @return XYZ in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
*/
public static double[] cie1931WavelengthToXYZFit(double wavelength) {
double wave = wavelength;
double x;
{
double t1 = (wave - 442.0) * ((wave < 442.0) ? 0.0624 : 0.0374);
double t2 = (wave - 599.8) * ((wave < 599.8) ? 0.0264 : 0.0323);
double t3 = (wave - 501.1) * ((wave < 501.1) ? 0.0490 : 0.0382);
x = 0.362 * Math.exp(-0.5 * t1 * t1)
+ 1.056 * Math.exp(-0.5 * t2 * t2)
- 0.065 * Math.exp(-0.5 * t3 * t3);
}
double y;
{
double t1 = (wave - 568.8) * ((wave < 568.8) ? 0.0213 : 0.0247);
double t2 = (wave - 530.9) * ((wave < 530.9) ? 0.0613 : 0.0322);
y = 0.821 * Math.exp(-0.5 * t1 * t1)
+ 0.286 * Math.exp(-0.5 * t2 * t2);
}
double z;
{
double t1 = (wave - 437.0) * ((wave < 437.0) ? 0.0845 : 0.0278);
double t2 = (wave - 459.0) * ((wave < 459.0) ? 0.0385 : 0.0725);
z = 1.217 * Math.exp(-0.5 * t1 * t1)
+ 0.681 * Math.exp(-0.5 * t2 * t2);
}
return new double[] { x, y, z };
}
my code is written in Java 8, but it shouldn't be hard to port it to lower versions of Java and other languages.
I guess I might as well follow up my comment with a formal answer. The best option is to use the HSV colour space - though the hue represents the wavelength it is not a one-to-one comparison.
I did a linear fit of known hue values and frequencies (dropping out red and violet because they extend so far in frequency values that they skew things a bit) and I got a rough conversion equation.
It goes like
frequency (in THz)=474+(3/4)(Hue Angle (in degrees))
I've tried to look around and see if anyone has come up with this equation, but I haven't found anything as of May 2010.
#ifndef common_utils_OnlineStats_hpp
#define common_utils_OnlineStats_hpp
namespace common_utils {
class ColorUtils {
public:
static void valToRGB(double val0To1, unsigned char& r, unsigned char& g, unsigned char& b)
{
//actual visible spectrum is 375 to 725 but outside of 400-700 things become too dark
wavelengthToRGB(val0To1 * (700 - 400) + 400, r, g, b);
}
/**
* Convert a wavelength in the visible light spectrum to a RGB color value that is suitable to be displayed on a
* monitor
*
* @param wavelength wavelength in nm
* @return RGB color encoded in int. each color is represented with 8 bits and has a layout of
* 00000000RRRRRRRRGGGGGGGGBBBBBBBB where MSB is at the leftmost
*/
static void wavelengthToRGB(double wavelength, unsigned char& r, unsigned char& g, unsigned char& b) {
double x, y, z;
cie1931WavelengthToXYZFit(wavelength, x, y, z);
double dr, dg, db;
srgbXYZ2RGB(x, y, z, dr, dg, db);
r = static_cast<unsigned char>(static_cast<int>(dr * 0xFF) & 0xFF);
g = static_cast<unsigned char>(static_cast<int>(dg * 0xFF) & 0xFF);
b = static_cast<unsigned char>(static_cast<int>(db * 0xFF) & 0xFF);
}
/**
* Convert XYZ to RGB in the sRGB color space
* <p>
* The conversion matrix and color component transfer function is taken from http://www.color.org/srgb.pdf, which
* follows the International Electrotechnical Commission standard IEC 61966-2-1 "Multimedia systems and equipment -
* Colour measurement and management - Part 2-1: Colour management - Default RGB colour space - sRGB"
*
* @param xyz XYZ values in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
* @return RGB values in a double array, in the order of R, G, B. each value in the range of [0.0, 1.0]
*/
static void srgbXYZ2RGB(double x, double y, double z, double& r, double& g, double& b) {
double rl = 3.2406255 * x + -1.537208 * y + -0.4986286 * z;
double gl = -0.9689307 * x + 1.8757561 * y + 0.0415175 * z;
double bl = 0.0557101 * x + -0.2040211 * y + 1.0569959 * z;
r = srgbXYZ2RGBPostprocess(rl);
g = srgbXYZ2RGBPostprocess(gl);
b = srgbXYZ2RGBPostprocess(bl);
}
/**
* helper function for {@link #srgbXYZ2RGB(double[])}
*/
static double srgbXYZ2RGBPostprocess(double c) {
// clip if c is out of range
c = c > 1 ? 1 : (c < 0 ? 0 : c);
// apply the color component transfer function
c = c <= 0.0031308 ? c * 12.92 : 1.055 * std::pow(c, 1. / 2.4) - 0.055;
return c;
}
/**
* A multi-lobe, piecewise Gaussian fit of CIE 1931 XYZ Color Matching Functions by Wyman el al. from Nvidia. The
* code here is adopted from the Listing 1 of the paper authored by Wyman et al.
* <p>
* Reference: Chris Wyman, Peter-Pike Sloan, and Peter Shirley, Simple Analytic Approximations to the CIE XYZ Color
* Matching Functions, Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 2, 1-11, 2013.
*
* @param wavelength wavelength in nm
* @return XYZ in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
*/
static void cie1931WavelengthToXYZFit(double wavelength, double& x, double& y, double& z) {
double wave = wavelength;
{
double t1 = (wave - 442.0) * ((wave < 442.0) ? 0.0624 : 0.0374);
double t2 = (wave - 599.8) * ((wave < 599.8) ? 0.0264 : 0.0323);
double t3 = (wave - 501.1) * ((wave < 501.1) ? 0.0490 : 0.0382);
x = 0.362 * std::exp(-0.5 * t1 * t1)
+ 1.056 * std::exp(-0.5 * t2 * t2)
- 0.065 * std::exp(-0.5 * t3 * t3);
}
{
double t1 = (wave - 568.8) * ((wave < 568.8) ? 0.0213 : 0.0247);
double t2 = (wave - 530.9) * ((wave < 530.9) ? 0.0613 : 0.0322);
y = 0.821 * std::exp(-0.5 * t1 * t1)
+ 0.286 * std::exp(-0.5 * t2 * t2);
}
{
double t1 = (wave - 437.0) * ((wave < 437.0) ? 0.0845 : 0.0278);
double t2 = (wave - 459.0) * ((wave < 459.0) ? 0.0385 : 0.0725);
z = 1.217 * std::exp(-0.5 * t1 * t1)
+ 0.681 * std::exp(-0.5 * t2 * t2);
}
}
};
} //namespace
#endif
This is bit cleaned up and tested C++11 version of @haochen-xie. I also added a function that converts value 0 to 1 to a wavelength in visible spectrum that is usable with this method. You can just put below in one header file and use it without any dependencies. This version will be maintained here.
#ifndef common_utils_OnlineStats_hpp
#define common_utils_OnlineStats_hpp
namespace common_utils {
class ColorUtils {
public:
static void valToRGB(double val0To1, unsigned char& r, unsigned char& g, unsigned char& b)
{
//actual visible spectrum is 375 to 725 but outside of 400-700 things become too dark
wavelengthToRGB(val0To1 * (700 - 400) + 400, r, g, b);
}
/**
* Convert a wavelength in the visible light spectrum to a RGB color value that is suitable to be displayed on a
* monitor
*
* @param wavelength wavelength in nm
* @return RGB color encoded in int. each color is represented with 8 bits and has a layout of
* 00000000RRRRRRRRGGGGGGGGBBBBBBBB where MSB is at the leftmost
*/
static void wavelengthToRGB(double wavelength, unsigned char& r, unsigned char& g, unsigned char& b) {
double x, y, z;
cie1931WavelengthToXYZFit(wavelength, x, y, z);
double dr, dg, db;
srgbXYZ2RGB(x, y, z, dr, dg, db);
r = static_cast<unsigned char>(static_cast<int>(dr * 0xFF) & 0xFF);
g = static_cast<unsigned char>(static_cast<int>(dg * 0xFF) & 0xFF);
b = static_cast<unsigned char>(static_cast<int>(db * 0xFF) & 0xFF);
}
/**
* Convert XYZ to RGB in the sRGB color space
* <p>
* The conversion matrix and color component transfer function is taken from http://www.color.org/srgb.pdf, which
* follows the International Electrotechnical Commission standard IEC 61966-2-1 "Multimedia systems and equipment -
* Colour measurement and management - Part 2-1: Colour management - Default RGB colour space - sRGB"
*
* @param xyz XYZ values in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
* @return RGB values in a double array, in the order of R, G, B. each value in the range of [0.0, 1.0]
*/
static void srgbXYZ2RGB(double x, double y, double z, double& r, double& g, double& b) {
double rl = 3.2406255 * x + -1.537208 * y + -0.4986286 * z;
double gl = -0.9689307 * x + 1.8757561 * y + 0.0415175 * z;
double bl = 0.0557101 * x + -0.2040211 * y + 1.0569959 * z;
r = srgbXYZ2RGBPostprocess(rl);
g = srgbXYZ2RGBPostprocess(gl);
b = srgbXYZ2RGBPostprocess(bl);
}
/**
* helper function for {@link #srgbXYZ2RGB(double[])}
*/
static double srgbXYZ2RGBPostprocess(double c) {
// clip if c is out of range
c = c > 1 ? 1 : (c < 0 ? 0 : c);
// apply the color component transfer function
c = c <= 0.0031308 ? c * 12.92 : 1.055 * std::pow(c, 1. / 2.4) - 0.055;
return c;
}
/**
* A multi-lobe, piecewise Gaussian fit of CIE 1931 XYZ Color Matching Functions by Wyman el al. from Nvidia. The
* code here is adopted from the Listing 1 of the paper authored by Wyman et al.
* <p>
* Reference: Chris Wyman, Peter-Pike Sloan, and Peter Shirley, Simple Analytic Approximations to the CIE XYZ Color
* Matching Functions, Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 2, 1-11, 2013.
*
* @param wavelength wavelength in nm
* @return XYZ in a double array in the order of X, Y, Z. each value in the range of [0.0, 1.0]
*/
static void cie1931WavelengthToXYZFit(double wavelength, double& x, double& y, double& z) {
double wave = wavelength;
{
double t1 = (wave - 442.0) * ((wave < 442.0) ? 0.0624 : 0.0374);
double t2 = (wave - 599.8) * ((wave < 599.8) ? 0.0264 : 0.0323);
double t3 = (wave - 501.1) * ((wave < 501.1) ? 0.0490 : 0.0382);
x = 0.362 * std::exp(-0.5 * t1 * t1)
+ 1.056 * std::exp(-0.5 * t2 * t2)
- 0.065 * std::exp(-0.5 * t3 * t3);
}
{
double t1 = (wave - 568.8) * ((wave < 568.8) ? 0.0213 : 0.0247);
double t2 = (wave - 530.9) * ((wave < 530.9) ? 0.0613 : 0.0322);
y = 0.821 * std::exp(-0.5 * t1 * t1)
+ 0.286 * std::exp(-0.5 * t2 * t2);
}
{
double t1 = (wave - 437.0) * ((wave < 437.0) ? 0.0845 : 0.0278);
double t2 = (wave - 459.0) * ((wave < 459.0) ? 0.0385 : 0.0725);
z = 1.217 * std::exp(-0.5 * t1 * t1)
+ 0.681 * std::exp(-0.5 * t2 * t2);
}
}
};
} //namespace
#endif
The plot of colors from 375nm to 725nm looks like below:
One issue with this method is the fact that it works only between 400-700nm and outside of that it sharply falls down to black. Another issue is narrower blue.
For comparison, below is the colors from Vision FAQ at maxmax.com:
I used this to visualize depth map where each pixel represents depth value in meters and this looks like below:
Method 2
This is implemented as part of bitmap_image single file header-only library by Aeash Partow:
inline rgb_t convert_wave_length_nm_to_rgb(const double wave_length_nm)
{
// Credits: Dan Bruton http://www.physics.sfasu.edu/astro/color.html
double red = 0.0;
double green = 0.0;
double blue = 0.0;
if ((380.0 <= wave_length_nm) && (wave_length_nm <= 439.0))
{
red = -(wave_length_nm - 440.0) / (440.0 - 380.0);
green = 0.0;
blue = 1.0;
}
else if ((440.0 <= wave_length_nm) && (wave_length_nm <= 489.0))
{
red = 0.0;
green = (wave_length_nm - 440.0) / (490.0 - 440.0);
blue = 1.0;
}
else if ((490.0 <= wave_length_nm) && (wave_length_nm <= 509.0))
{
red = 0.0;
green = 1.0;
blue = -(wave_length_nm - 510.0) / (510.0 - 490.0);
}
else if ((510.0 <= wave_length_nm) && (wave_length_nm <= 579.0))
{
red = (wave_length_nm - 510.0) / (580.0 - 510.0);
green = 1.0;
blue = 0.0;
}
else if ((580.0 <= wave_length_nm) && (wave_length_nm <= 644.0))
{
red = 1.0;
green = -(wave_length_nm - 645.0) / (645.0 - 580.0);
blue = 0.0;
}
else if ((645.0 <= wave_length_nm) && (wave_length_nm <= 780.0))
{
red = 1.0;
green = 0.0;
blue = 0.0;
}
double factor = 0.0;
if ((380.0 <= wave_length_nm) && (wave_length_nm <= 419.0))
factor = 0.3 + 0.7 * (wave_length_nm - 380.0) / (420.0 - 380.0);
else if ((420.0 <= wave_length_nm) && (wave_length_nm <= 700.0))
factor = 1.0;
else if ((701.0 <= wave_length_nm) && (wave_length_nm <= 780.0))
factor = 0.3 + 0.7 * (780.0 - wave_length_nm) / (780.0 - 700.0);
else
factor = 0.0;
rgb_t result;
const double gamma = 0.8;
const double intensity_max = 255.0;
#define round(d) std::floor(d + 0.5)
result.red = static_cast<unsigned char>((red == 0.0) ? red : round(intensity_max * std::pow(red * factor, gamma)));
result.green = static_cast<unsigned char>((green == 0.0) ? green : round(intensity_max * std::pow(green * factor, gamma)));
result.blue = static_cast<unsigned char>((blue == 0.0) ? blue : round(intensity_max * std::pow(blue * factor, gamma)));
#undef round
return result;
}
Plot of wavelength from 375-725nm looks like below:
So this is more usable in 400-725nm. When I visualize same depth map as in method 1, I get below. There is an obvious issue of those black lines which I think indicates minor bug in this code which I haven't looked more deeply. Also violets are bit narrower in this method which causes less contrast for far away objects.
发布评论
评论(8)
对于懒惰的人(像我一样),这里是 @user151323 的答案中找到的代码的 java 实现(也就是说,只是对 光谱实验室报告):
For lazy guys (like me), here is an implementation in java of the code found in @user151323 's answer (that is, just a simple translation from pascal code found in Spectra Lab Report):
总体思路:
步骤 1 和 2 可能会有所不同。
有多种颜色匹配函数,可作为表格或解析近似值(由@Tarc建议)和@谢浩辰)。如果您需要平滑精确的结果,表格是最好的选择。
不存在单一的 RGB 色彩空间。可以使用多个变换矩阵和不同类型的伽玛校正。
下面是我最近想出的 C# 代码。它在“CIE 1964 标准观察者”表和 sRGB 矩阵 + 伽玛校正上使用线性插值。
400-700 nm 范围的结果:
General idea:
Steps 1 and 2 may vary.
There are several color matching functions, available as tables or as analytic approximations (suggested by @Tarc and @Haochen Xie). Tables are best if you need a smooth preсise result.
There is no single RGB color space. Multiple transformation matrices and different kinds of gamma correction may be used.
Below is the C# code I came up with recently. It uses linear interpolation over the "CIE 1964 standard observer" table and sRGB matrix + gamma correction.
Result for the 400-700 nm range:
虽然这是一个老问题并且已经得到了一些好的答案,但当我尝试在我的应用程序中实现此类转换功能时,我对此处列出的算法并不满意,并进行了自己的研究,这给了我一些好的结果。所以我要发布一个新答案。
经过一些研究,我发现了这篇论文,简单分析近似
参考了CIE XYZ颜色匹配函数,并尝试在我的应用程序中采用引入的多瓣分段高斯拟合算法。论文只描述了将波长转换为相应XYZ值的函数,因此我实现了一个将XYZ转换为RGB的函数在 sRGB 色彩空间中并将它们组合起来。结果非常棒,值得分享:
我的代码是用 Java 8 编写的,但将其移植到较低版本的 Java 和其他语言应该不难。
Although this is an old question and already gets a handful good answers, when I tried to implement such conversion functionality in my application I was not satisfied with the algorithms already listed here and did my own research, which gave me some good result. So I'm going to post a new answer.
After some researchs I came across this paper, Simple Analytic Approximations
to the CIE XYZ Color Matching Functions, and tried to adopt the introduced multi-lobe piecewise Gaussian fit algorithm in my application. The paper only described the functions to convert a wavelength to the corresponding XYZ values, so I implemented a function to convert XYZ to RGB in the sRGB color space and combined them. The result is fantastic and worth sharing:
my code is written in Java 8, but it shouldn't be hard to port it to lower versions of Java and other languages.
您正在谈论从波长转换为RGB值。
看看这里,或许能解答你的疑问。您有一个实用程序可以使用源代码以及一些解释来执行此操作。
WaveLengthToRGB
You're talking about converting from wave length to an RGB value.
Look here, will probably answer your question. Thy have an utility for doing this with the source code as well as some explanation.
WaveLengthToRGB
我想我不妨用正式的答案来跟进我的评论。最好的选择是使用 HSV 色彩空间 - 尽管色调代表波长,不是一对一的比较。
I guess I might as well follow up my comment with a formal answer. The best option is to use the HSV colour space - though the hue represents the wavelength it is not a one-to-one comparison.
我对已知的色调值和频率进行了线性拟合(去掉红色和紫色,因为它们的频率值延伸得太远,以致于它们有点扭曲),我得到了一个粗略的转换方程。
它就像
频率(以太赫兹为单位)= 474 +(3/4)(色相角(以度为单位))
我试图环顾四周,看看是否有人提出了这个方程,但截至 2010 年 5 月我还没有找到任何东西。
I did a linear fit of known hue values and frequencies (dropping out red and violet because they extend so far in frequency values that they skew things a bit) and I got a rough conversion equation.
It goes like
frequency (in THz)=474+(3/4)(Hue Angle (in degrees))
I've tried to look around and see if anyone has come up with this equation, but I haven't found anything as of May 2010.
方法 1
这是对 @haochen-xie 的 C++11 版本进行了一些清理和测试。我还添加了一个函数,将 0 到 1 的值转换为可用于此方法的可见光谱中的波长。您可以将以下内容放入一个头文件中并在没有任何依赖项的情况下使用它。此版本将在此处维护。
从 375nm 到 725nm 的颜色图如下所示:
这种方法的一个问题是,它仅在 400-700nm 之间起作用,在该范围之外,它会急剧下降到黑色。另一个问题是蓝色较窄。
为了进行比较,以下是 maxmax.com 上的 Vision FAQ 中的颜色:
我用它来可视化深度图,其中每个像素代表以米为单位的深度值,如下所示:
方法 2
这是作为 bitmap_image Aeash Partow 的单文件头库:
375-725nm 的波长图如下所示:
所以这在 400-725nm 中更有用。当我可视化与方法 1 中相同的深度图时,我得到下面的结果。这些黑线有一个明显的问题,我认为这表明这段代码中存在小错误,我没有更深入地研究过。此外,这种方法中的紫罗兰色有点窄,这会导致远处物体的对比度较小。
Method 1
This is bit cleaned up and tested C++11 version of @haochen-xie. I also added a function that converts value 0 to 1 to a wavelength in visible spectrum that is usable with this method. You can just put below in one header file and use it without any dependencies. This version will be maintained here.
The plot of colors from 375nm to 725nm looks like below:
One issue with this method is the fact that it works only between 400-700nm and outside of that it sharply falls down to black. Another issue is narrower blue.
For comparison, below is the colors from Vision FAQ at maxmax.com:
I used this to visualize depth map where each pixel represents depth value in meters and this looks like below:
Method 2
This is implemented as part of bitmap_image single file header-only library by Aeash Partow:
Plot of wavelength from 375-725nm looks like below:
So this is more usable in 400-725nm. When I visualize same depth map as in method 1, I get below. There is an obvious issue of those black lines which I think indicates minor bug in this code which I haven't looked more deeply. Also violets are bit narrower in this method which causes less contrast for far away objects.
将波长的 CIExy 朝向 D65 白色投影到 sRGB 色域上
Project the wavelength's CIExy towards the D65 white onto the sRGB gamut