using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Media; namespace FLY.Weight.UI.Client { public class WeighterColorDB { class WeighterColor { public Brush Self; public List Items = new List(); public WeighterColor() { } } static List mWeighters; static WeighterColorDB() { //if (File.Exists("default/weighterColorDb.json")) //{ // string json = File.ReadAllText("default/weighterColorDb.json"); // try // { // mWeighters = Newtonsoft.Json.JsonConvert.DeserializeObject>(json); // } // catch { // //异常,从本地读取 // } //} //if(mWeighters == null || mWeighters.Count()==0) { ResourceDictionary r = new ResourceDictionary() { Source = new Uri("pack://application:,,,/FLY.Weight.UI.Client;component/Themes/Dictionary_CellColor.xaml") }; mWeighters = new List(); int color_cnt = 0; //找出颜色数量 while (true) { int i = color_cnt + 1; string color_name = $"Color_g{i}#0"; if (!r.Contains(color_name)) { //上一个已经是极限了 break; } color_cnt = i; } if (color_cnt < 3) throw new Exception("称重颜色不够3个!!!!"); for (int i = 0; i < color_cnt; i++) { WeighterColor c = new WeighterColor(); string color_name = $"Color_g{i+1}#0"; c.Self = r[color_name] as Brush; for (int j = 0; j < color_cnt; j++) { int i2 = i + 1 + j; if (i2 > color_cnt) i2 -= color_cnt; color_name = $"Color_g{i2}#0"; c.Items.Add(r[color_name] as Brush); } mWeighters.Add(c); } } } public static Brush GetSelf(int index) { //颜色循环 index = GetNumberIndex(index); if(index<0) return null; return mWeighters[index].Self; } static int GetNumberIndex(int index) { //颜色循环 if (mWeighters.Count() <= 0) return -1; while (index < 0) index += mWeighters.Count(); while (index >= mWeighters.Count()) index -= mWeighters.Count(); return index; } public static Brush GetItem(int numberIndex, int index) { //颜色循环 numberIndex = GetNumberIndex(numberIndex); if (numberIndex < 0) return null; int cnt = mWeighters[numberIndex].Items.Count(); if (index >= cnt || index < 0) { Random r = new Random(); index = r.Next(cnt - 1); } return mWeighters[numberIndex].Items[index]; } } public class Number2ColorValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int index = (int)value; return WeighterColorDB.GetSelf(index); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class Number2ColorItemValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int number_index = (int)value; int index = (int)parameter; return WeighterColorDB.GetItem(number_index, index); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }