Beispiel #1
0
    public static HSVColor RGBToHSV(Color color)
    {
        HSVColor ret = new HSVColor(0f, 0f, 0f, color.a);

        float r = color.r;
        float g = color.g;
        float v = color.b;

        float max = Mathf.Max(r, Mathf.Max(g, v));

        if (max <= 0f)
        {
            return(ret);
        }

        float min = Mathf.Min(r, Mathf.Min(g, v));
        float dif = max - min;

        if (max > min)
        {
            if (WadeUtils.AreEqual(g, max))
            {
                ret.h = (v - r) / dif * 60f + 120f;
            }
            else if (WadeUtils.AreEqual(v, max))
            {
                ret.h = (r - g) / dif * 60f + 240f;
            }
            else if (v > g)
            {
                ret.h = (g - v) / dif * 60f + 360f;
            }
            else
            {
                ret.h = (g - v) / dif * 60f;
            }

            if (WadeUtils.IsNegative(ret.h))
            {
                ret.h = ret.h + 360f;
            }
        }
        else
        {
            ret.h = 0f;
        }

        ret.h *= 1f / 360f;
        ret.s  = (dif / max) * 1f;
        ret.v  = max;

        return(ret);
    }
Beispiel #2
0
    public static HSVColor Lerp(HSVColor a, HSVColor b, float t)
    {
        float h, s;

        //check special case black (color.v==0): interpolate neither hue nor saturation!
        //check special case grey (color.s==0): don't interpolate hue!
        if (WadeUtils.AreEqual(a.v, 0f))
        {
            h = b.h;
            s = b.s;
        }
        else if (WadeUtils.AreEqual(b.v, 0f))
        {
            h = a.h;
            s = a.s;
        }
        else
        {
            if (WadeUtils.AreEqual(a.s, 0f))
            {
                h = b.h;
            }
            else if (WadeUtils.AreEqual(b.s, 0f))
            {
                h = a.h;
            }
            else
            {
                // works around bug with LerpAngle
                float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
                while (angle < 0f)
                {
                    angle += 360f;
                }
                while (angle > 360f)
                {
                    angle -= 360f;
                }

                h = angle / 360f;
            }

            s = Mathf.Lerp(a.s, b.s, t);
        }

        return(new HSVColor(h, s, Mathf.Lerp(a.v, b.v, t), Mathf.Lerp(a.a, b.a, t)));
    }