2013-06-13 52 views
2

我正在为我的游戏框架写一个HSVtoRGB方法,并且在通过色调时,发生这种情况 - >http://youtu.be/ACBwR_0iMWEHSV到RGB停在黄色C#

这是代码。

public static Color HSVtoRGB(float hue, float saturation, float value, float alpha) 
    { 
     if(hue > 1 || saturation > 1 || value > 1) throw new Exception("values cannot be more than 1!"); 
     if (hue < 0 || saturation < 0|| value < 0) throw new Exception("values cannot be less than 0!"); 

     Color output = new Color(); 
     if (Math.Abs(saturation) < 0.001) 
     { 
      output.R = (byte) (value*byte.MaxValue); 
      output.G = (byte) (value*byte.MaxValue); 
      output.B = (byte) (value*byte.MaxValue); 
     } 
     else 
     { 
      hue = hue/60f; 
      float f = hue - (int)hue; 
      float p = value*(1f - saturation); 
      float q = value*(1f - saturation*f); 
      float t = value*(1f - saturation*(1f - f)); 
      switch ((int)hue) 
      { 
       case (0) : 
        output = new Color(value * 255, t * 255, p * 255, alpha); 
        break; 
       case (1): 
        output = new Color(q * 255, value * 255, p * 255, alpha); 
        break; 
       case (2): 
        output = new Color(p * 255, value * 255, t * 255, alpha); 
        break; 
       case (3): 
        output = new Color(p * 255, q * 255, value * 255, alpha); 
        break; 
       case (4): 
        output = new Color(t * 255, p * 255, value * 255, alpha); 
        break; 
       case (5): 
        output = new Color(value * 255, p * 255, q * 255, alpha); 
        break; 
       default : 
        throw new Exception("RGB color unknown!"); 
      } 

     } 
     return output; 
    } 

当添加.001f的色调,它导致它从红色到黄色去,但随后在黄枝,直到它回滚到0。请注意,我用的Microsoft.Xna.Framework.ColorSystem.Drawing.Color

作为参考,这里是基于我试图复制的Flixel Power Tools的HSVtoRGB方法。

 public static function HSVtoRGB(h:Number, s:Number, v:Number, alpha:uint = 255):uint 
    { 
     var result:uint; 

     if (s == 0.0) 
     { 
      result = getColor32(alpha, v * 255, v * 255, v * 255); 
     } 
     else 
     { 
      h = h/60.0; 
      var f:Number = h - int(h); 
      var p:Number = v * (1.0 - s); 
      var q:Number = v * (1.0 - s * f); 
      var t:Number = v * (1.0 - s * (1.0 - f)); 

      switch (int(h)) 
      { 
       case 0: 
        result = getColor32(alpha, v * 255, t * 255, p * 255); 
        break; 

       case 1: 
        result = getColor32(alpha, q * 255, v * 255, p * 255); 
        break; 

       case 2: 
        result = getColor32(alpha, p * 255, v * 255, t * 255); 
        break; 

       case 3: 
        result = getColor32(alpha, p * 255, q * 255, v * 255); 
        break; 

       case 4: 
        result = getColor32(alpha, t * 255, p * 255, v * 255); 
        break; 

       case 5: 
        result = getColor32(alpha, v * 255, p * 255, q * 255); 
        break; 

       default: 
        FlxG.log("FlxColor Error: HSVtoRGB : Unknown color"); 
      } 
     } 

     return result; 
    } 

回答

2

我写了我自己的HSV-> RGB转换器基于你的代码...

(和你的链接到http://www.easyrgb.com/index.php?X=MATH&H=21#text21

的代码是:

public static Color HSVtoRGB(float hue, float saturation, float value, float alpha) 
    { 
     while (hue > 1f) { hue -= 1f; } 
     while (hue < 0f) { hue += 1f; } 
     while (saturation > 1f) { saturation -= 1f; } 
     while (saturation < 0f) { saturation += 1f; } 
     while (value > 1f) { value -= 1f; } 
     while (value < 0f) { value += 1f; } 
     if (hue > 0.999f) { hue = 0.999f; } 
     if (hue < 0.001f) { hue = 0.001f; } 
     if (saturation > 0.999f) { saturation = 0.999f; } 
     if (saturation < 0.001f) { return new Color(value * 255f, value * 255f, value * 255f); } 
     if (value > 0.999f) { value = 0.999f; } 
     if (value < 0.001f) { value = 0.001f; } 

     float h6 = hue * 6f; 
     if (h6 == 6f) { h6 = 0f; } 
     int ihue = (int)(h6); 
     float p = value * (1f - saturation); 
     float q = value * (1f - (saturation * (h6 - (float)ihue))); 
     float t = value * (1f - (saturation * (1f - (h6 - (float)ihue)))); 
     switch (ihue) 
     { 
      case 0: 
       return new Color(value, t, p, alpha); 
      case 1: 
       return new Color(q, value, p, alpha); 
      case 2: 
       return new Color(p, value, t, alpha); 
      case 3: 
       return new Color(p, q, value, alpha); 
      case 4: 
       return new Color(t, p, value, alpha); 
      default: 
       return new Color(value, p, q, alpha); 
     } 
    } 

有了它,我提出这个: Screenshot

值得注意的几件事:

  • 新的颜色(......)获取输入从0到1,而不是0到255
  • 确保整数之间的差异,浮标始终是明确的
  • 每次不要抛出异常的情况并不完美。尽可能地适应。
  • 当你从其他程序借用代码,也是一个完美的数学参考...尝试在借用代码之前复制数学网站 - 谁知道从适当的数学中进行了哪些奇怪的情况特定的修改?
+0

谢谢!我最终找到了一个不那么复杂的算法,在这里 - > http://wiki.beyondunreal.com/HSV-RGB_Conversion。谢谢,但我让你回答正确的。 – redcodefinal

+0

呵呵,'Microsoft.Xna.Framework。Color可以接受一个浮点数或一个字节,但是它将每个值存储为一个字节,所以在所有的实际中,它只是将浮点数乘以255。 – redcodefinal

2

在色相被限制为0.0f的方法的开始.. 1.0F 然后它被60F divied所以它现在是在范围0.0F .. 1/60.0f

然后你正在打开(int)色调,它将始终为零。

我不认为您的交换机的其他分支将永远执行。

+0

这就是Flixel Power Tools的家伙如何使用HSLtoRGB方法。老实说,我不是很确定这件事真的有效。 :(我试着完全像这篇文章http://www.easyrgb.com/index.php?X=MATH&H=21#text21,但它根本不起作用,用这种方法,它至少会经过红色 - >黄色没有问题 – redcodefinal

+0

我用HSVtoRGB方法更新了问题,我正在复制 – redcodefinal

+0

他们不知道我应该做什么每个HSV-RGB方法都是这样工作的。 .. – redcodefinal