예제 #1
0
        /// <summary>
        /// Evaluate pos into a color
        /// </summary>
        /// <param name="pos">Value in range 0.0f-1.0f</param>
        /// <param name="color">Color will be interpolated into this</param>
        /// <returns></returns>
        public void evaluate(float pos, float4 color)
        {
            if (Stops.Count == 0)
            {
                color.x = 0.0f;
                color.y = 0.0f;
                color.z = 0.0f;
                color.w = 0.0f;
                return;
            }

            // stop1 = left of pos
            var left = Stops[0];
            // stop2 = right of pos
            ColorStop right = null;

            // if there is only one stop, or we're on the left of the very first stop
            // just copy the color, we're done.
            if (Stops.Count == 1 || pos <= left.Position)
            {
                color.Copy(left.Color);
            }
            else
            {
                // get left and right items
                var lr = GetLeftRight(pos);
                left  = lr.Item1;
                right = lr.Item2;
                var last_item = lr.Item3;

                // if we're on the right of the right stop, copy
                // color, we're done.
                if (pos >= right.Position)
                {
                    color.Copy(right.Color);
                }
                else
                {
                    // if we're on constant interpolation, lets use
                    // color on left of position.
                    if (Interpolation == Interpolations.Constant)
                    {
                        color.Copy(left.Color);
                    }
                    else
                    {
                        // ok, so we need to interpolate
                        float mfac;
                        float fac;
                        // get factor
                        if (Math.Abs(left.Position - right.Position) > 0.0001)
                        {
                            fac = Math.Abs(pos - right.Position) / Math.Abs(left.Position - right.Position);
                        }
                        else
                        {
                            fac = last_item ? 1.0f : 0.0f;
                        }

                        // extra easing if ease
                        if (Interpolation == Interpolations.Ease)
                        {
                            mfac = fac * fac;
                            fac  = 3.0f * mfac - 2.0f * mfac * fac;
                        }

                        // right color fac
                        mfac = 1.0f - fac;

                        // factor colors
                        left.Color  *= fac;
                        right.Color *= mfac;
                        // add factored colors to get new interpolated color
                        var interpolated_color = left.Color + right.Color;
                        // copy, done
                        color.Copy(interpolated_color);
                    }
                }
            }
        }