public static List <LCColor> GetColorsFromImage(Bitmap image, int count) { var colorTuple = GetAllColors(image); Dictionary <int, int> pixelsHighToLow = colorTuple.Item1; int totalPixels = colorTuple.Item2; List <LCColor> mostUsedColors = new List <LCColor>(); decimal numberOfGroups = 4; int counter = 0; int groupSize = Convert.ToInt32(Math.Ceiling(pixelsHighToLow.Count / numberOfGroups)); var result = pixelsHighToLow.GroupBy(x => counter++ / groupSize); int timesToAdd = 0; List <int> take = new List <int>(new int[] { 25, 15, 5, 5, 0, 0 }); bool addBlack = true; foreach (var dict in result) { var tempdict = dict.Take(take[0]); foreach (KeyValuePair <int, int> kvp in tempdict) { decimal pixelCount = kvp.Value; if (mostUsedColors.Count < count) { int percent = Convert.ToInt32((pixelCount / totalPixels) * 100); timesToAdd = Math.Max((percent * 20) / 100, 1); LCColor color = new LCColor(Color.FromArgb(kvp.Key)); if ((color.r == 0 && color.g == 0 && color.b == 0 && addBlack) || percent > 50) { mostUsedColors.AddToList(color, timesToAdd); addBlack = false; } else { mostUsedColors.AddToList(color, timesToAdd); } } } take.Remove(take[0]); if (mostUsedColors.Count > 50) { break; } } if (mostUsedColors.Count == 0) { mostUsedColors.Add(new LCColor(Color.Black)); } return(mostUsedColors); }
public void MimickMostUsed(Nanoleaf nanoleaf) { Stopwatch timer = null; Bitmap bitmap = null; while (true) { timer = new Stopwatch(); timer.Start(); bitmap = ImageAnalysis.CaptureBitmap(1); if (bitmap == null) { JArray colors = PaletteController.RandomPalette(1, maxBrightness: 0); lightController.CreateCustom(nanoleaf, colors, 10, 10); } else { int paletteCount = 12; var mostUsed = ImageAnalysis.GetColorsFromImage(bitmap, paletteCount); JArray palette = new JArray(); foreach (LCColor color in mostUsed) { LCColor lcc = new LCColor("rgb", color.r, color.g, color.b); palette.Add(lcc); } lightController.CreateCustom(nanoleaf, palette, 5, 50, animType: "random"); } timer.Stop(); CalcAverage(timer.ElapsedMilliseconds); } }
public static void AddToList(this List <LCColor> colors, LCColor color, int timesToAdd) { while (timesToAdd > 0) { colors.Add(color); timesToAdd -= 1; } }
public static JObject Random() { Random random = new Random(); int hue = random.Next(0, 360); int saturation = random.Next(0, 100); int brightness = random.Next(2, 100); JObject color = new LCColor("hsb", hue, saturation, brightness).HSBJO; return(color); }
public static List <LCColor> GetAverageColors(Bitmap image, int rows, int columns, int borderReduction = 0) { List <LCColor> averageColors = new List <LCColor>(); List <List <LCColor> > chunks = new List <List <LCColor> >(); //this will reduce the border on every side by X pixels int width = image.Size.Width - borderReduction; int height = image.Size.Height - borderReduction; int rowSize = width / rows; int columnSize = height / columns; int currentRow = borderReduction; int currentColumn = borderReduction; List <int> currentRowPixels = new List <int>(); while (currentColumn < height) { currentRow = borderReduction; while (currentRow < width) { chunks.Add(GetChunkColors(image, rowSize, columnSize, currentRow, currentColumn, width, height, pixelJump: 5)); currentRow += rowSize; } currentColumn += columnSize; } foreach (List <LCColor> chunk in chunks) { double r = 0; double g = 0; double b = 0; foreach (LCColor color in chunk) { r += color.r; g += color.g; b += color.b; } r = Math.Round(r / chunk.Count); g = Math.Round(g / chunk.Count); b = Math.Round(b / chunk.Count); LCColor averageColor = new LCColor(Color.FromArgb(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b))); averageColors.Add(averageColor); } return(averageColors); }
public static JArray RandomPalette(int count, int minHue = 1000, int maxHue = 1000, int minSaturation = 1000, int maxSaturation = 1000, int minBrightness = 1000, int maxBrightness = 1000) { if (count > 50) { //20 is the max allowed to edit on the app count = 50; } //if maintaining the values of HSB, check that they are valid minHue = (minHue != 1000 && minHue < 360) ? minHue : minHue = 0; maxHue = (maxHue != 1000 && maxHue < 360) ? maxHue : maxHue = 360; minSaturation = (minSaturation != 1000 && minSaturation < 100) ? minSaturation : minSaturation = 0; maxSaturation = (maxSaturation != 1000 && maxSaturation < 100) ? maxSaturation : maxSaturation = 100; minBrightness = (minBrightness != 1000 && minBrightness < 100) ? minBrightness : minBrightness = 2 /*min brightness needs to be 2*/; maxBrightness = (maxBrightness != 1000 && maxBrightness < 100) ? maxBrightness : maxBrightness = 100; JArray colors = new JArray(); Random random = new Random(); while (count > 0) { int hue = random.Next(minHue, maxHue); int saturation = random.Next(minSaturation, minSaturation); int brightness = random.Next(minBrightness, minBrightness); JObject color = new LCColor("hsb", hue, saturation, brightness).HSBJO; colors.Add(color); count--; } return(colors); }