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"); } }
private void Color2Tb_TextChanged(object sender, TextChangedEventArgs e) { ColorsConverter colors = new ColorsConverter(); if (Hexradio.IsChecked == true) { try { Color color = colors.FromHex(Color2Tb.Text); COlorLabel2.Background = new SolidColorBrush(color); } catch (Exception) { } } else if (RGBradio.IsChecked == true) { try { int r1 = int.Parse(Color2Tb.Text.Substring(0, Color2Tb.Text.IndexOf(","))); int b1 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.LastIndexOf(",") + 1)); int g1 = int.Parse(Color2Tb.Text.Substring(Color2Tb.Text.IndexOf(",") + 1, (Color2Tb.Text.Length - r1.ToString().Length - b1.ToString().Length - 2))); Color color = colors.RGBTOColor(r1, g1, b1); COlorLabel2.Background = new SolidColorBrush(color); } catch (Exception) { } } else { MessageBox.Show("Please Check one of Buttons HEX or RGB", "Option mismatch error"); } }
private void FromBox_TextChanged(object sender, TextChangedEventArgs e) { string Input = FromBox.Text; ColorsConverter converter = new ColorsConverter(); switch (FromCombo.SelectedIndex) { case 0: { try { From = converter.FromHex(Input); } catch (Exception) { } break; } case 1: { try { int r1 = int.Parse(Input.Substring(0, Input.IndexOf(","))); int b1 = int.Parse(Input.Substring(Input.LastIndexOf(",") + 1)); int g1 = int.Parse(Input.Substring(Input.IndexOf(",") + 1, (Input.Length - r1.ToString().Length - b1.ToString().Length - 2))); From = converter.RGBTOColor(r1, g1, b1); } catch (Exception) { } break; } case 2: { try { int a = int.Parse(Input.Substring(0, Input.IndexOf(","))); int b = int.Parse(Input.Substring(Input.LastIndexOf(",") + 1)); string Rest = Input.Substring(Input.IndexOf(",") + 1, Input.LastIndexOf(",")); int r = int.Parse(Rest.Substring(0, Rest.IndexOf(","))); int g = int.Parse(Rest.Substring(Rest.IndexOf(",") + 1)); From = Color.FromArgb(Convert.ToByte(a), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)); } catch (Exception) { } break; } case 3: { try { int r1 = int.Parse(Input.Substring(0, Input.IndexOf(","))); int b1 = int.Parse(Input.Substring(Input.LastIndexOf(",") + 1)); int g1 = int.Parse(Input.Substring(Input.IndexOf(",") + 1, (Input.Length - r1.ToString().Length - b1.ToString().Length - 2))); From = converter.FromHSL(r1, g1, b1); } catch (Exception) { } break; } case 4: { try { int a = int.Parse(Input.Substring(0, Input.IndexOf(","))); float b = float.Parse(Input.Substring(Input.LastIndexOf(",") + 1)); string Rest = Input.Substring(Input.IndexOf(",") + 1, Input.LastIndexOf(",")); float r = float.Parse(Rest.Substring(0, Rest.IndexOf(","))); float g = float.Parse(Rest.Substring(Rest.IndexOf(",") + 1)); From = converter.FromAhsb(a, r, g, b); } catch (Exception) { } break; } } try { ConvColLabel.Background = new SolidColorBrush(From); } catch (Exception) { } }
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; } } } } }