123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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);
- }
- }
-
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- ///
- public partial class MainWindow : MetroWindow
- {
- private ObservableCollection<ColorInfo> SysColorList;
- public MainWindow()
- {
- InitializeComponent();
- SysColorList = new ObservableCollection<ColorInfo>();
- 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;
- }
- }
- }
- }
|