How to get Windows 10 accent color?

3

I'm looking for a way to get the color that windows 10 chooses automatically depending on the background image as shown below.

enter image description here

I tried searching, and found

var color = (Color)this.Resources["SystemAccentColor"];

and

var color = (Color)Application.Current.Resources["SystemAccentColor"];

but both thew an exception

System.Exception
  HResult=0x8000FFFF
  Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

c#
xaml
uwp
uwp-xaml
asked on Stack Overflow Sep 12, 2019 by master_ruko • edited Sep 12, 2019 by master_ruko

1 Answer

3

You will get only Hex Color in this code Application.Current.Resources["SystemAccentColor"] You have to convert it into usable color format, here is the solution.

        var color = Application.Current.Resources["SystemAccentColor"];
        btnTest.Background = GetColorFromHex(color.ToString());

And here is the converting function

public static SolidColorBrush GetColorFromHex(string hexaColor)
        {
            return new SolidColorBrush(
                Color.FromArgb(
                  Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16)
                )
            );
        }

Hope this helps,

answered on Stack Overflow Sep 12, 2019 by Noorul

User contributions licensed under CC BY-SA 3.0