private void DrawCategory(FlagGenerator.FlagColorCategory matchCategory, double?hue) { canvasDebug.Children.Clear(); double width = canvasDebug.ActualWidth; double height = canvasDebug.ActualHeight; int size = 20; int count = Convert.ToInt32((Math.Ceiling(width / size) * Math.Ceiling(height / size))); FlagGenerator.FlagColorCategoriesCache cache = new FlagGenerator.FlagColorCategoriesCache(); Color[] colors = Enumerable.Range(0, count). AsParallel(). Select(o => cache.GetRandomColor(matchCategory, hue).ToRGB()). ToArray(); int index = 0; for (int x = 0; x < width; x += size) { for (int y = 0; y < height; y += size) { Rectangle rect = new Rectangle() { Width = size, Height = size, Fill = new SolidColorBrush(colors[index]), ToolTip = colors[index].ToHex(false, false), }; rect.MouseDown += new MouseButtonEventHandler((s, b) => { Clipboard.SetText(((Rectangle)s).ToolTip.ToString()); }); Canvas.SetLeft(rect, x); Canvas.SetTop(rect, y); canvasDebug.Children.Add(rect); index++; } } }
private void ColorCategories_Click(object sender, RoutedEventArgs e) { try { canvasDebug.Children.Clear(); double width = canvasDebug.ActualWidth; double height = canvasDebug.ActualHeight; // Reverse so the top priority is drawn first foreach (var category in new FlagGenerator.FlagColorCategoriesCache().Categories.Reverse()) { Rectangle rect = new Rectangle() { Width = category.Item1.Width / 100 * width, Height = category.Item1.Height / 100 * height, Fill = new SolidColorBrush(UtilityWPF.GetRandomColor(0, 255)), ToolTip = category.Item2.ToString(), }; rect.MouseDown += new MouseButtonEventHandler((s, b) => { FlagGenerator.FlagColorCategory cat = UtilityCore.EnumParse <FlagGenerator.FlagColorCategory>(((Rectangle)s).ToolTip.ToString()); double?hue = b.LeftButton == MouseButtonState.Pressed ? (double?)null : StaticRandom.NextDouble(360); DrawCategory(cat, hue); }); Canvas.SetLeft(rect, category.Item1.X / 100 * width); double top = height - ((category.Item1.Y + category.Item1.Height) / 100 * height); Canvas.SetTop(rect, top); canvasDebug.Children.Add(rect); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }