MainWindow.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using MahApps.Metro.Controls;
  16. using System.Reflection;
  17. using System.Collections.ObjectModel;
  18. namespace WPFColorAssiant
  19. {
  20. public class ColorInfo
  21. {
  22. public Brush SolidColorBrush { get; set; }
  23. public Color ColorObject;
  24. public string Name { get; set; }
  25. public ColorInfo(PropertyInfo pi) {
  26. Name = pi.Name;
  27. var c = (Color)pi.GetValue(null, null);
  28. ColorObject = c;
  29. SolidColorBrush = new SolidColorBrush(c);
  30. }
  31. }
  32. /// <summary>
  33. /// MainWindow.xaml 的交互逻辑
  34. /// </summary>
  35. ///
  36. public partial class MainWindow : MetroWindow
  37. {
  38. private ObservableCollection<ColorInfo> SysColorList;
  39. public MainWindow()
  40. {
  41. InitializeComponent();
  42. SysColorList = new ObservableCollection<ColorInfo>();
  43. PropertyInfo[] props = typeof(Colors).GetProperties();
  44. foreach(var color in props){
  45. ColorInfo ci = new ColorInfo(color);
  46. SysColorList.Add(ci);
  47. }
  48. ColorListBox.ItemsSource = SysColorList;
  49. }
  50. private void ColorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  51. {
  52. if (ColorListBox.SelectedValue != null)
  53. {
  54. ColorInfo ci = (ColorInfo)ColorListBox.SelectedValue;
  55. ColorViewBox.Fill = ci.SolidColorBrush;
  56. vlb_ColorName.Content = ci.Name;
  57. vlb_ColorHex.Content = ColorToHex(ci.ColorObject);
  58. vlb_ColorR.Content = ci.ColorObject.R;
  59. vlb_ColorG.Content = ci.ColorObject.G;
  60. vlb_ColorB.Content = ci.ColorObject.B;
  61. vlb_ColorScR.Content = ci.ColorObject.ScR;
  62. vlb_ColorScG.Content = ci.ColorObject.ScG;
  63. vlb_ColorScB.Content = ci.ColorObject.ScB;
  64. }
  65. }
  66. private void btn_CopyColorName_Click(object sender, RoutedEventArgs e)
  67. {
  68. Clipboard.SetText(vlb_ColorName.Content.ToString(), TextDataFormat.Text);
  69. }
  70. private void btn_CopyColorHex_Click(object sender, RoutedEventArgs e)
  71. {
  72. Clipboard.SetText(vlb_ColorHex.Content.ToString(), TextDataFormat.Text);
  73. }
  74. public static string ColorToHex(Color c){
  75. return string.Format("#{0:X02}{1:X02}{2:X02}", c.R, c.G, c.B);
  76. }
  77. private void btn_CopyRGB_Click(object sender, RoutedEventArgs e)
  78. {
  79. var s = string.Format(
  80. "({0},{1},{2})",
  81. vlb_ColorR.Content.ToString(),
  82. vlb_ColorG.Content.ToString(),
  83. vlb_ColorB.Content.ToString()
  84. );
  85. }
  86. private void btn_CopyScRGB_Click(object sender, RoutedEventArgs e)
  87. {
  88. var s = string.Format(
  89. "({0},{1},{2})",
  90. vlb_ColorScR.Content.ToString(),
  91. vlb_ColorScG.Content.ToString(),
  92. vlb_ColorScB.Content.ToString()
  93. );
  94. }
  95. private void Btn_AsForeground_Click(object sender, RoutedEventArgs e)
  96. {
  97. if (ColorListBox.SelectedValue != null)
  98. {
  99. var c = (ColorInfo)ColorListBox.SelectedValue;
  100. clb_l1.Foreground = c.SolidColorBrush;
  101. clb_l2.Foreground = c.SolidColorBrush;
  102. }
  103. }
  104. private void Btn_AsBackground_Click(object sender, RoutedEventArgs e)
  105. {
  106. if (ColorListBox.SelectedValue != null)
  107. {
  108. var c = (ColorInfo)ColorListBox.SelectedValue;
  109. clb_l1.Background = c.SolidColorBrush;
  110. clb_l2.Background = c.SolidColorBrush;
  111. }
  112. }
  113. }
  114. }