From 3e1b07f52c955ec733db76249dce2ee5d766f1e2 Mon Sep 17 00:00:00 2001 From: Ruthie Sun <90534270+ruthiesun@users.noreply.github.com> Date: Fri, 27 Feb 2026 14:12:58 -0800 Subject: [PATCH] Color picker - Lab format: use roundoff optional #13603 (#42986) ## Summary of the Pull Request The default CIELab format rounds the values to two decimal places, which is a degree of precision that isn't always needed. This PR adds an optional formatting character (i) to the three CIELab format parameters, which rounds the value to the nearest integer. ## PR Checklist - [x] Closes: #13603/#14863. Note that in the discussion for #13603, there are some additional suggestions that this PR doesn't address. - [ ] **Communication:** Haven't gotten the green light for this approach with the core contributors yet. Happy to pivot to a different approach if needed. - [ ] **Tests:** Added/updated and all pass - [x] **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 ## Detailed Description of the Pull Request / Additional comments In the case where a or b get rounded to -0 (e.g. -0.0001 rounds to -0), the negative sign gets removed. However, I noticed during testing that the default format (rounding to two decimal places) retains the negative sign in these situations (see third screenshot). I can a) revert to keeping the -0 for the new rounding behavior, b) change -0 to 0 for other rounded values, or c) leave it as-is. Also open to suggestions. I can update the docs as well, if we're happy with the approach. ## Validation Steps Performed - Settings: Can change the default CIELab format to display rounded values - Settings: Can create a custom format with rounded CIELab values - Color Picker: Rounded values are displayed when hovering and as a saved color powertoys-cielab powertoys-cielab-selected powertoys-cielab-selected-0 --------- Co-authored-by: vanzue --- src/common/ManagedCommon/ColorFormatHelper.cs | 27 ++++++++++++++----- .../Controls/ColorFormatEditor.xaml | 14 +++++++++- .../Controls/ColorFormatEditor.xaml.cs | 7 ++++- .../Settings.UI/Strings/en-us/Resources.resw | 8 +++++- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/common/ManagedCommon/ColorFormatHelper.cs b/src/common/ManagedCommon/ColorFormatHelper.cs index 471104f215..d353e52018 100644 --- a/src/common/ManagedCommon/ColorFormatHelper.cs +++ b/src/common/ManagedCommon/ColorFormatHelper.cs @@ -515,8 +515,7 @@ namespace ManagedCommon return lightnessL.ToString(CultureInfo.InvariantCulture); case "Lc": var (lightnessC, _, _) = ConvertToCIELABColor(color); - lightnessC = Math.Round(lightnessC, 2); - return lightnessC.ToString(CultureInfo.InvariantCulture); + return ColorPercentFormatted(lightnessC, paramFormat, 2); case "Lo": var (lightnessO, _, _) = ConvertToOklabColor(color); lightnessO = Math.Round(lightnessO, 2); @@ -531,12 +530,10 @@ namespace ManagedCommon return blackness.ToString(CultureInfo.InvariantCulture); case "Ca": var (_, chromaticityA, _) = ConvertToCIELABColor(color); - chromaticityA = Math.Round(chromaticityA, 2); - return chromaticityA.ToString(CultureInfo.InvariantCulture); + return ColorPercentFormatted(chromaticityA, paramFormat, 2); case "Cb": var (_, _, chromaticityB) = ConvertToCIELABColor(color); - chromaticityB = Math.Round(chromaticityB, 2); - return chromaticityB.ToString(CultureInfo.InvariantCulture); + return ColorPercentFormatted(chromaticityB, paramFormat, 2); case "Oa": var (_, chromaticityAOklab, _) = ConvertToOklabColor(color); chromaticityAOklab = Math.Round(chromaticityAOklab, 2); @@ -595,6 +592,24 @@ namespace ManagedCommon } } + private static string ColorPercentFormatted(double colorPercentValue, char paramFormat, int defaultDecimalDigits) + { + switch (paramFormat) + { + case 'i': + double roundedColorPercentValue = Math.Round(colorPercentValue); + if (roundedColorPercentValue == 0) + { + // convert -0 to 0 + roundedColorPercentValue = 0.0; + } + + return roundedColorPercentValue.ToString(CultureInfo.InvariantCulture); + default: + return Math.Round(colorPercentValue, defaultDecimalDigits).ToString(CultureInfo.InvariantCulture); + } + } + public static string GetDefaultFormat(string formatName) { switch (formatName) diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml index 743ced3204..c42693107f 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml @@ -108,7 +108,7 @@ Style="{StaticResource CaptionTextBlockStyle}" /> @@ -117,6 +117,18 @@ x:Uid="ColorFormatEditorHelpline3" VerticalAlignment="Bottom" Style="{StaticResource CaptionTextBlockStyle}" /> + + + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml.cs index 475d399674..cf505a4817 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml.cs @@ -66,7 +66,7 @@ namespace Microsoft.PowerToys.Settings.UI.Controls new ColorFormatParameter() { Parameter = "%Na", Description = resourceLoader.GetString("Help_color_name") }, }; - ColorParametersItemsControl.ItemsSource = new List + RGBAColorParametersItemsControl.ItemsSource = new List { new ColorFormatParameter() { Parameter = "b", Description = resourceLoader.GetString("Help_byte") }, new ColorFormatParameter() { Parameter = "h", Description = resourceLoader.GetString("Help_hexL1") }, @@ -76,6 +76,11 @@ namespace Microsoft.PowerToys.Settings.UI.Controls new ColorFormatParameter() { Parameter = "f", Description = resourceLoader.GetString("Help_floatWith") }, new ColorFormatParameter() { Parameter = "F", Description = resourceLoader.GetString("Help_floatWithout") }, }; + + CIELabColorParametersItemsControl.ItemsSource = new List + { + new ColorFormatParameter() { Parameter = "i", Description = resourceLoader.GetString("Help_integer") }, + }; } private void NewColorName_TextChanged(object sender, TextChangedEventArgs e) diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index 7315147ddb..6b2fd3646f 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -1727,7 +1727,13 @@ Made with 💗 by Microsoft and the PowerToys community. float without leading zero - + + The lightness (CIE), chromaticity A (CIE Lab) and chromaticity B (CIE Lab) values can be formatted to the following formats: + + + rounded to the nearest integer + + Example: %ReX means red value in hex uppercase two digits format.