-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
81 lines (77 loc) · 3.44 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Author: Leonardo Tazzini @ https://electro-logic.blogspot.com/
using Mscms;
using Mscms.WPF;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfManagedColors
{
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
void Window_Loaded(object sender, RoutedEventArgs e)
{
if (ColorHelper.IsNightLightEnabled())
{
MessageBox.Show("Detected Windows Night Light.\nPlease turn it off for accurate colors.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
Demo();
}
void Demo()
{
//BasicExample();
AdvancedExample();
}
void BasicExample()
{
// Convert a color
Color unmanaged = Color.FromRgb(248, 218, 69);
Color managed = unmanaged.ToManagedColor();
rectUnmanaged.Fill = new SolidColorBrush(unmanaged);
rectManaged.Fill = new SolidColorBrush(managed);
// Convert an image
BitmapImage unmanagedImage = new BitmapImage(new Uri("pack://application:,,,/d8m.jpg", UriKind.Absolute));
imageUnmanaged.Source = unmanagedImage;
imageManaged.Source = unmanagedImage.ToManagedImage();
}
void AdvancedExample()
{
BitmapSource unmanagedImage = new BitmapImage(new Uri("pack://application:,,,/d8m.jpg", UriKind.Absolute));
Color unmanagedColor = Color.FromRgb(248, 218, 69);
// If we need to convert multiple colors we can initialize a Color Transform once and convert multiple times.
// This performance-oriented approach is also used by the Attached Property.
// The API is similar to lcmsNET, but based on the Windows Color System (Win32 API)
using (var inputProfile = Profile.FromSRGBColorSpace())
using (var outputProfile = WpfProfileExtension.FromVisual())
using (var transform = ColorTransform.Create(inputProfile, outputProfile, RenderingIntent.Perceptual, ColorTransformFlags.PreserveBlack))
{
// Show used settings
txtMonitor.Text = _monitor.Device;
txtInputProfile.Text = Path.GetFileName(transform.InputProfile.Filename);
txtOutputProfile.Text = Path.GetFileName(transform.OutputProfile.Filename);
txtRenderingIntent.Text = transform.RenderingIntent.ToString();
txtFlags.Text = transform.Flags.ToString();
// Convert a color
Color managedColor = transform.DoTransform(unmanagedColor);
rectUnmanaged.Fill = new SolidColorBrush(unmanagedColor);
rectManaged.Fill = new SolidColorBrush(managedColor);
// Convert an sRGB image
BitmapSource managedImage = unmanagedImage.ToManagedImage(transform);
imageUnmanaged.Source = unmanagedImage;
imageManaged.Source = managedImage;
}
}
Mscms.Monitor _monitor;
void Window_LocationChanged(object sender, EventArgs e)
{
var newMonitor = WpfMonitorExtension.FromVisual();
if (newMonitor != _monitor)
{
// Window has moved to another monitor, update colors
_monitor = newMonitor;
Demo();
}
}
}
}