WeighterColorDB.cs 3.55 KB
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<Brush> Items = new List<Brush>();
            public WeighterColor()
            {
            }
        }
        static List<WeighterColor> mWeighters;
        static WeighterColorDB()
        {
            //if (File.Exists("default/weighterColorDb.json"))
            //{
            //    string json = File.ReadAllText("default/weighterColorDb.json");
            //    try
            //    {
            //        mWeighters = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WeighterColor>>(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<WeighterColor>();

                for (int i = 0; i < 7; 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 < 7; j++)
                    {
                        int i2 = i + 1 + j;
                        if (i2 > 7) i2 -= 7;
                        color_name = $"Color_g{i2}#0";
                        c.Items.Add(r[color_name] as Brush);
                    }
                    mWeighters.Add(c);
                }
            }
        }

        public static Brush GetSelf(int index)
        {
            if(index<0 || index>=mWeighters.Count())
                return null;
            return mWeighters[index].Self; 
        }
        public static Brush GetItem(int numberIndex, int index)
        {
            if (numberIndex < 0 || numberIndex >= mWeighters.Count())
                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();
        }
    }
}