using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using MahApps.Metro.Controls; using System.Reflection; using System.Collections.ObjectModel; namespace WPFColorAssiant { public class ColorInfo { public Brush SolidColorBrush { get; set; } public Color ColorObject; public string Name { get; set; } public ColorInfo(PropertyInfo pi) { Name = pi.Name; var c = (Color)pi.GetValue(null, null); ColorObject = c; SolidColorBrush = new SolidColorBrush(c); } } /// /// MainWindow.xaml 的交互逻辑 /// /// public partial class MainWindow : MetroWindow { private ObservableCollection SysColorList; public MainWindow() { InitializeComponent(); SysColorList = new ObservableCollection(); PropertyInfo[] props = typeof(Colors).GetProperties(); foreach(var color in props){ ColorInfo ci = new ColorInfo(color); SysColorList.Add(ci); } ColorListBox.ItemsSource = SysColorList; } private void ColorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (ColorListBox.SelectedValue != null) { ColorInfo ci = (ColorInfo)ColorListBox.SelectedValue; ColorViewBox.Fill = ci.SolidColorBrush; vlb_ColorName.Content = ci.Name; vlb_ColorHex.Content = ColorToHex(ci.ColorObject); vlb_ColorR.Content = ci.ColorObject.R; vlb_ColorG.Content = ci.ColorObject.G; vlb_ColorB.Content = ci.ColorObject.B; vlb_ColorScR.Content = ci.ColorObject.ScR; vlb_ColorScG.Content = ci.ColorObject.ScG; vlb_ColorScB.Content = ci.ColorObject.ScB; } } private void btn_CopyColorName_Click(object sender, RoutedEventArgs e) { Clipboard.SetText(vlb_ColorName.Content.ToString(), TextDataFormat.Text); } private void btn_CopyColorHex_Click(object sender, RoutedEventArgs e) { Clipboard.SetText(vlb_ColorHex.Content.ToString(), TextDataFormat.Text); } public static string ColorToHex(Color c){ return string.Format("#{0:X02}{1:X02}{2:X02}", c.R, c.G, c.B); } private void btn_CopyRGB_Click(object sender, RoutedEventArgs e) { var s = string.Format( "({0},{1},{2})", vlb_ColorR.Content.ToString(), vlb_ColorG.Content.ToString(), vlb_ColorB.Content.ToString() ); } private void btn_CopyScRGB_Click(object sender, RoutedEventArgs e) { var s = string.Format( "({0},{1},{2})", vlb_ColorScR.Content.ToString(), vlb_ColorScG.Content.ToString(), vlb_ColorScB.Content.ToString() ); } private void Btn_AsForeground_Click(object sender, RoutedEventArgs e) { if (ColorListBox.SelectedValue != null) { var c = (ColorInfo)ColorListBox.SelectedValue; clb_l1.Foreground = c.SolidColorBrush; clb_l2.Foreground = c.SolidColorBrush; } } private void Btn_AsBackground_Click(object sender, RoutedEventArgs e) { if (ColorListBox.SelectedValue != null) { var c = (ColorInfo)ColorListBox.SelectedValue; clb_l1.Background = c.SolidColorBrush; clb_l2.Background = c.SolidColorBrush; } } } }