HSV到RGB停止在黄色C#

HSV到RGB停止在黄色C#

本文介绍了HSV到RGB停止在黄色C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个HSVtoRGB方法为我的游戏框架,并通过色彩去的时候,出现这种情况 - > HTTP:/ /youtu.be/ACBwR_0iMWE

下面是code。

 公共静态颜色HSVtoRGB(浮动色调,饱和度浮动,浮点值,浮阿尔法)
    {
        如果(色调→1 ||饱和度大于1 ||值大于1)抛出新的异常(值不能超过1个!);
        如果(色调℃,||饱和℃的||值小于0)抛出新的异常(值不能小于0!);

        彩色输出=新颜色();
        如果(Math.Abs​​(饱和)&所述0.001)
        {
            output.R =(字节)(值* byte.MaxValue);
            output.G =(字节)(值* byte.MaxValue);
            output.B =(字节)(值* byte.MaxValue);
        }
        其他
        {
            色相=色相/ 60F;
            浮动F =色调 - (INT)的色相;
            浮动P =值*(1F  - 饱和度);
            浮q =值*(1F  - 饱和* F);
            浮动T =值*(1F  - 饱和度*(1F  -  F));
            开关((int)的色调)
            {
                情况下(0):
                    输出=新颜色(值* 255,T * 255,P * 255,阿尔法);
                    打破;
                情况1):
                    输出=新颜色(Q * 255,值* 255,P * 255,阿尔法);
                    打破;
                壳体(2):
                    输出=新颜色(P * 255,值* 255,T * 255,阿尔法);
                    打破;
                情况下(3):
                    输出=新颜色(P * 255,Q * 255,值* 255,阿尔法);
                    打破;
                壳体(4):
                    输出=新的色彩(T * 255,P * 255,值* 255,阿尔法);
                    打破;
                壳体(5):
                    输出=新颜色(值* 255,P * 255,Q * 255,阿尔法);
                    打破;
                默认 :
                    抛出新的异常(RGB色彩不明!);
            }

        }
        返回输出;
    }
 

在加入 .001f 的色调,这导致它去从红色到黄色,但随后执着在黄色的,直到它回滚至0。请注意,我使用 Microsoft.Xna.Framework.Color 不是的System.Drawing.Color

有关引用,这里是 Flixel电动工具中的HSVtoRGB方法,基本上是我我试图复制。

 公共静态函数HSVtoRGB(H:编号,S:号码,V:数字,字母:UINT = 255):UINT
    {
        VAR结果:UINT;

        如果(S == 0.0)
        {
            结果= getColor32(α,V * 255,V * 255,V * 255);
        }
        其他
        {
            H = H / 60.0;
            变种F:数= H  -  INT(H);
            VAR号码:号码= V *(1.0  -  S);
            VAR问:数= V *(1.0  -  S * F);
            VAR参数t:Number = V *(1.0  -  S *(1.0  -  F));

            开关(INT(h)条)
            {
                情况下0:
                    结果= getColor32(α,V * 255,吨* 255,P * 255);
                    打破;

                情况1:
                    结果= getColor32(α,Q * 255,V * 255,第* 255);
                    打破;

                案例2:
                    结果= getColor32(α,P * 255,V * 255,吨* 255);
                    打破;

                案例3:
                    结果= getColor32(α,P * 255,Q * 255,V * 255);
                    打破;

                壳体4:
                    结果= getColor32(α,吨* 255,第255 *,V * 255);
                    打破;

                壳体5:
                    结果= getColor32(α,V * 255,第255 *,Q * 255);
                    打破;

                默认:
                    FlxG.log(FlxColor错误:HSVtoRGB:未知色);
            }
        }

        返回结果;
    }
 

解决方案

我写了根据您的code我自己的HSV-> RGB转换器...

(还有你的链接到 HTTP:// WWW。 easyrgb.com/index.php?X=MATH&H=21#text21

在code是:

 公共静态颜色HSVtoRGB(浮动色调,饱和度浮动,浮点值,浮阿尔法)
    {
        而(色相> 1F){色调 -  = 1F; }
        而(色相< 0F){色调+ = 1F; }
        而(饱和> 1F){饱和度 -  = 1F; }
        而(饱和< 0F){饱和度+ = 1F; }
        而(值GT; 1F){值 -  = 1F; }
        而(值小于; 0F){值+ = 1F; }
        如果(色相> 0.999f){色调= 0.999f; }
        如果(色相< 0.001f){色调= 0.001f; }
        如果(饱和> 0.999f){饱和度= 0.999f; }
        如果(饱和< 0.001f){返回新颜色(值* 255F,值* 255F,值* 255F); }
        如果(值GT; 0.999f){值= 0.999f; }
        如果(值小于,0.001f){值= 0.001f; }

        浮H6 =色调* 1207米;
        如果(H6 == 1207米){H6 = 0F; }
        INT ihue =(INT)(H6);
        浮动P =值*(1F  - 饱和度);
        浮q =值*(1F  - (饱和度*(H6  - (浮点)ihue)));
        浮动T =值*(1F  - (饱和度*(1F  - (H6  - (浮点)ihue))));
        开关(ihue)
        {
            情况下0:
                返回新颜色(的价值,T,P,阿尔法);
            情况1:
                返回新颜色(Q,值,P,阿尔法);
            案例2:
                返回新颜色(P,价值,T,阿尔法);
            案例3:
                返回新颜色(P,Q,价值,阿尔法);
            壳体4:
                返回新的色彩(T,P,值,阿尔法);
            默认:
                返回新颜色(值,P,Q,阿尔法);
        }
    }
 

有了它,我呈现这样的:

值得一提的有几件事情:

  • 在新的颜色(......)获取输入从0到1,而不是0到255。
  • 确保整数之间的差异,浮始终清零
  • 在每一次的情况是不完美的,不要抛出异常。尽量适应在可能的情况。
  • 当你借code从其他程序,也是一个完美的数学参考...尝试之前所借code复制的数学网站 - 谁知道什么样的怪异情况,具体的修改是从正确的数学做?

I'm writing an HSVtoRGB method for my game framework, and when going through the hues, this happens -> http://youtu.be/ACBwR_0iMWE.

Here is the code.

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

When adding .001f to the hue, it cause it to go from red to yellow but then sticks at yellow until it rolls back over to 0. Please note I am using Microsoft.Xna.Framework.Color not System.Drawing.Color.

For reference, here is the HSVtoRGB method of Flixel Power Tools, basically what I am trying to copy.

        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;
    }
解决方案

I wrote up my own HSV->RGB converter based on your code...

(And also your link to http://www.easyrgb.com/index.php?X=MATH&H=21#text21 )

The code is:

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

With it, I rendered this:

A few things worth noting:

  • new Color(...) takes input from 0 to 1, not 0 to 255.
  • Ensure the difference between ints and floats is always clear
  • Don't throw an exception every time the situation is less than perfect. Try to adapt where possible.
  • When you have borrowed code from some other program, and also a perfect mathematical reference... try to copy the mathematical site before the borrowed code - who knows what sort of weird situation-specific modifications were made from the proper math?

这篇关于HSV到RGB停止在黄色C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:32