【问题标题】:Convert light frequency to RGB?将光频率转换为RGB?
【发布时间】:2010-12-01 03:30:29
【问题描述】:

有人知道将光频率转换为 RGB 值的公式吗?

【问题讨论】:

标签: algorithm language-agnostic rgb formula approximation


【解决方案1】:

这里是整个转换过程的详细解释:http://www.fourmilab.ch/documents/specrend/。包含源代码!

【讨论】:

  • Fourmilab 的文章强调了一些颜色不能用 RGB 表示(亮橙色就是一个很好的例子),因为你不能通过将三种原色加在一起来“制造”任意颜色的光,无论如何我们的物理老师可能告诉过我们(我的很好)。太糟糕了,但实际上通常不会致命。
  • 除此之外:en.wikipedia.org/wiki/Srgb 这篇文章是在 sRGB 标准被广泛采用之前写的。另请注意“计算假定使用 2° 标准色度观察者”短语,这意味着应使用论文随附来源中的 CIE 1931 表,而不是 CIE 1964。
  • 如果能提供一些如何使用代码的例子就好了。它需要函数作为参数,使用温度来计算颜色等。人们会很高兴知道要删除和更改什么以使其正常工作。
  • 值得注意的是,只有一小部分可能的可见波长可以在 RGB 颜色空间中精确表示。转换过程非常复杂和模棱两可。见physics.stackexchange.com/a/94446/5089physics.stackexchange.com/a/419628/5089
【解决方案2】:

对于懒惰的人(比如我),这里是在@user151323 的答案中找到的代码的java 实现(也就是说,只是从Spectra Lab Report 中找到的pascal 代码的简单翻译):

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;
}

【讨论】:

  • 您的代码中似乎存在错误。例如,如果波长为 439.5,您的函数将返回黑色。我相信,网站上的原始代码使用的是整数(我根本不知道帕斯卡)。我建议将Wavelength&lt;=439 更改为Wavelength&lt;440
  • 你是对的!感谢您向我指出这一点 :) 已更正。
  • 是否预计对某些频率有重复 RFB? (红色): 652 - RGB(255, 0, 0) | 660 - RGB(255, 0, 0) | 692 - RGB(255, 0, 0) | 700 - RGB(255, 0, 0) | ...
【解决方案3】:

总体思路:

  1. 使用CEI color matching functions 将波长转换为XYZ color
  2. 将 XYZ 转换为 RGB
  3. 将组件剪裁到 [0..1] 并乘以 255 以适应无符号字节范围。

第 1 步和第 2 步可能会有所不同。

有几个颜色匹配函数,可用作tables 或解析近似值(@Tarc 和@Haochen Xie 建议)。如果您需要平滑精确的结果,最好使用表格。

没有单一的 RGB 颜色空间。 Multiple transformation matrices 和不同种类的伽马校正可以使用。

下面是我最近想出的 C# 代码。它在“CIE 1964 标准观察者”表和sRGB matrix + gamma correction 上使用线性插值。

static class RgbCalculator {

    const int
         LEN_MIN = 380,
         LEN_MAX = 780,
         LEN_STEP = 5;

    static readonly double[]
        X = {
                0.000160, 0.000662, 0.002362, 0.007242, 0.019110, 0.043400, 0.084736, 0.140638, 0.204492, 0.264737,
                0.314679, 0.357719, 0.383734, 0.386726, 0.370702, 0.342957, 0.302273, 0.254085, 0.195618, 0.132349,
                0.080507, 0.041072, 0.016172, 0.005132, 0.003816, 0.015444, 0.037465, 0.071358, 0.117749, 0.172953,
                0.236491, 0.304213, 0.376772, 0.451584, 0.529826, 0.616053, 0.705224, 0.793832, 0.878655, 0.951162,
                1.014160, 1.074300, 1.118520, 1.134300, 1.123990, 1.089100, 1.030480, 0.950740, 0.856297, 0.754930,
                0.647467, 0.535110, 0.431567, 0.343690, 0.268329, 0.204300, 0.152568, 0.112210, 0.081261, 0.057930,
                0.040851, 0.028623, 0.019941, 0.013842, 0.009577, 0.006605, 0.004553, 0.003145, 0.002175, 0.001506,
                0.001045, 0.000727, 0.000508, 0.000356, 0.000251, 0.000178, 0.000126, 0.000090, 0.000065, 0.000046,
                0.000033
            },

