Example #1
0
        public static Color Lerp(Color start, Color end, double t)
        {
            if (start == null ||
                end == null ||
                t < 0.0 || t > 1.0)
            {
                return(Color.ByARGB(255, 255, 255, 255));
            }

            // Calculate the weighted average
            var num = new double[4];

            var d1 = 1 - Math.Sqrt(Math.Pow(t - 0.0, 2));
            var d2 = 1 - Math.Sqrt(Math.Pow(t - 1.0, 2));

            num[0] += (start.Alpha * d1) + (end.Alpha * d2);
            num[1] += (start.Red * d1) + (end.Red * d2);
            num[2] += (start.Green * d1) + (end.Green * d2);
            num[3] += (start.Blue * d1) + (end.Blue * d2);

            return(ByARGB((int)(num[0] / (d1 + d2)),
                          (int)(num[1] / (d1 + d2)),
                          (int)(num[2] / (d1 + d2)),
                          (int)(num[3] / (d1 + d2))));
        }
Example #2
0
        /// <summary>
        ///     Reads an image file and returns the color values at the specified grid locations.
        /// </summary>
        /// <param name="filePath">Path to the image file.</param>
        /// <param name="numX">Number of sample grid points in the X direction.</param>
        /// <param name="numY">Number of sample grid points in the Y direction.</param>
        /// <returns name="colors">Colors at the specified grid points.</returns>
        /// <search>read,image,bitmap,png,jpg,jpeg</search>
        public static IList ReadImage(string filePath, int numX, int numY)
        {
            if (System.IO.File.Exists(filePath) == false)
            {
                return(List.Empty);
            }

            IList result = List.Empty;
            var   bmp    = new Bitmap(filePath);

            for (int y = 0; y < numY; y++)
            {
                for (int x = 0; x < numX; x++)
                {
                    int xParam = x * (bmp.Width / numX);
                    int yParam = y * (bmp.Height / numY);

                    // Insert new color at the front of the list.
                    var c = bmp.GetPixel(xParam, yParam);
                    result.Insert(0, Color.ByARGB(c.A, c.R, c.G, c.B));
                }
            }

            return(result);
        }
Example #3
0
 public static Color Divide(DSCore.Color c1, double div)
 {
     return(Color.ByARGB(
                (int)(c1.Alpha / div),
                (int)(c1.Red / div),
                (int)(c1.Green / div),
                (int)(c1.Blue / div)));
 }
Example #4
0
 public static Color Multiply(DSCore.Color c1, double div)
 {
     return(Color.ByARGB(
                (int)(c1.Alpha * div),
                (int)(c1.Red * div),
                (int)(c1.Green * div),
                (int)(c1.Blue * div)));
 }
Example #5
0
 public static Color Add(DSCore.Color c1, DSCore.Color c2)
 {
     return(Color.ByARGB(
                c1.Alpha + c2.Alpha,
                c1.Red + c2.Red,
                c1.Green + c2.Green,
                c1.Blue + c2.Blue));
 }
Example #6
0
        /// <summary>
        /// Returns the color in this color range at the specified parameter.
        /// </summary>
        /// <param name="parameter">A UV between (0.0,0.0) and (1.0,1.0).</param>
        /// <returns>A Color.</returns>
        public Color GetColorAtParameter(UV parameter)
        {
            var color = Color.ByARGB(255, 255, 255, 255);

            var weightedColors = indexedColors.ToList()
                                 .OrderBy(ic => ic.Parameter.Area(parameter)).Take(4).ToList();

            color = Color.Blerp(weightedColors, parameter);

            return(color);
        }
Example #7
0
        /// <summary>
        /// Get the color in this color range at the specified parameter.
        /// </summary>
        /// <param name="parameter">A UV between (0.0,0.0) and (1.0,1.0).</param>
        /// <returns>A Color.</returns>
        public Color GetColorAtParameter(UV parameter)
        {
            var color = Color.ByARGB(255, 255, 255, 255);

            Node node = quadtree.Root.FindNodeWhichContains(parameter);

            if (node == null)
            {
                return(color);
            }

            //var weightedColors = indexedColors.ToList()
            //    .OrderBy(ic => ic.Parameter.Area(parameter)).Take(4).ToList();

            var nodes          = node.FindAllNodesUpLevel(1);
            var weightedColors =
                nodes.Where(n => n.Point != null)
                .Where(n => n.Item != null)
                .Select(n => new Color.IndexedColor2D((Color)n.Item, n.Point)).ToList();

            color = Color.Blerp(weightedColors, parameter);

            return(color);
        }
Example #8
0
        /// <summary>
        /// Create a ColorRange1D by supplying lists of colors and parameters.
        /// </summary>
        /// <param name="colors">A list of colors.</param>
        /// <param name="parameters">A list of parameters between 0.0 and 1.0.</param>
        /// <returns>A ColorRange1D object.</returns>
        public static ColorRange1D ByColorsAndParameters(
            List <Color> colors, List <double> parameters)
        {
            var blue = Color.ByARGB(255, 0, 0, 255);
            var red  = Color.ByARGB(255, 255, 0, 0);

            if (colors == null)
            {
                colors = new List <Color>();
            }

            colors.RemoveAll(c => c == null);

            colors = colors.Where(c => c != null).ToList();

            if (parameters == null)
            {
                parameters = new List <double>();
            }

            // If there's no colors supplied, then supply
            // a red->blue gradient.
            if (!colors.Any())
            {
                colors = new List <Color>()
                {
                    red, blue
                };
            }

            // If there's no parameters supplied, then set the parameters
            // in an even range across the colors.
            if (!parameters.Any())
            {
                if (colors.Any())
                {
                    var step = 1.0 / (colors.Count() - 1);
                    for (var i = 0; i < colors.Count(); i++)
                    {
                        parameters.Add(i * step);
                    }
                }
            }

            // Remap the parameters into the 0->1 range
            var max    = parameters.Max();
            var min    = parameters.Min();
            var domain = max - min;

            if (domain == 0.0)
            {
                parameters.Clear();
                parameters.Add(0.0);
            }
            else
            {
                for (var i = 0; i < parameters.Count(); i++)
                {
                    parameters[i] = (parameters[i] - min) / domain;
                }
            }


            // If the number of colors is greater than the
            // number of parameters
            if (colors.Count() > parameters.Count())
            {
                var diff = colors.Count() - parameters.Count();
                for (var i = 0; i < diff; i++)
                {
                    parameters.Add(1.0);
                }
            }
            // If the number of parameters is greater than the
            // number of colors
            else if (parameters.Count() > colors.Count())
            {
                var diff = parameters.Count() - colors.Count();
                for (var i = 0; i < diff; i++)
                {
                    colors.Add(blue);
                }
            }

            return(new ColorRange1D(colors, parameters));
        }