Skip to content

Commit 3e1b07f

Browse files
ruthiesunvanzue
andauthored
Color picker - Lab format: use roundoff optional #13603 (#42986)
<!-- 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 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. <!-- Please review the items on the PR checklist before submitting--> ## 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 <!-- 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 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. <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## 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 <img width="1076" height="1520" alt="powertoys-cielab" src="https://github.com/user-attachments/assets/8a67142d-d7f4-49bc-b1ba-ad9304235218" /> <img width="447" height="390" alt="powertoys-cielab-selected" src="https://github.com/user-attachments/assets/c96d3bc9-cac7-4470-af3f-b2bce78d0915" /> <img width="445" height="389" alt="powertoys-cielab-selected-0" src="https://github.com/user-attachments/assets/c329bc5b-c18a-4f61-a808-0fa5050e09ed" /> --------- Co-authored-by: vanzue <vanzue@outlook.com>
1 parent 96e6542 commit 3e1b07f

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/common/ManagedCommon/ColorFormatHelper.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ private static string GetStringRepresentation(Color color, char paramFormat, str
515515
return lightnessL.ToString(CultureInfo.InvariantCulture);
516516
case "Lc":
517517
var (lightnessC, _, _) = ConvertToCIELABColor(color);
518-
lightnessC = Math.Round(lightnessC, 2);
519-
return lightnessC.ToString(CultureInfo.InvariantCulture);
518+
return ColorPercentFormatted(lightnessC, paramFormat, 2);
520519
case "Lo":
521520
var (lightnessO, _, _) = ConvertToOklabColor(color);
522521
lightnessO = Math.Round(lightnessO, 2);
@@ -531,12 +530,10 @@ private static string GetStringRepresentation(Color color, char paramFormat, str
531530
return blackness.ToString(CultureInfo.InvariantCulture);
532531
case "Ca":
533532
var (_, chromaticityA, _) = ConvertToCIELABColor(color);
534-
chromaticityA = Math.Round(chromaticityA, 2);
535-
return chromaticityA.ToString(CultureInfo.InvariantCulture);
533+
return ColorPercentFormatted(chromaticityA, paramFormat, 2);
536534
case "Cb":
537535
var (_, _, chromaticityB) = ConvertToCIELABColor(color);
538-
chromaticityB = Math.Round(chromaticityB, 2);
539-
return chromaticityB.ToString(CultureInfo.InvariantCulture);
536+
return ColorPercentFormatted(chromaticityB, paramFormat, 2);
540537
case "Oa":
541538
var (_, chromaticityAOklab, _) = ConvertToOklabColor(color);
542539
chromaticityAOklab = Math.Round(chromaticityAOklab, 2);
@@ -595,6 +592,24 @@ private static string ColorByteFormatted(byte colorByteValue, char paramFormat)
595592
}
596593
}
597594

595+
private static string ColorPercentFormatted(double colorPercentValue, char paramFormat, int defaultDecimalDigits)
596+
{
597+
switch (paramFormat)
598+
{
599+
case 'i':
600+
double roundedColorPercentValue = Math.Round(colorPercentValue);
601+
if (roundedColorPercentValue == 0)
602+
{
603+
// convert -0 to 0
604+
roundedColorPercentValue = 0.0;
605+
}
606+
607+
return roundedColorPercentValue.ToString(CultureInfo.InvariantCulture);
608+
default:
609+
return Math.Round(colorPercentValue, defaultDecimalDigits).ToString(CultureInfo.InvariantCulture);
610+
}
611+
}
612+
598613
public static string GetDefaultFormat(string formatName)
599614
{
600615
switch (formatName)

src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
Style="{StaticResource CaptionTextBlockStyle}" />
109109

110110
<ItemsControl
111-
x:Name="ColorParametersItemsControl"
111+
x:Name="RGBAColorParametersItemsControl"
112112
IsTabStop="False"
113113
ItemTemplate="{StaticResource ColorParameterTemplate}"
114114
ItemsPanel="{StaticResource ItemPanelTemplate}" />
@@ -117,6 +117,18 @@
117117
x:Uid="ColorFormatEditorHelpline3"
118118
VerticalAlignment="Bottom"
119119
Style="{StaticResource CaptionTextBlockStyle}" />
120+
121+
<ItemsControl
122+
x:Name="CIELabColorParametersItemsControl"
123+
IsTabStop="False"
124+
ItemTemplate="{StaticResource ColorParameterTemplate}"
125+
ItemsPanel="{StaticResource ItemPanelTemplate}" />
126+
127+
<TextBlock
128+
x:Uid="ColorFormatEditorHelpline4"
129+
VerticalAlignment="Bottom"
130+
Style="{StaticResource CaptionTextBlockStyle}" />
131+
120132
</StackPanel>
121133
</Grid>
122134
</UserControl>

src/settings-ui/Settings.UI/SettingsXAML/Controls/ColorFormatEditor.xaml.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void LoadParameters()
6666
new ColorFormatParameter() { Parameter = "%Na", Description = resourceLoader.GetString("Help_color_name") },
6767
};
6868

69-
ColorParametersItemsControl.ItemsSource = new List<ColorFormatParameter>
69+
RGBAColorParametersItemsControl.ItemsSource = new List<ColorFormatParameter>
7070
{
7171
new ColorFormatParameter() { Parameter = "b", Description = resourceLoader.GetString("Help_byte") },
7272
new ColorFormatParameter() { Parameter = "h", Description = resourceLoader.GetString("Help_hexL1") },
@@ -76,6 +76,11 @@ public void LoadParameters()
7676
new ColorFormatParameter() { Parameter = "f", Description = resourceLoader.GetString("Help_floatWith") },
7777
new ColorFormatParameter() { Parameter = "F", Description = resourceLoader.GetString("Help_floatWithout") },
7878
};
79+
80+
CIELabColorParametersItemsControl.ItemsSource = new List<ColorFormatParameter>
81+
{
82+
new ColorFormatParameter() { Parameter = "i", Description = resourceLoader.GetString("Help_integer") },
83+
};
7984
}
8085

8186
private void NewColorName_TextChanged(object sender, TextChangedEventArgs e)

src/settings-ui/Settings.UI/Strings/en-us/Resources.resw

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,13 @@ Made with 💗 by Microsoft and the PowerToys community.</value>
17271727
<data name="Help_floatWithout" xml:space="preserve">
17281728
<value>float without leading zero</value>
17291729
</data>
1730-
<data name="ColorFormatEditorHelpline3.Text" xml:space="preserve">
1730+
<data name="ColorFormatEditorHelpline3.Text" xml:space="preserve">
1731+
<value>The lightness (CIE), chromaticity A (CIE Lab) and chromaticity B (CIE Lab) values can be formatted to the following formats:</value>
1732+
</data>
1733+
<data name="Help_integer" xml:space="preserve">
1734+
<value>rounded to the nearest integer</value>
1735+
</data>
1736+
<data name="ColorFormatEditorHelpline4.Text" xml:space="preserve">
17311737
<value>Example: %ReX means red value in hex uppercase two digits format.</value>
17321738
</data>
17331739
<data name="ColorPicker_ShowColorName.Header" xml:space="preserve">

0 commit comments

Comments
 (0)