        Y = {
                0.000017, 0.000072, 0.000253, 0.000769, 0.002004, 0.004509, 0.008756, 0.014456, 0.021391, 0.029497,
                0.038676, 0.049602, 0.062077, 0.074704, 0.089456, 0.106256, 0.128201, 0.152761, 0.185190, 0.219940,
                0.253589, 0.297665, 0.339133, 0.395379, 0.460777, 0.531360, 0.606741, 0.685660, 0.761757, 0.823330,
                0.875211, 0.923810, 0.961988, 0.982200, 0.991761, 0.999110, 0.997340, 0.982380, 0.955552, 0.915175,
                0.868934, 0.825623, 0.777405, 0.720353, 0.658341, 0.593878, 0.527963, 0.461834, 0.398057, 0.339554,
                0.283493, 0.228254, 0.179828, 0.140211, 0.107633, 0.081187, 0.060281, 0.044096, 0.031800, 0.022602,
                0.015905, 0.011130, 0.007749, 0.005375, 0.003718, 0.002565, 0.001768, 0.001222, 0.000846, 0.000586,
                0.000407, 0.000284, 0.000199, 0.000140, 0.000098, 0.000070, 0.000050, 0.000036, 0.000025, 0.000018,
                0.000013
            },

        Z = {
                0.000705, 0.002928, 0.010482, 0.032344, 0.086011, 0.197120, 0.389366, 0.656760, 0.972542, 1.282500,
                1.553480, 1.798500, 1.967280, 2.027300, 1.994800, 1.900700, 1.745370, 1.554900, 1.317560, 1.030200,
                0.772125, 0.570060, 0.415254, 0.302356, 0.218502, 0.159249, 0.112044, 0.082248, 0.060709, 0.043050,
                0.030451, 0.020584, 0.013676, 0.007918, 0.003988, 0.001091, 0.000000, 0.000000, 0.000000, 0.000000,
                0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
                0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
                0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
                0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
                0.000000
            };

    static readonly double[]
        MATRIX_SRGB_D65 = {
             3.2404542, -1.5371385, -0.4985314,
            -0.9692660,  1.8760108,  0.0415560,
             0.0556434, -0.2040259,  1.0572252
        };

    public static byte[] Calc(double len) {
        if(len < LEN_MIN || len > LEN_MAX)
            return new byte[3];

        len -= LEN_MIN;
        var index = (int)Math.Floor(len / LEN_STEP);
        var offset = len - LEN_STEP * index;

        var x = Interpolate(X, index, offset);
        var y = Interpolate(Y, index, offset);
        var z = Interpolate(Z, index, offset);

        var m = MATRIX_SRGB_D65;

        var r = m[0] * x + m[1] * y + m[2] * z;
        var g = m[3] * x + m[4] * y + m[5] * z;
        var b = m[6] * x + m[7] * y + m[8] * z;

        r = Clip(GammaCorrect_sRGB(r));
        g = Clip(GammaCorrect_sRGB(g));
        b = Clip(GammaCorrect_sRGB(b));

        return new[] { 
            (byte)(255 * r),
            (byte)(255 * g),
            (byte)(255 * b)
        };
    }

    static double Interpolate(double[] values, int index, double offset) {
        if(offset == 0)
            return values[index];

        var x0 = index * LEN_STEP;
        var x1 = x0 + LEN_STEP;
        var y0 = values[index];
        var y1 = values[1 + index];

        return y0 + offset * (y1 - y0) / (x1 - x0);
    }

    static double GammaCorrect_sRGB(double c) {
        if(c <= 0.0031308)
            return 12.92 * c;

        var a = 0.055;
        return (1 + a) * Math.Pow(c, 1 / 2.4) - a;
    }

    static double Clip(double c) {
        if(c < 0)
            return 0;
        if(c > 1)
            return 1;
        return c;
    }
}

400-700 nm 范围的结果:

