Skip to content

Instantly share code, notes, and snippets.

@usagirei
Created October 15, 2015 21:23
Show Gist options
  • Save usagirei/a2cfe768e4cf5fb34694 to your computer and use it in GitHub Desktop.
Save usagirei/a2cfe768e4cf5fb34694 to your computer and use it in GitHub Desktop.
MahApps Metro DWM Colored Accent
/*
* Provides DWM Color as a Bindable Property
* Add the Namespace
* xmlns:dwmAero="clr-namespace:MahApps.Metro.DWMAero"
* And a Resource
* <dwmAero:AeroColorProvider x:Key="AeroColorProvider" />
* Then Bind on the Properties (110%,95%,80%,65%,50% Respectively)
* Color="{Binding Source={StaticResource AeroColorProvider}, Path=Highlight}"
* Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent}"
* Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent2}"
* Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent3}"
* Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent4}"
* You may add More Properties and change the color intensity
*/
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.Win32;
namespace MahApps.Metro.DWMAero
{
public sealed class AeroColorProvider : DependencyObject, INotifyPropertyChanged, IDisposable
{
public static readonly DependencyProperty Accent1Property = DependencyProperty.Register
(nameof(Accent), typeof(Color), typeof(AeroColorProvider), new PropertyMetadata(Color.FromRgb(204, 204, 204)));
public static readonly DependencyProperty Accent2Property = DependencyProperty.Register
(nameof(Accent2), typeof(Color), typeof(AeroColorProvider), new PropertyMetadata(Color.FromRgb(152, 152, 152)));
public static readonly DependencyProperty Accent3Property = DependencyProperty.Register
(nameof(Accent3), typeof(Color), typeof(AeroColorProvider), new PropertyMetadata(Color.FromRgb(102, 102, 102)));
public static readonly DependencyProperty Accent4Property = DependencyProperty.Register
(nameof(Accent4), typeof(Color), typeof(AeroColorProvider), new PropertyMetadata(Color.FromRgb(51, 51, 51)));
public static readonly DependencyProperty HighlightProperty = DependencyProperty.Register
(nameof(Highlight), typeof(Color), typeof(AeroColorProvider), new PropertyMetadata(Color.FromRgb(255, 255, 255)));
private readonly HwndSource _windowHandle;
public Color Accent
{
get { return (Color) GetValue(Accent1Property); }
set
{
SetValue(Accent1Property, value);
OnPropertyChanged(nameof(Accent));
}
}
public Color Accent2
{
get { return (Color) GetValue(Accent2Property); }
set
{
SetValue(Accent2Property, value);
OnPropertyChanged(nameof(Accent2));
}
}
public Color Accent3
{
get { return (Color) GetValue(Accent3Property); }
set
{
SetValue(Accent3Property, value);
OnPropertyChanged(nameof(Accent3));
}
}
public Color Accent4
{
get { return (Color) GetValue(Accent4Property); }
set
{
SetValue(Accent4Property, value);
OnPropertyChanged(nameof(Accent4));
}
}
public Color Highlight
{
get { return (Color) GetValue(HighlightProperty); }
set
{
SetValue(HighlightProperty, value);
OnPropertyChanged(nameof(Highlight));
}
}
/// <summary>
/// Constructor
/// </summary>
public AeroColorProvider()
{
_windowHandle = new HwndSource(0, 0, 0, 0, 0, 0, 0, "HWND_DWMHOOK", IntPtr.Zero);
_windowHandle.AddHook(WndProc);
GetAeroColor();
}
private static byte ClipByte(float input)
{
return (byte) ((input < 0 ? 0 : input > 1 ? 1 : input) * 255);
}
private static Color Intensity(Color input, float power)
{
var newR = (input.R / 255f) * power;
var newG = (input.G / 255f) * power;
var newB = (input.B / 255f) * power;
return Color.FromRgb(ClipByte(newR), ClipByte(newG), ClipByte(newB));
}
public void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void GetAeroColor()
{
int argbVal = (int) Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", 0);
byte[] bytes = BitConverter.GetBytes(argbVal);
float a = bytes[3] / 255.0f;
float r = bytes[2] / 255.0f;
float g = bytes[1] / 255.0f;
float b = bytes[0] / 255.0f;
r = ((1 - a)) + (r * (a));
g = ((1 - a)) + (g * (a));
b = ((1 - a)) + (b * (a));
Color aero = Color.FromRgb((byte) (r * 255), (byte) (g * 255), (byte) (b * 255));
Highlight = Intensity(aero, 1.1f);
Accent = Intensity(aero, 0.95f);
Accent2 = Intensity(aero, 0.8f);
Accent3 = Intensity(aero, 0.65f);
Accent4 = Intensity(aero, 0.5f);
//OnPropertyChanged();
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x320)
{
GetAeroColor();
handled = true;
}
return IntPtr.Zero;
}
public void Dispose()
{
_windowHandle.Dispose();
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
<!-- Styles\Accents\DWMAero.xaml
Provides DWM Colored Accents for a MahApps Metro Window
Attach on a Shared Dictionary alongside the Base Theme
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro.DWMAero;component/Styles/Accents/DWMAero.xaml" />
-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dwmAero="clr-namespace:MahApps.Metro.DWMAero">
<dwmAero:AeroColorProvider x:Key="AeroColorProvider" />
<SolidColorBrush x:Key="HighlightBrush" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Highlight}" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent}"/>
<SolidColorBrush x:Key="AccentColorBrush2" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent2}"/>
<SolidColorBrush x:Key="AccentColorBrush3" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent3}"/>
<SolidColorBrush x:Key="AccentColorBrush4" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent4}"/>
<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent}" />
<SolidColorBrush x:Key="AccentSelectedColorBrush" Color="White" />
<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5">
<GradientStop Color="{Binding Source={StaticResource AeroColorProvider}, Path=Highlight}" Offset="0" />
<GradientStop Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent3}" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="CheckmarkFill" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent}" />
<SolidColorBrush x:Key="RightArrowFill" Color="{Binding Source={StaticResource AeroColorProvider}, Path=Accent}" />
<SolidColorBrush x:Key="IdealForegroundColorBrush" Color="Black" />
</ResourceDictionary>
The MIT License
Copyright (c) 2015 Usagirei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment