Beispiel #1
0
        /// <summary>
        /// Interpolates between 2 colors and returns a Color struct
        /// </summary>
        /// <param name="c1">The bottom value on the spectrum</param>
        /// <param name="c2">The top value on the spectrum</param>
        /// <param name="val">The place along the spectrum, value between 0.0f and 1.0f, with 0.0f being c1, and 1.0f being c2</param>
        /// <returns>Color struct which is along the hue spectrum according to val</returns>
        public static Color Interpolate(Color c1, Color c2, float val)
        {
            // The conversion to HSV makes the interpolation smoother
            HSVColor color1 = new HSVColor(c1.R, c1.G, c1.B);
            HSVColor color2 = new HSVColor(c2.R, c2.G, c2.B);

            float h;
            float d = color2.H - color1.H;
            // Depending on where the 2 colors are on the spectrum we may have to swap them
            float h1, h2;

            if (color1.H > color2.H)
            {
                h2  = color1.H;
                h1  = color2.H;
                d   = -d;
                val = 1 - val;
            }
            else
            {
                h1 = color1.H;
                h2 = color2.H;
            }

            if (d > 0.5f)
            {
                h1 = h1 + 1;
                h  = (h1 + val * (h2 - h1)) % 1;
            }
            else
            {
                h = h1 + val * d;
            }

            float s          = color1.S + val * (color2.S - color1.S);
            float v          = color1.V + val * (color2.V - color1.V);
            var   finalColor = new HSVColor(h, s, v);


            return(finalColor.GetColor((int)((c2.A - c1.A) * val + c1.A)));
        }