public static async Task <FindClosestColor> Create(Color originalColor) { var findClosestColor = new FindClosestColor(); var colors = await FindClosestColor.GetColors(originalColor); // Set the data to show. findClosestColor.lbColors.ItemsSource = colors; findClosestColor.DataContext.OriginalColor = originalColor; findClosestColor.DataContext.SelectedColorItem = colors[0]; return(findClosestColor); }
static async Task <List <ColorItem> > GetColors(Color color) => await FindClosestColor.colorItemCache.GetOrAdd(color, async key => { // Find the L*a*b* color for the given color, find the distance between that and the L*a*b* colors for all wall+paint combinations and sort by the distance. var originalLAB = LAB.FromRGB(color); var cachedColors = await FindClosestColor.InitializeCache(); var unorderedColors = await Task.WhenAll(cachedColors.Select(async c => await Task.Run(() => new ColorItem() { ColorName = c.ColorName, Distance = LAB.Distance(originalLAB, c.LABColor), WallColor = c.WallColor, WallImage = c.WallImage, WallName = c.WallName }))); return(unorderedColors.OrderBy(c => c.Distance).ToList()); });
async void FindClosestColor_Click(object sender, RoutedEventArgs e) { // Show the overlay on the main window and show the Find Closest Color dialog box. var mainWindow = Window.GetWindow(this) as MainWindow; mainWindow.ShowOverlay(false); var findClosestColor = await FindClosestColor.Create(this.DataContext.Color); findClosestColor.Owner = mainWindow; if (findClosestColor.ShowDialog() ?? false) { // This will only happen if the user clicked OK in the Find Closest Color dialog box. this.ProcessColorChange = false; this.DataContext.SelectedWall = await WallItem.Create(findClosestColor.DataContext.SelectedColorItem.WallName); this.DataContext.SelectedWallColor = findClosestColor.DataContext.SelectedColorItem.ColorName; this.ProcessColorChange = true; this.Wall_SelectionChanged(this, null); } mainWindow.HideOverlay(); }