コード例 #1
0
    public Color GetDesaturatedColor()
    {
        RXColorHSL hsl = RXColor.HSLFromColor(_upColor);

        hsl.s = 0.25f;
        hsl.l = 0.6f;
        return(RXColor.ColorFromHSL(hsl));
    }
コード例 #2
0
    //
    // Math for the conversion found here: http://www.easyrgb.com/index.php?X=MATH
    //
    public static RXColorHSL HSLFromColor(Color rgb)
    {
        RXColorHSL c = new RXColorHSL();

        float r = rgb.r;
        float g = rgb.g;
        float b = rgb.b;

        float minChan  = Mathf.Min(r, g, b);                            //Min. value of RGB
        float maxChan  = Mathf.Max(r, g, b);                            //Max. value of RGB
        float deltaMax = maxChan - minChan;

        c.l = (maxChan + minChan) * 0.5f;

        if (Mathf.Abs(deltaMax) <= 0.0001f)                      //This is a gray, no chroma...
        {
            c.h = 0;                                             //HSL results from 0 to 1
            c.s = 0;
        }
        else                                                                                    //Chromatic data...
        {
            if (c.l < 0.5f)
            {
                c.s = deltaMax / (maxChan + minChan);
            }
            else
            {
                c.s = deltaMax / (2.0f - maxChan - minChan);
            }

            float deltaR = (((maxChan - r) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;
            float deltaG = (((maxChan - g) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;
            float deltaB = (((maxChan - b) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;

            if (Mathf.Approximately(r, maxChan))
            {
                c.h = deltaB - deltaG;
            }
            else if (Mathf.Approximately(g, maxChan))
            {
                c.h = (1.0f / 3.0f) + deltaR - deltaB;
            }
            else if (Mathf.Approximately(b, maxChan))
            {
                c.h = (2.0f / 3.0f) + deltaG - deltaR;
            }

            if (c.h < 0.0f)
            {
                c.h += 1.0f;
            }
            else if (c.h > 1.0f)
            {
                c.h -= 1.0f;
            }
        }
        return(c);
    }
コード例 #3
0
 public static Color ColorFromHSL(RXColorHSL hsl)
 {
     return(ColorFromHSL(hsl.h, hsl.s, hsl.l));
 }