【讨论】:

  • 这对我来说真的很有趣。我有一个想法,使用这样的东西来给出正常的响应,但使用 WXYZ 响应来模拟具有第四个视锥的四色视者的响应,该视锥响应的频率与其他正常的三种视锥中的任何一种都足够远。这可能让我获取源图像并推断他们看到的差异。注:他们看不到新的颜色,而是混合的灯光,(总和),例如,对于我们大多数人来说,特定的黄色似乎与特定频率的黄色相同,但对他们来说,灯光不会混合到完全是黄色的。
  • 当然,对于特定的 RGB 颜色,可以通过多种方式获得。叶子的绿色可能来自过滤掉除绿色之外的所有东西,或者绿色可能已被过滤掉,但纳米特性可能导致蓝色和黄色反射并看起来与绿色相同。给定图像而不是光线,有什么方法可以区分吗?
【解决方案4】:

虽然这是一个老问题,并且已经得到了一些很好的答案,但当我尝试在我的应用程序中实现这种转换功能时,我对这里已经列出的算法并不满意,我自己进行了研究,这给了我一些很好的结果.所以我要发布一个新的答案。

经过一些研究,我发现了这篇论文,Simple Analytic Approximations to the CIE XYZ Color Matching Functions,并尝试在我的应用程序中采用引入的多叶分段高斯拟合算法。论文中只描述了将一个波长转换为对应的XYZ values的函数,所以我在sRGB色彩空间中实现了一个将XYZ转换为RGB的函数并将它们组合起来。结果非常棒,值得分享:

