如何转换指定 波长 到 RGB 颜色?

//指定波长转换成RGBA颜色
std::vector<int> lambdaToColor(double lambda,double gamma = 0.8,double intensityMax = 255.0)
{
    double r, g, b, alpha;
    if (lambda >= 380.0 && lambda < 440.0) {
        r = -1.0 * (lambda - 440.0) / (440.0 - 380.0);
        g = 0.0;
        b = 1.0;
    }else if (lambda >= 440.0 && lambda < 490.0) {
        r = 0.0;
        g = (lambda - 440.0) / (490.0 - 440.0);
        b = 1.0;
    }else if (lambda >= 490.0 && lambda < 510.0) {
        r = 0.0;
        g = 1.0;
        b = -1.0 * (lambda - 510.0) / (510.0 - 490.0);
    }else if (lambda >= 510.0 && lambda < 580.0) {
        r = (lambda - 510.0) / (580.0 - 510.0);
        g = 1.0;
        b = 0.0;
    }else if (lambda >= 580.0 && lambda < 645.0) {
        r = 1.0;
        g = -1.0 * (lambda - 645.0) / (645.0 - 580.0);
        b = 0.0;
    }else if (lambda >= 645.0 && lambda <= 780.0) {
        r = 1.0;
        g = 0.0;
        b = 0.0;
    }else {
        r = 0.0;
        g = 0.0;
        b = 0.0;
    }

    //在可见光谱的边缘处强度较低。
    if (lambda >= 380.0 && lambda < 420.0) {
        alpha = 0.30 + 0.70 * (lambda - 380.0) / (420.0 - 380.0);
    }else if (lambda >= 420.0 && lambda < 701.0) {
        alpha = 1.0;
    }else if (lambda >= 701.0 && lambda < 780.0) {
        alpha = 0.30 + 0.70 * (780.0 - lambda) / (780.0 - 700.0);
    }else {
        alpha = 0.0;
    }

    //1953年在引入NTSC电视时,计算具有荧光体的监视器的亮度公式如下
    int Y = static_cast<int>(0.212671*r + 0.715160*g + 0.072169*b);

    //伽马射线 gamma
    //照明强度 intensityMax
    int R = r == 0.0 ? 0 : static_cast<int>(std::round(intensityMax * std::pow(r * alpha, gamma)));
    int G = g == 0.0 ? 0 : static_cast<int>(std::round(intensityMax * std::pow(g * alpha, gamma)));
    int B = b == 0.0 ? 0 : static_cast<int>(std::round(intensityMax * std::pow(b * alpha, gamma)));
    int A = static_cast<int>(alpha);

    return std::vector<int>{R, G, B, A, Y};
}
posted @ 2018-06-14 09:20  學海無涯  阅读(3029)  评论(0编辑  收藏  举报