Example #1
0
        private static void GetDominantColour(string inputFile, int k)
        {
            using (Image image = Image.FromFile(inputFile)) {
                const int maxResizedDimension = 200;
                Size      resizedSize;
                if (image.Width > image.Height)
                {
                    resizedSize = new Size(maxResizedDimension, (int)Math.Floor((image.Height / (image.Width * 1.0f)) * maxResizedDimension));
                }
                else
                {
                    resizedSize = new Size((int)Math.Floor((image.Width / (image.Width * 1.0f)) * maxResizedDimension), maxResizedDimension);
                }

                using (Bitmap resized = new Bitmap(image, resizedSize)) {
                    List <Color> colors = new List <Color>(resized.Width * resized.Height);
                    for (int x = 0; x < resized.Width; x++)
                    {
                        for (int y = 0; y < resized.Height; y++)
                        {
                            colors.Add(resized.GetPixel(x, y));
                        }
                    }

                    KMeansClusteringCalculator clustering = new KMeansClusteringCalculator();
                    IList <Color> dominantColours         = clustering.Calculate(k, colors, 5.0d);

                    Console.WriteLine("Dominant colours for {0}:", inputFile);
                    foreach (Color color in dominantColours)
                    {
                        Console.WriteLine("K: {0} (#{1:x2}{2:x2}{3:x2})", color, color.R, color.G, color.B);
                    }

                    const int swatchHeight = 20;
                    using (Bitmap bmp = new Bitmap(resized.Width, resized.Height + swatchHeight)) {
                        using (Graphics gfx = Graphics.FromImage(bmp)) {
                            gfx.DrawImage(resized, new Rectangle(0, 0, resized.Width, resized.Height));

                            int swatchWidth = (int)Math.Floor(bmp.Width / (k * 1.0f));
                            for (int i = 0; i < k; i++)
                            {
                                using (SolidBrush brush = new SolidBrush(dominantColours[i])) {
                                    gfx.FillRectangle(brush, new Rectangle(i * swatchWidth, resized.Height, swatchWidth, swatchHeight));
                                }
                            }
                        }

                        string outputFile = string.Format("{0}.output.png", Path.GetFileNameWithoutExtension(inputFile));
                        bmp.Save(outputFile, ImageFormat.Png);
                        Process.Start("explorer.exe", outputFile);
                    }
                }
            }
        }
        public static void top_colors(int ofs_p, bool sv_c, int dc, Image img, SubsystemPalette p)
        {
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(img.Width, img.Height);

            List <System.Drawing.Color> colors = new List <System.Drawing.Color>(image.Width * image.Height);

            for (int x = 0; x < img.Width; x++)
            {
                for (int y = 0; y < img.Height; y++)
                {
                    colors.Add(System.Drawing.Color.FromArgb(img.GetPixel(x, y).R, img.GetPixel(x, y).G, img.GetPixel(x, y).B));
                }
            }


            KMeansClusteringCalculator   clustering      = new KMeansClusteringCalculator();
            IList <System.Drawing.Color> dominantColours = clustering.Calculate(dc, colors, 6.0d);

            Color[] c_c = WorldPalette.DefaultColors;


            for (int i = 0; i < dominantColours.Count; i++)
            {
                c_c[i + ofs_p] = new Engine.Color(dominantColours[i].R, dominantColours[i].G, dominantColours[i].B);
            }

            p.m_colors = c_c.ToArray();

            if (sv_c)
            {
                p_c__ = c_c.ToArray();
                //p_c = "158,2,81;158,131,100;1,0,1;190,2,107;186,2,80;91,45,42;128,4,62;178,90,65;130,78,56;224,2,96;209,102,76;143,94,88;186,144,135;242,123,90;227,174,167;255,255,255";
                n_w = GameManager.WorldInfo.DirectoryName;
                //dat.ValuesDictionary.GetValue<ValuesDictionary>("GameInfo").GetValue<ValuesDictionary>("Palette").SetValue<string>(str1,"Colors");
            }
        }