2016-01-09 63 views
0

我不是100%支持什麼我做錯了。 林努力增加HSL顏色我的定義引擎,我的測試程序創建了錯誤的色彩頻譜 屏幕截圖: Screenshot of Program OutputHSL顏色不正常

源代碼:

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

代碼在示例程序exicuted如下:

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

只是一些筆記:我使用微軟XNA Famework。 我對使用其他庫不感興趣,甚至XNA可能只是根據這個引擎開發的方式而定製。

+0

歡迎SO。知道你期望發生的事情會很有幫助。 – m00am

回答

0

這是我制定了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; 
} 

我沒有測試它,但它看起來不錯。無論如何,你會注意到前兩個if與你的相同。錯誤絕對在第三個if

而這僅僅是一個挑剔,但我會稍微改善色調參數的精度,像這樣:

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