此HSL-> RGB转换有什么问题?

时间:2018-11-25 19:00:11

标签: hsl

我有一个HueSaturation类,它可以修复色相和饱和度并允许不同的发光度。

我已经从Wikipedia实现了从HSL到RGB的转换,但是结果与其他地方的参考值并不完全相同。

所有参数h,s和l都在[0..1]范围内。

HueSaturation::RGB
HueSaturation::rgb (double lum)
const
{
    if (false == (0 <= lum && lum <= 1))
        throw std::invalid_argument (__PRETTY_FUNCTION__);

    const double C = (1 - abs (2 * lum - 1)) * m_sat;

    const double H = m_hue * 6; // NOTE: 0 <= m_hue <= 1, not [0..360]

    const double X = C * (1 - abs (fmod (H, 2) - 1));

    double R1 = 0;
    double G1 = 0;
    double B1 = 0;

    switch (int (H))
    {
    case 6:
        // Overlap.
        if (1 == m_hue)
            [[fallthrough]];
        else
            throw std::logic_error (__PRETTY_FUNCTION__);
    case 0:
        R1 = C;
        G1 = X;
        break;

    case 1:
        R1 = X;
        G1 = C;
        break;

    case 2:
        G1 = C;
        B1 = X;
        break;

    case 3:
        G1 = X;
        B1 = C;
        break;

    case 4:
        R1 = X;
        B1 = C;
        break;

    case 5:
        R1 = C;
        B1 = X;
        break;

    default:
        throw std::logic_error (__PRETTY_FUNCTION__);
    };

    const double m = lum - C / 2;

    return {R1 + m, G1 + m, B1 + m};
}

例如,HSL[0.8,0.2,0.4](或[208,20,40](以度/百分比为单位))映射到RGB[114,81,122],而其他软件告诉我这应该是RGB[82,103,122]。关闭,但不太完全。

我的错误在哪里?

0 个答案:

没有答案