private void LoadColorInfo(LVColorItem lVColorItem) { JObject jTokenColor = (JObject)lVColorItem.jToken; UpdatePreview(); // Set main color info TBoxColorName.Text = jTokenColor["name"].ToString(); TBoxColorRGBCode.Text = jTokenColor["rgb"].ToString(); TBoxColorHexCode.Text = jTokenColor["hex"].ToString(); TBoxColorDistance.Text = jTokenColor["distance"].ToString(); // Set Hue info if (jTokenColor.ContainsKey("hueName")) { TBoxHueColorName.Text = jTokenColor["hueName"].ToString(); } else { TBoxHueColorName.Text = ""; } }
/// <summary> /// Gets the best matching colors through all color lists available /// </summary> private void GetBestColorMatches() { int lViewCurrentSelectedIndex = LViewColors.SelectedIndex; listColorBestResults.Clear(); object _objLock = new object(); // Execute computing operations over all lists in parallel to speedup the operation Parallel.ForEach(Common.colorsLists, (jColorList) => { JToken jTokenColor = FindColor.GetNearestColor(Common.chosenColorHex, jColorList); LVColorItem lVColorItem = new LVColorItem { SourceName = jColorList["Info"]["source"].ToString(), Name = jTokenColor["name"].ToString(), HexCode = jTokenColor["hex"].ToString(), Color = (SolidColorBrush) new BrushConverter().ConvertFromString(jTokenColor["hex"].ToString()), jToken = jTokenColor }; lock (_objLock) // Prevent access to the variable from multiple thread at same time { lVColorItem.Color.Freeze(); // Make color variable unmodifieable, avoid thread issues listColorBestResults.Add(lVColorItem); } }); LVColorItem itemBest = FindBestSimilarColor(); if (itemBest != null) { // TODO: Seem (not sure) that the SortDescription added to the CollectionViewSource causes // a delay in the passage of items in the ListView.Items probably asynchronously, // then the ListView.Items are populated late and this cause the failure of ListView.SelectedIndex // i have not found a good solution to this problem, // force the Refresh is a ugly workaround fortunately we have few items only viewSource.View.Refresh(); // Auto-select the best result on the ListView if (listColorBestResults.Count > 0) { if (MIAutoSelectBestColor.IsChecked == true) { LViewColors.SelectedItem = itemBest; } else { if (lViewCurrentSelectedIndex == -1) { LViewColors.SelectedIndex = 0; } else { LViewColors.SelectedIndex = lViewCurrentSelectedIndex; } } } // Report info to the left grid TBoxApproxColorName.Text = itemBest.jToken["name"].ToString(); // Try get the best hue name from results (find the lower distance value with hue name) var itemBestHue = listColorBestResults.OrderBy(o => o.jToken["distance"].ToObject <double>()).FirstOrDefault(f => (f.jToken as JObject).ContainsKey("hueName")); if (itemBestHue != null) { TBoxApproxHueName.Text = itemBestHue.jToken["hueName"].ToString(); } else { TBoxApproxHueName.Text = ""; } } else { TBoxApproxColorName.Text = ""; TBoxApproxHueName.Text = ""; } }