Files
PowerToys/src/modules/cmdpal/Microsoft.CmdPal.UI/Converters/ContrastBrushConverter.cs

122 lines
4.2 KiB
C#
Raw Normal View History

CmdPal: Light, dark, pink, and unicorns (#43505) <!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This PR introduces user settings for app mode themes (dark, light, or system) and background customization options, including custom colors, system accent colors, or custom images. - Adds a new page to the Settings window with new appearance settings and moves some existing settings there as well. - Introduces a new core-level service abstraction, `IThemeService`, that holds the state for the current theme. - Uses the helper class `ResourceSwapper` to update application-level XAML resources. The way WinUI / XAML handles these is painful, and XAML Hot Reload is pain². Initialization must be lazy, as XAML resources can only be accessed after the window is activated. - `ThemeService` takes app and system settings and selects one of the registered `IThemeProvider`s to calculate visuals and choose the appropriate XAML resources. - At the moment, there are two: - `NormalThemeProvider` - Provides the current uncolorized light and dark styles - `ms-appx:///Styles/Theme.Normal.xaml` - `ColorfulThemeProvider` - Style that matches the Windows 11 visual style (based on the Start menu) and colors - `ms-appx:///Styles/Theme.Colorful.xaml` - Applied when the background is colorized or a background image is selected - The app theme is applied only on the main window (`WindowThemeSynchronizer` helper class can be used to synchronize other windows if needed). - Adds a new dependency on `Microsoft.Graphics.Win2D`. - Adds a custom color picker popup; the one from the Community Toolkit occasionally loses the selected color. - Flyby: separates the keyword tag and localizable label for pages in the Settings window navigation. ## Pictures? Pictures! <img width="2027" height="1276" alt="image" src="https://github.com/user-attachments/assets/e3485c71-7faa-495b-b455-b313ea6046ee" /> <img width="3776" height="2025" alt="image" src="https://github.com/user-attachments/assets/820fa823-34d4-426d-b066-b1049dc3266f" /> Matching Windows accent color and tint: <img width="3840" height="2160" alt="image" src="https://github.com/user-attachments/assets/65f3b608-e282-4894-b7c8-e014a194f11f" /> <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #38444 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Niels Laute <niels.laute@live.nl>
2025-12-10 01:56:03 +01:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
namespace Microsoft.CmdPal.UI.Converters;
/// <summary>
/// Gets a color, either black or white, depending on the brightness of the supplied color.
/// </summary>
public sealed partial class ContrastBrushConverter : IValueConverter
{
/// <summary>
/// Gets or sets the alpha channel threshold below which a default color is used instead of black/white.
/// </summary>
public byte AlphaThreshold { get; set; } = 128;
/// <inheritdoc />
public object Convert(
object value,
Type targetType,
object parameter,
string language)
{
Color comparisonColor;
Color? defaultColor = null;
// Get the changing color to compare against
if (value is Color valueColor)
{
comparisonColor = valueColor;
}
else if (value is SolidColorBrush valueBrush)
{
comparisonColor = valueBrush.Color;
}
else
{
// Invalid color value provided
return DependencyProperty.UnsetValue;
}
// Get the default color when transparency is high
if (parameter is Color parameterColor)
{
defaultColor = parameterColor;
}
else if (parameter is SolidColorBrush parameterBrush)
{
defaultColor = parameterBrush.Color;
}
if (comparisonColor.A < AlphaThreshold &&
defaultColor.HasValue)
{
// If the transparency is less than 50 %, just use the default brush
// This can commonly be something like the TextControlForeground brush
return new SolidColorBrush(defaultColor.Value);
}
else
{
// Chose a white/black brush based on contrast to the base color
return UseLightContrastColor(comparisonColor)
? new SolidColorBrush(Colors.White)
: new SolidColorBrush(Colors.Black);
}
}
/// <inheritdoc />
public object ConvertBack(
object value,
Type targetType,
object parameter,
string language)
{
return DependencyProperty.UnsetValue;
}
/// <summary>
/// Determines whether a light or dark contrast color should be used with the given displayed color.
/// </summary>
/// <remarks>
/// This code is using the WinUI algorithm.
/// </remarks>
private bool UseLightContrastColor(Color displayedColor)
{
// The selection ellipse should be light if and only if the chosen color
// contrasts more with black than it does with white.
// To find how much something contrasts with white, we use the equation
// for relative luminance, which is given by
//
// L = 0.2126 * Rg + 0.7152 * Gg + 0.0722 * Bg
//
// where Xg = { X/3294 if X <= 10, (R/269 + 0.0513)^2.4 otherwise }
//
// If L is closer to 1, then the color is closer to white; if it is closer to 0,
// then the color is closer to black. This is based on the fact that the human
// eye perceives green to be much brighter than red, which in turn is perceived to be
// brighter than blue.
//
// If the third dimension is value, then we won't be updating the spectrum's displayed colors,
// so in that case we should use a value of 1 when considering the backdrop
// for the selection ellipse.
var rg = displayedColor.R <= 10
? displayedColor.R / 3294.0
: Math.Pow((displayedColor.R / 269.0) + 0.0513, 2.4);
var gg = displayedColor.G <= 10
? displayedColor.G / 3294.0
: Math.Pow((displayedColor.G / 269.0) + 0.0513, 2.4);
var bg = displayedColor.B <= 10
? displayedColor.B / 3294.0
: Math.Pow((displayedColor.B / 269.0) + 0.0513, 2.4);
return (0.2126 * rg) + (0.7152 * gg) + (0.0722 * bg) <= 0.5;
}
}