/**
 * 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 和其他语言应该不难。

【讨论】:

  • @Baddack,你是对的:这只是对计算值进行进一步转换的一种奇特方式。我不记得确切,但我认为它首先应用伽马校正,然后切断超出范围的值。也许我应该用单独的方法完成它,但我实际上并没有考虑在编写代码时共享代码,而且这是一个我需要这种转换的玩具项目。
  • @Baddack 我挖出了我需要这种转换的项目,并在不使用 java 8 lambda 的情况下重写了这部分,因此代码更清晰。我实际上记错了transferDoubleUnaryOperator 在做什么(因此我之前评论中的解释不正确),所以请检查新代码。
  • @Baddack 我很高兴代码对您有所帮助。如果您不介意,能否请您投票,这样它可能会帮助更多人?
  • @Baddack Math.pow(c, 1. / 2.4) = c^(1/2.4),即提高 c 的 1/2.4 次方; 1. 只是 1 但类型将是 double 而不是 int
  • @Ruslan 由于该算法是 CIE 标准观察者的分析拟合(可以被认为是“精确”模型),因此存在错误。但是从论文中,如果您查看第 7 页的图 1(比较 (d) 和 (f)),这种方法提供了非常接近的近似值。特别是如果您查看 (f),您会发现即使在标准模型中也有一条蓝线。另外,纯光源对颜色的感知因人而异,所以这种程度的误差可能可以忽略不计。
【解决方案5】:

您说的是从 波长 转换为 RGB 值。

看这里,可能会回答你的问题。你有一个实用程序可以用源代码和一些解释来做到这一点。

WaveLengthToRGB

【讨论】:

  • 只是阅读同一个页面“波长和 RGB 值之间没有唯一的一对一映射”——那么您就被查找表和启发式算法困住了。作为第一次剪辑,我将研究 HSV 到 RGB 的转换,因为“色调”的范围从蓝色到红色。可能会有轻微的偏移,因为在 RGB 域中,红色+蓝色 = 紫色,而紫色的可见波长最短。
  • 实际上不一样吗?频率 = c / 波长
  • @Mauricio Scheffer 是的,完全一样。
  • 这个 Bruton 的算法比较美观而不是现实
  • @ Joseph Gordon - 强烈反对。考虑在空气中发出的 400nm 的绿色射线撞击水面,然后在水中传播。水的折射系数是 1.33,所以现在水中的光线波长是 300nm,这显然不会改变它的颜色。使光线“着色”的物质是频率,而不是波长。在相同的物质(真空、空气、水)中,频率(颜色)映射到相同的波长。在不同的媒体中 - 不是。
【解决方案6】:

我想我不妨用一个正式的答案来跟进我的评论。最好的选择是使用HSV colour space - 虽然色调代表波长,但它不是一对一的比较。

【讨论】:

  • 您的链接已失效。
【解决方案7】:

我对已知的色调值和频率进行了线性拟合(去掉了红色和紫色,因为它们的频率值延伸得太远以至于它们有点歪斜),我得到了一个粗略的转换方程。

好像
频率(THz)=474+(3/4)(色相角(度))

我试图环顾四周,看看是否有人提出了这个等式,但截至 2010 年 5 月我还没有找到任何东西。

【讨论】:

    【解决方案8】:

    方法一

    这是对@haochen-xie 的 C++11 版本进行了一点清理和测试。我还添加了一个函数,将值 0 转换为 1 为可用于此方法的可见光谱波长。您可以将其放在一个头文件中并在没有任何依赖关系的情况下使用它。此版本将被维护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
    

    从 375nm 到 725nm 的颜色图如下所示:

    这种方法的一个问题是它只能在 400-700nm 之间工作,在此范围之外它会急剧下降到黑色。另一个问题是较窄的蓝色。

    为了比较,下面是来自 maxmax.com 的 Vision FAQ 的颜色:

    我用它来可视化深度图,其中每个像素代表以米为单位的深度值,如下所示:

    方法二

    这是由 Aeash Partow 作为 bitmap_image 单文件头库的一部分实现的:

    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;
    }
    

    375-725nm 的波长图如下所示:

    所以这在 400-725nm 中更有用。当我可视化与方法 1 中相同的深度图时,我得到了以下信息。这些黑线有一个明显的问题,我认为这表明该代码中的小错误,我没有深入研究。此外,这种方法中的紫罗兰色有点窄,导致远处物体的对比度较低。

    【讨论】:

      【解决方案9】:

      将波长的 CIExy 向 D65 白色投射到 sRGB 色域上

      #!/usr/bin/ghci
      ångstrømsfromTHz terahertz = 2997924.58 / terahertz
      tristimulusXYZfromÅngstrøms å=map(sum.map(stimulus))[
       [[1056,5998,379,310],[362,4420,160,267],[-65,5011,204,262]],
       [[821,5688,469,405],[286,5309,163,311]],
       [[1217,4370,118,360],[681,4590,260,138]]]
       where stimulus[ω,μ,ς,σ]=ω/1000*exp(-((å-μ)/if å<μ then ς else σ)^2/2)
      
      standardRGBfromTristimulusXYZ xyz=
       map(gamma.sum.zipWith(*)(gamutConfine xyz))[
       [3.2406,-1.5372,-0.4986],[-0.9689,1.8758,0.0415],[0.0557,-0.2040,1.057]]
      gamma u=if u<=0.0031308 then 12.92*u else (u**(5/12)*211-11)/200
      [red,green,blue,black]=
       [[0.64,0.33],[0.3,0.6],[0.15,0.06],[0.3127,0.3290,0]]
      ciexyYfromXYZ xyz=if xyz!!1==0 then black else map(/sum xyz)xyz
      cieXYZfromxyY[x,y,l]=if y==0 then black else[x*l/y,l,(1-x-y)*l/y]
      gamutConfine xyz=last$xyz:[cieXYZfromxyY[x0+t*(x1-x0),y0+t*(y1-y0),xyz!!1]|
       x0:y0:_<-[black],x1:y1:_<-[ciexyYfromXYZ xyz],i<-[0..2],
       [x2,y2]:[x3,y3]:_<-[drop i[red,green,blue,red]],
       det<-[(x0-x1)*(y2-y3)-(y0-y1)*(x2-x3)],
       t <-[((x0-x2)*(y2-y3)-(y0-y2)*(x2-x3))/det|det/=0],0<=t,t<=1]
      
      sRGBfromÅ=standardRGBfromTristimulusXYZ.tristimulusXYZfromÅngstrøms
      x s rgb=concat["\ESC[48;2;",
                     intercalate";"$map(show.(17*).round.(15*).max 0.min 1)rgb,
                     "m",s,"\ESC[49m"]
      spectrum=concatMap(x" ".sRGBfromÅ)$takeWhile(<7000)$iterate(+60)4000
      main=putStrLn spectrum
      

      【讨论】:

        猜你喜欢
        • 2012-08-27
        • 1970-01-01
        • 2017-07-04
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        相关资源
        最近更新 更多