Ejemplo n.º 1
0
        public Color GetFoliageColor(float temp, float rain, int elevation)
        {
            if (FoliageColors == null)
            {
                return(new Color(94, 157, 52));
            }
            temp = MathHelper.Clamp(temp - elevation * 0.00166667f, 0f, 1f);
            rain = MathHelper.Clamp(rain, 0f, 1f) * temp;

            int x = (int)Math.Floor(MathHelper.Clamp(_foliageWidth - (_foliageWidth * temp), 0, _foliageWidth));
            int y = (int)Math.Floor(MathHelper.Clamp(_foliageHeight - (_foliageHeight * rain), 0, _foliageHeight));

            var indx = _foliageWidth * y + x;

            if (indx < 0)
            {
                indx = 0;
            }
            if (indx > FoliageColors.Length - 1)
            {
                indx = FoliageColors.Length - 1;
            }

            var result = FoliageColors[indx];

            return(new Color(result.R, result.G, result.B));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a color from this gradient at the specified lerp value.
        /// </summary>
        /// <param name="amount">The lerp amount.</param>
        /// <returns>A color.</returns>
        public Color Lerp(float amount)
        {
            if (Stops.Length == 0)
            {
                throw new global::System.IndexOutOfRangeException("The ColorGradient object does not have any gradient stops defined.");
            }

            else if (Stops.Length == 1)
            {
                return(Stops[0].Color);
            }

            int counter;

            for (counter = 0; counter < Stops.Length && Stops[counter].Stop < amount; counter++)
            {
                ;
            }

            counter--;
            counter = (int)MyMathHelper.Clamp(counter, 0, Stops.Length - 2);

            float newLerp = (Stops[counter].Stop - (float)amount) / (Stops[counter].Stop - Stops[counter + 1].Stop);

            return(ColorHelper.Lerp(Stops[counter].Color, Stops[counter + 1].Color, newLerp));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets an array of colors based from the gradient.
        /// </summary>
        /// <param name="count">The amount of colors to produce.</param>
        /// <returns>An array of colors.</returns>
        public Color[] ToColorArray(int count)
        {
            Color[] returnArray = new Color[count];

            if (Stops.Length == 0)
            {
                throw new global::System.IndexOutOfRangeException("The ColorGradient object does not have any gradient stops defined.");
            }

            else if (Stops.Length == 1)
            {
                for (int i = 0; i < count; i++)
                {
                    returnArray[i] = Stops[0].Color;
                }

                return(returnArray);
            }

            float lerp      = 1f / (count - 1);
            float lerpTotal = 0f;

            returnArray[0]         = Stops[0].Color;
            returnArray[count - 1] = Stops[Stops.Length - 1].Color;

            for (int i = 1; i < count - 1; i++)
            {
                lerpTotal += lerp;
                int counter;
                for (counter = 0; counter < Stops.Length && Stops[counter].Stop < lerpTotal; counter++)
                {
                    ;
                }

                counter--;
                counter = (int)MyMathHelper.Clamp(counter, 0, Stops.Length - 2);

                float newLerp = (Stops[counter].Stop - (float)lerpTotal) / (Stops[counter].Stop - Stops[counter + 1].Stop);

                returnArray[i] = ColorHelper.Lerp(Stops[counter].Color, Stops[counter + 1].Color, newLerp);
            }

            return(returnArray);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a <see cref="SadConsole.ColoredString"/> object using the current gradient.
        /// </summary>
        /// <param name="text">The text to use for the colored string.</param>
        /// <returns>A new colored string object.</returns>
        public SadConsole.ColoredString ToColoredString(string text)
        {
            SadConsole.ColoredString stringObject = new SadConsole.ColoredString(text);

            if (Stops.Length == 0)
            {
                throw new global::System.IndexOutOfRangeException("The ColorGradient object does not have any gradient stops defined.");
            }

            else if (Stops.Length == 1)
            {
                stringObject.SetForeground(Stops[0].Color);
                return(stringObject);
            }

            float lerp      = 1f / (text.Length - 1);
            float lerpTotal = 0f;

            stringObject[0].Foreground = Stops[0].Color;
            stringObject[text.Length - 1].Foreground = Stops[Stops.Length - 1].Color;

            for (int i = 1; i < text.Length - 1; i++)
            {
                lerpTotal += lerp;
                int counter;
                for (counter = 0; counter < Stops.Length && Stops[counter].Stop < lerpTotal; counter++)
                {
                    ;
                }

                counter--;
                counter = (int)MyMathHelper.Clamp(counter, 0, Stops.Length - 2);

                float newLerp = (Stops[counter].Stop - (float)lerpTotal) / (Stops[counter].Stop - Stops[counter + 1].Stop);

                stringObject[i].Foreground = ColorHelper.Lerp(Stops[counter].Color, Stops[counter + 1].Color, newLerp);
            }

            return(stringObject);
        }
Ejemplo n.º 5
0
 public static float Clamp(float val, float min, float max)
 {
     return(Helper.Clamp(val, min, max));
 }