| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.WixBA |
| 4 | { |
| 5 | using System; |
| 6 | using System.Windows; |
| 7 | using System.Windows.Media; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Dependency Properties associated with the main Window object. |
| 11 | /// </summary> |
| 12 | public class WindowProperties : DependencyObject |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// Dependency Property to hold the result of detecting the relative luminosity (or brightness) of a Windows background. |
| 16 | /// </summary> |
| 17 | public static readonly DependencyProperty IsLightBackgroundProperty = DependencyProperty.Register( |
| 18 | "IsLightBackground", typeof(bool), typeof(WindowProperties), new PropertyMetadata( false )); |
| 19 | |
| 20 | private static Lazy<WindowProperties> _instance = new Lazy<WindowProperties>(() => |
| 21 | { |
| 22 | WindowProperties wp = new WindowProperties(); |
| 23 | wp.CheckBackgroundBrightness(); |
| 24 | return wp; |
| 25 | }); |
| 26 | |
| 27 | public static WindowProperties Instance |
| 28 | { |
| 29 | get |
| 30 | { |
| 31 | return _instance.Value; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public bool IsLightBackground |
| 37 | { |
| 38 | get { return (bool)GetValue(IsLightBackgroundProperty); } |
| 39 | private set { SetValue(IsLightBackgroundProperty, value); } |
| 40 | } |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Use the Luminosity parameter of the background color to detect light vs dark theme settings. |
| 44 | /// </summary> |
| 45 | /// <remarks> |
| 46 | /// This approach detects both the common High Contrast themes (White vs Black) and custom themes which may have relatively lighter backgrounds. |
| 47 | /// </remarks> |
| 48 | public void CheckBackgroundBrightness() |
| 49 | { |
| 50 | SolidColorBrush windowbrush = System.Windows.SystemColors.WindowBrush; |
| 51 | System.Drawing.Color dcolor = System.Drawing.Color.FromArgb(windowbrush.Color.A, windowbrush.Color.R, windowbrush.Color.G, windowbrush.Color.B); |
| 52 | |
| 53 | var brightness = dcolor.GetBrightness(); |
| 54 | // Test for 'Lightness' at an arbitrary point, approaching 1.0 (White). |
| 55 | if (0.7 < brightness) |
| 56 | { |
| 57 | this.IsLightBackground = true; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | this.IsLightBackground = false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |