コード例 #1
0
        private void EqualBtn_Click(object sender, RoutedEventArgs e)
        {
            ColorsConverter colors = new ColorsConverter();
            Color           color1 = new Color(), color2 = new Color();
            float           coef = 0;

            try
            {
                if (Hexradio.IsChecked == true)
                {
                    color1 = colors.FromHex(Color1Tb.Text);
                    if (Operator != '*')
                    {
                        color2 = colors.FromHex(Color2Tb.Text);
                    }
                    else
                    {
                        coef = float.Parse(Color2Tb.Text);
                    }
                }
                else if (RGBradio.IsChecked == true)
                {
                    int r1 = int.Parse(Color1Tb.Text.Substring(0, Color1Tb.Text.IndexOf(",")));
                    int b1 = int.Parse(Color1Tb.Text.Substring(Color1Tb.Text.LastIndexOf(",") + 1));
                    int g1 = int.Parse(Color1Tb.Text.Substring(Color1Tb.Text.IndexOf(",") + 1, (Color1Tb.Text.Length - r1.ToString().Length - b1.ToString().Length - 2)));
                    color1 = colors.RGBTOColor(r1, g1, b1);
                    if (Operator != '*')
                    {
                        int r2 = int.Parse(Color2Tb.Text.Substring(0, Color1Tb.Text.IndexOf(",")));
                        int b2 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.LastIndexOf(",") + 1));
                        int g2 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.IndexOf(",") + 1, (Color2Tb.Text.Length - r2.ToString().Length - b2.ToString().Length - 2)));

                        color2 = colors.RGBTOColor(r2, g2, b2);
                    }

                    else
                    {
                        coef = float.Parse(Color2Tb.Text);
                    }
                }
                Result        = colors.ColorOperation(color1, color2, Operator, coef);
                ResultTb.Text = Result;
                ResultColor   = colors.FromHex(Result);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message + "\nPlease enter value like #ffffff or 255,255,255");
            }
        }
コード例 #2
0
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ColorsConverter colors = new ColorsConverter();
                Color           color1 = new Color(), color2 = new Color();
                float           coef = 0;
                try
                {
                    if (Hexradio.IsChecked == true)
                    {
                        color1 = colors.FromHex(Color1Tb.Text);
                        if (Operator != '*')
                        {
                            color2 = colors.FromHex(Color2Tb.Text);
                        }
                        else
                        {
                            coef = float.Parse(Color2Tb.Text);
                        }
                    }
                    else if (RGBradio.IsChecked == true)
                    {
                        int r1 = int.Parse(Color1Tb.Text.Substring(0, Color1Tb.Text.IndexOf(",")));
                        int b1 = int.Parse(Color1Tb.Text.Substring(Color1Tb.Text.LastIndexOf(",") + 1));
                        int g1 = int.Parse(Color1Tb.Text.Substring(Color1Tb.Text.IndexOf(",") + 1, (Color1Tb.Text.Length - r1.ToString().Length - b1.ToString().Length - 2)));
                        color1 = colors.RGBTOColor(r1, g1, b1);
                        if (Operator != '*')
                        {
                            int r2 = int.Parse(Color2Tb.Text.Substring(0, Color1Tb.Text.IndexOf(",")));
                            int b2 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.LastIndexOf(",") + 1));
                            int g2 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.IndexOf(",") + 1, (Color2Tb.Text.Length - r2.ToString().Length - b2.ToString().Length - 2)));

                            color2 = colors.RGBTOColor(r2, g2, b2);
                        }

                        else
                        {
                            coef = float.Parse(Color2Tb.Text);
                        }
                    }
                    Result        = colors.ColorOperation(color1, color2, Operator, coef);
                    ResultTb.Text = Result;
                    ResultColor   = colors.FromHex(Result);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message + "\nPlease enter value like #ffffff or 255,255,255");
                }
            }
            else if (e.Key == Key.OemPlus || e.Key == Key.Add)
            {
                Operator           = '+';
                OperLabel.Content  = PlusBtn.Content;
                Color2Tb.Focusable = true;
                Color2Tb.Focus();
            }
            else if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
            {
                Operator           = '-';
                OperLabel.Content  = MinusBtn.Content;
                Color2Tb.Focusable = true;
                Color2Tb.Focus();
            }
            else if (e.Key == Key.Multiply)
            {
                Operator           = '*';
                OperLabel.Content  = TimesBtn.Content;
                Color2Tb.Focusable = true;
                Color2Tb.Focus();
            }
            else
            {
                if (ColorList.IsVisible)
                {
                    foreach (var i in ColorList.Items)
                    {
                        if (i.ToString().Remove(0, 38).StartsWith(e.Key.ToString()))
                        {
                            ColorList.ScrollIntoView(i);
                            return;
                        }
                    }
                }
            }
        }