using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ryControls
{
    public partial class FrmColorConvert : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmColorConvert()
        {
            InitializeComponent();
            LblColor.ForeColor = Color.FromArgb( 255 - BackColor.R, 255 - BackColor.G, 255 - BackColor.B);
            CbbFormat.SelectedIndex = 0;
        }

        private void LblColor_BackColorChanged(object sender, EventArgs e)
        {
            LblColor.ForeColor = Color.FromArgb(255- LblColor.BackColor.A,255 - LblColor.BackColor.R, 255 - LblColor.BackColor.G, 255 - LblColor.BackColor.B);
        }

        private void TxtColor_TextChanged(object sender, EventArgs e)
        {
            string color_text = TxtColor.Text.ToLower();
            switch (CbbFormat.Text)
            {
                case "自动识别"://全自动
                    AutoColor(color_text);
                    break;
                case "CMYK"://
                    if(color_text.IndexOf("cmyk(")==0)
                    {
                        AutoColor(color_text);
                    }
                    else
                    {
                        AutoColor("cmyk(" + color_text+")");
                    }
                    break;
                case "XYZ"://
                    if (color_text.IndexOf("xyz(") == 0)
                    {
                        AutoColor(color_text);
                    }
                    else
                    {
                        AutoColor("xyz(" + color_text + ")");
                    }
                    break;
            }

        }
        private void CMYKColor(string color_text)
        {
            bool isRight_color = false;
            try
            {
                LblColor.BackColor = System.Drawing.ColorTranslator.FromHtml(color_text);
                LblColor.Text = "示例";
                isRight_color = true;
            }
            catch
            {
                try
                {
                    if (color_text.IndexOfEx("rgb(") == 0)
                    {
                        #region rgb颜色
                        string rgb = color_text.ToLower().GetStr("rgb(", ")");
                        if (rgb != "")
                        {
                            var rgb_item = rgb.Split(',');
                            if (rgb_item.Length == 3 && rgb_item[0].IsInt() && rgb_item[1].IsInt() && rgb_item[2].IsInt())
                            {
                                LblColor.BackColor = Color.FromArgb(rgb_item[0].ToInt(), rgb_item[1].ToInt(), rgb_item[2].ToInt());
                                isRight_color = true;
                            }
                        }
                        #endregion
                    }
                    else if (color_text.IndexOfEx("rgba(") == 0)
                    {
                        #region rgb颜色
                        string rgb = color_text.ToLower().GetStr("rgba(", ")");
                        if (rgb != "")
                        {
                            var rgb_item = rgb.Split(',');
                            if (rgb_item.Length == 4 && rgb_item[0].IsInt() && rgb_item[1].IsInt() && rgb_item[2].IsInt() && rgb_item[3].IsInt())
                            {
                                LblColor.BackColor = Color.FromArgb((rgb_item[3].ToDouble() * 255).ToInt(), rgb_item[0].ToInt(), rgb_item[1].ToInt(), rgb_item[2].ToInt());
                                isRight_color = true;
                            }
                        }
                        #endregion
                    }
                    else if (color_text.IndexOfEx("hsl(") == 0)
                    {
                        #region hsl颜色
                        string rgb = color_text.ToLower().GetStr("hsl(", ")");
                        if (rgb != "")
                        {
                            var rgb_item = rgb.Split(',');
                            if (rgb_item.Length == 3)
                            {
                                string s_str = rgb_item[1].Trim();
                                string l_str = rgb_item[2].Trim();
                                double s = (s_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (s_str.EndsWith("%")) { s = s / 100d; }
                                double l = (l_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (l_str.EndsWith("%")) { l = l / 100d; }
                                ColorHSL hsl = new ColorHSL(rgb_item[0].Trim().TrimEnd('°').ToInt(), s.ToInt(), l.ToInt());
                                ColorRGB RGB = ColorHelper.HslToRgb(hsl);
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                        }
                        #endregion
                    }
                    else if (color_text.IndexOfEx("hsv(") == 0)
                    {
                        #region hsv颜色
                        string rgb = color_text.ToLower().GetStr("hsv(", ")");
                        if (rgb != "")
                        {
                            var rgb_item = rgb.Split(',');
                            if (rgb_item.Length == 3)
                            {
                                string s_str = rgb_item[1].Trim();
                                string v_str = rgb_item[2].Trim();
                                double s = (s_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (s_str.EndsWith("%")) { s = s / 100d; }
                                double v = (v_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (v_str.EndsWith("%")) { v = v / 100d; }
                                ColorHSV hsv = new ColorHSV(rgb_item[0].Trim().TrimEnd('°').ToInt(), s.ToInt(), v.ToInt());
                                ColorRGB RGB = ColorHelper.HsvToRgb(hsv);
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                        }
                        #endregion
                    }
                    else if (color_text.IndexOfEx("xyz(") == 0)
                    {
                        #region xyz颜色
                        string rgb = color_text.ToLower().GetStr("xyz(", ")");
                        if (rgb != "")
                        {
                            var rgb_item = rgb.Split(',');
                            if (rgb_item.Length == 3)
                            {
                                ColorRGB RGB = ColorHelper.XYZ2RGB(rgb_item[0].Trim().ToDouble(), rgb_item[1].Trim().ToDouble(), rgb_item[2].Trim().ToDouble());
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                        }
                        #endregion
                    }
                    if (!isRight_color) { ErrorColor(); }
                    else { LblColor.Text = "示例"; }
                }
                catch
                {
                    ErrorColor();
                }
            }
        }
        private void AutoColor(string color_text)
        {
            bool isRight_color = false;
            try
            {
                LblColor.BackColor = System.Drawing.ColorTranslator.FromHtml(color_text);
                LblColor.Text = "示例";
                isRight_color = true;
            }
            catch
            {
                try
                {
                    var color_name = color_text.GetStr("", "(");
                    var color_item= color_text.GetStr("(", ")");
                    var color_array = color_item.Split(',');
                    switch (color_name)
                    {
                        case "rgb":
                            #region rgb颜色
                            if (color_array.Length == 3 && color_array[0].IsInt() && color_array[1].IsInt() && color_array[2].IsInt())
                            {
                                LblColor.BackColor = Color.FromArgb(color_array[0].ToInt(), color_array[1].ToInt(), color_array[2].ToInt());
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "rgba":
                            #region rgb颜色
                            if (color_array.Length == 4 && color_array[0].IsInt() && color_array[1].IsInt() && color_array[2].IsInt() && color_array[3].IsInt())
                            {
                                LblColor.BackColor = Color.FromArgb((color_array[3].ToDouble() * 255).ToInt(), color_array[0].ToInt(), color_array[1].ToInt(), color_array[2].ToInt());
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "hsl":
                            #region hsl颜色
                            if (color_array.Length == 3)
                            {
                                string s_str = color_array[1].Trim();
                                string l_str = color_array[2].Trim();
                                double s = (s_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (s_str.EndsWith("%")) { s = s / 100d; }
                                double l = (l_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (l_str.EndsWith("%")) { l = l / 100d; }
                                ColorHSL hsl = new ColorHSL(color_array[0].Trim().TrimEnd('°').ToInt(), s.ToInt(), l.ToInt());
                                ColorRGB RGB = ColorHelper.HslToRgb(hsl);
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "hsv":
                            #region hsv颜色
                            if (color_array.Length == 3)
                            {
                                string s_str = color_array[1].Trim();
                                string v_str = color_array[2].Trim();
                                double s = (s_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (s_str.EndsWith("%")) { s = s / 100d; }
                                double v = (v_str.TrimEnd('%').ToDouble() * 255).ToInt();
                                if (v_str.EndsWith("%")) { v = v / 100d; }
                                ColorHSV hsv = new ColorHSV(color_array[0].Trim().TrimEnd('°').ToInt(), s.ToInt(), v.ToInt());
                                ColorRGB RGB = ColorHelper.HsvToRgb(hsv);
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "xyz":
                            #region xyz颜色
                            if (color_array.Length == 3)
                            {
                                ColorRGB RGB = ColorHelper.XYZ2RGB(color_array[0].Trim().ToDouble(), color_array[1].Trim().ToDouble(), color_array[2].Trim().ToDouble());
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "yxy":
                            #region xyz颜色
                            if (color_array.Length == 3)
                            {
                                ColorRGB RGB = ColorHelper.Yxy2RGB(color_array[0].Trim().ToDouble(), color_array[1].Trim().ToDouble(), color_array[2].Trim().ToDouble());
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                            #endregion
                            break;
                        case "cmyk":
                            #region cmyk颜色
                            if (color_array.Length == 4)
                            {
                                ColorRGB RGB = ColorHelper.CMYK2RGB(color_array[0].ToDouble(), color_array[1].ToDouble(), color_array[2].ToDouble(), color_array[3].ToDouble());
                                LblColor.BackColor = Color.FromArgb(RGB.R, RGB.G, RGB.B);
                                isRight_color = true;
                            }
                            #endregion
                            break;
                    }
                    if (!isRight_color) { ErrorColor(); }
                    else { LblColor.Text = "示例"; }
                }
                catch
                {
                    ErrorColor();
                }
            }
        }
        void ErrorColor()
        {
            LblColor.Text = "错误";
        }
        private void BtnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
