Beispiel #1
0
        public static Bitmap CreatePalette(int width, int height, List <Color> paleteColors)
        {
            UnsafeBitmap palette      = new UnsafeBitmap(width, height);
            PixelData    currentColor = new PixelData();

            using (Graphics graphics = Graphics.FromImage(palette.Bitmap))
                graphics.FillRectangle(new SolidBrush(Color.FromArgb(245, 245, 245)), 0, 0, palette.Bitmap.Width, palette.Bitmap.Height);

            int CoordStep = width / paleteColors.Count;
            int lowerX    = (width - CoordStep * paleteColors.Count) / 2;
            int upperX    = CoordStep;

            foreach (Color color in paleteColors)
            {
                currentColor.red   = color.R;
                currentColor.green = color.G;
                currentColor.blue  = color.B;

                palette.LockBitmap();
                for (int x = lowerX; x < upperX; x++)
                {
                    for (int y = 0; y < palette.Bitmap.Height; y++)
                    {
                        palette.SetPixel(x, y, currentColor);
                    }
                }
                palette.UnlockBitmap();

                lowerX += CoordStep;
                upperX += CoordStep;
            }

            return(palette.Bitmap);
        }
        static public List <List <Color> > GetMainColors(Bitmap Image)
        {
            s = "";
            var watch           = System.Diagnostics.Stopwatch.StartNew();
            int AllColorsCount  = 20;
            int WorkColorsCount = 5;
            int Suppression     = 0;

            #region берем цвета из изображения
            Color[] RGB_Colors = new Color[Image.Width * Image.Height];

            PixelData CurrentPixel;
            Color     CurrentColor;

            UnsafeBitmap unsafeBitmap = new UnsafeBitmap(Image);
            int          index        = 0;
            int          upperbound   = 0;
            int          lowerboundX  = unsafeBitmap.Bitmap.Width;
            int          lowerboundY  = unsafeBitmap.Bitmap.Height;

            unsafeBitmap.LockBitmap();
            for (int x = upperbound; x < lowerboundX; x++)
            {
                for (int y = upperbound; y < lowerboundY; y++)
                {
                    CurrentPixel = unsafeBitmap.GetPixel(x, y);
                    CurrentColor = Color.FromArgb(CurrentPixel.red, CurrentPixel.green, CurrentPixel.blue);

                    if (CurrentColor.R <= 255 - Suppression && CurrentColor.G <= 255 - Suppression && CurrentColor.B <= 255 - Suppression &&
                        CurrentColor.R >= Suppression && CurrentColor.G >= Suppression && CurrentColor.B >= Suppression)
                    {
                        RGB_Colors[index] = CurrentColor;
                    }
                    index++;
                }
            }
            unsafeBitmap.UnlockBitmap();
            #endregion

            List <List <Color> > result = new List <List <Color> >();
            List <List <Color> > RGB_GroupedColors;
            Color[] palette;

            if (Environment.ProcessorCount == 2)
            {
                RGB_GroupedColors = K_means.RunSingleThread(RGB_Colors, AllColorsCount, 4);
            }
            else
            {
                RGB_GroupedColors = K_means.RunMultiThread(RGB_Colors, AllColorsCount, 4);
            }

            for (int i = 0; i < AllColorsCount; i++)
            {
                RGB_GroupedColors[i] = DeleteLowSaturated(RGB_GroupedColors[i]);
            }

            palette = GetNearestColors(RGB_GroupedColors);

            RGB_GroupedColors.Clear();

            if (MainForm.CoreCount == 2)
            {
                RGB_GroupedColors = K_means.RunSingleThread(palette, WorkColorsCount, 2);
            }
            else
            {
                RGB_GroupedColors = K_means.RunMultiThread(palette, WorkColorsCount, 2);
            }

            result.Add(palette.ToList());
            result.Add(GetCustomColors(RGB_GroupedColors, "saturated"));
            result.Add(GetCustomColors(RGB_GroupedColors, "muffled"));
            result.Add(GetCustomColors(RGB_GroupedColors, "dark"));
            result.Add(GetCustomColors(RGB_GroupedColors, "bright"));

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            //   System.Windows.Forms.MessageBox.Show(s + "\n" + ((float)elapsedMs / 1000).ToString() + "\t time");
            return(result);
        }