简体   繁体   English

HSL颜色无法正常工作

[英]HSL Color not working right

Im not 100% shore what im doing wrong. 我不是100%支持我做错了什么。 Im working on adding HSL Color to my custom engine, and my test program is creating the wrong colour spectrum Screen Shot: Screenshot of Program Output 我正在努力将HSL颜色添加到自定义引擎中,并且我的测试程序正在创建错误的色谱图屏幕快照: 程序输出的屏幕截图

Source Code: 源代码:

public Color toRGB()
{
    float r = 0.0f;
    float g = 0.0f;
    float b = 0.0f;

    float v1, v2;

    if(_sat == 0.0f)
    {
        r = _lum;
        g = _lum;
        b = _lum;
    }
    else
    {
        if (_lum < 0.5f)
            v2 = _lum * (1.0f + _sat);
        else
            v2 = (_lum + _sat) - (_lum * _sat);

        v1 = 2.0f * _lum - v2;

        r = GetElement(v1, v2, (_hue / 360.0f) + (1.0f / 3.0f));
        g = GetElement(v1, v2, (_hue / 360.0f));
        b = GetElement(v1, v2, (_hue / 360.0f) - (1.0f / 3.0f));
    }
    return new Color(r, g, b);
}

private static float GetElement(float v1, float v2, float vH)
{
    if (vH < 0.0f)
        vH += 1.0f;
    else if (vH > 1.0f)
        vH -= 1.0f;

    if ((6.0f * vH) < 1.0f)
    return (v1 + (v2 - v1) * 6.0f * vH);
    if ((2.0f * vH) < 1.0f)
        return v2;
    if ((3.0f * vH) < 2.0f)
        return (v1 + (v2 - v1) * 6.0f * ((1.0f / 3.0f) - vH));
    return v1;
}

Code is exicuted in sample program as follows: 示例程序中的代码如下:

float width = GraphicsDevice.Viewport.Width;
float height = GraphicsDevice.Viewport.Height;

spriteBatch.Begin();
for (float x = 0.0f; x < width; x++)
{
    for (float y = 0.0f; y < height; y++)
    {
        HSLColor temp = new HSLColor(((x / width) * 360.0f), 0.5f, (y / height));
        DrawPixel(spriteBatch, (int)x, (int)y, temp.toRGB());
    }
}

Just a couple of notes: Im using Microsofts XNA Famework. 请注意以下几点:我使用Microsoft的XNA Famework。 I am not interested in using other libraries, and even XNA may only be tempoary depending on which way this engine develops. 我对使用其他库不感兴趣,甚至XNA可能只是暂时的,具体取决于此引擎的开发方式。

Here's what I worked out for GetElement : 这是我为GetElement

private static float GetElement(float v1, float v2, float vH)
{
    if (vH < 0.0f)
        vH += 1.0f;
    else if (vH > 1.0f)
        vH -= 1.0f;

    if (6.0f * vH < 1.0f) 
        return v1 + (v2 - v1) * 6.0f * vH;
    if (3.0f * vH < 1.0f)
        return v2;
    if (2.0f * vH < 1.0f)
        return v1 + (v2 - v1) * 6.0f * (0.5f - vH);
    return v1;
}

I haven't tested it, but it looks good to me. 我还没有测试过,但是对我来说看起来不错。 In any case, you'll notice that the first two if s are identical to yours. 无论如何,您会注意到前两个if与您相同。 The errors are definitely in the third if . 错误肯定是在第三个if

And this is just a nitpick, but I would improve the precision of the hue parameters slightly like so: 这只是一个“ nitpick”,但我会稍微提高色相参数的精度,如下所示:

    r = GetElement(v1, v2, (_hue + 120.0f) / 360.0f);
    g = GetElement(v1, v2, _hue / 360.0f);
    b = GetElement(v1, v2, (_hue - 120.0f) / 360.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM