From 6d7b23fd74328f965a2ca58bed03c50b890b4c2e Mon Sep 17 00:00:00 2001 From: Laute Date: Mon, 25 Oct 2021 11:48:16 +0200 Subject: [PATCH] Added HEX2 --- .../Helpers/ColorRepresentationHelper.cs | 15 ++++++++++++++- .../ViewModels/ColorEditorViewModel.cs | 7 +++++++ .../Enumerations/ColorRepresentationType.cs | 7 ++++++- .../ViewModels/ColorPickerViewModel.cs | 7 +++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs index e7bbbf1f97..ddfb551fb7 100644 --- a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs +++ b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs @@ -46,6 +46,7 @@ namespace ColorPicker.Helpers ColorRepresentationType.RGB => ColorToRGB(color), ColorRepresentationType.CIELAB => ColorToCIELAB(color), ColorRepresentationType.CIEXYZ => ColorToCIEXYZ(color), + ColorRepresentationType.HEX2 => ColorToHex2(color), // Fall-back value, when "_userSettings.CopiedColorRepresentation.Value" is incorrect _ => ColorToHex(color), @@ -77,7 +78,7 @@ namespace ColorPicker.Helpers /// The see cref="Color"/> for the hexadecimal presentation /// A hexadecimal representation of a RGB color private static string ColorToHex(Color color) - => $"#{color.R.ToString("X2", CultureInfo.InvariantCulture)}" + => $"{color.R.ToString("X2", CultureInfo.InvariantCulture)}" + $"{color.G.ToString("X2", CultureInfo.InvariantCulture)}" + $"{color.B.ToString("X2", CultureInfo.InvariantCulture)}"; @@ -234,5 +235,17 @@ namespace ColorPicker.Helpers $", {y.ToString(CultureInfo.InvariantCulture)}" + $", {z.ToString(CultureInfo.InvariantCulture)})"; } + + /// + /// Return a hexadecimal representation of a RGB color in lowercase and # included + /// + /// The see cref="Color"/> for the hexadecimal presentation + /// A hexadecimal representation of a RGB color + private static string ColorToHex2(Color color) +#pragma warning disable CA1304 // Specify CultureInfo + => $"#{color.R.ToString("X2", CultureInfo.InvariantCulture).ToLower()}" + + $"{color.G.ToString("X2", CultureInfo.InvariantCulture).ToLower()}" + + $"{color.B.ToString("X2", CultureInfo.InvariantCulture).ToLower()}"; +#pragma warning restore CA1304 // Specify CultureInfo } } diff --git a/src/modules/colorPicker/ColorPickerUI/ViewModels/ColorEditorViewModel.cs b/src/modules/colorPicker/ColorPickerUI/ViewModels/ColorEditorViewModel.cs index 03966566b3..3b26718336 100644 --- a/src/modules/colorPicker/ColorPickerUI/ViewModels/ColorEditorViewModel.cs +++ b/src/modules/colorPicker/ColorPickerUI/ViewModels/ColorEditorViewModel.cs @@ -215,6 +215,13 @@ namespace ColorPicker.ViewModels Convert = (Color color) => { return ColorRepresentationHelper.GetStringRepresentationFromMediaColor(color, ColorRepresentationType.CIEXYZ); }, }); + _allColorRepresentations.Add( + new ColorFormatModel() + { + FormatName = ColorRepresentationType.HEX2.ToString(), + Convert = (Color color) => { return ColorRepresentationHelper.GetStringRepresentationFromMediaColor(color, ColorRepresentationType.HEX2); }, + }); + _userSettings.VisibleColorFormats.CollectionChanged += VisibleColorFormats_CollectionChanged; // Any other custom format to be added here as well that are read from settings diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/Enumerations/ColorRepresentationType.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/Enumerations/ColorRepresentationType.cs index 5b592f22a1..c50d704a80 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/Enumerations/ColorRepresentationType.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/Enumerations/ColorRepresentationType.cs @@ -12,7 +12,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Enumerations public enum ColorRepresentationType { /// - /// Color presentation as hexadecimal color value without the alpha-value (e.g. #0055FF) + /// Color presentation as hexadecimal color value without the alpha-value (e.g. 0055FF) /// HEX = 0, @@ -65,5 +65,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Enumerations /// Color presentation as CIEXYZ color space (X[0..95], Y[0..100], Z[0..109] /// CIEXYZ = 10, + + /// + /// Color presentation as hexadecimal color value without the alpha-value - lowercase and with # (e.g. #0055FF) + /// + HEX2 = 11, } } diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/ColorPickerViewModel.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/ColorPickerViewModel.cs index ba4a756734..f4d16846ca 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/ColorPickerViewModel.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/ColorPickerViewModel.cs @@ -45,7 +45,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels SelectableColorRepresentations = new Dictionary { { ColorRepresentationType.CMYK, "CMYK - cmyk(100%, 50%, 75%, 0%)" }, - { ColorRepresentationType.HEX, "HEX - #FFAA00" }, + { ColorRepresentationType.HEX, "HEX - FFAA00" }, { ColorRepresentationType.HSB, "HSB - hsb(100, 50%, 75%)" }, { ColorRepresentationType.HSI, "HSI - hsi(100, 50%, 75%)" }, { ColorRepresentationType.HSL, "HSL - hsl(100, 50%, 75%)" }, @@ -55,6 +55,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels { ColorRepresentationType.RGB, "RGB - rgb(100, 50, 75)" }, { ColorRepresentationType.CIELAB, "CIE LAB - CIELab(76, 21, 80)" }, { ColorRepresentationType.CIEXYZ, "CIE XYZ - xyz(56, 50, 7)" }, + { ColorRepresentationType.HEX2, "HEX - #ffaa00" }, }; GeneralSettingsConfig = settingsRepository.SettingsConfig; @@ -198,8 +199,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels var ncolFormatName = ColorRepresentationType.NCol.ToString(); var cielabFormatName = ColorRepresentationType.CIELAB.ToString(); var ciexyzFormatName = ColorRepresentationType.CIEXYZ.ToString(); + var hex2FormatName = ColorRepresentationType.HEX2.ToString(); - formatsUnordered.Add(new ColorFormatModel(hexFormatName, "#EF68FF", visibleFormats.ContainsKey(hexFormatName) && visibleFormats[hexFormatName])); + formatsUnordered.Add(new ColorFormatModel(hexFormatName, "EF68FF", visibleFormats.ContainsKey(hexFormatName) && visibleFormats[hexFormatName])); formatsUnordered.Add(new ColorFormatModel(rgbFormatName, "rgb(239, 104, 255)", visibleFormats.ContainsKey(rgbFormatName) && visibleFormats[rgbFormatName])); formatsUnordered.Add(new ColorFormatModel(hslFormatName, "hsl(294, 100%, 70%)", visibleFormats.ContainsKey(hslFormatName) && visibleFormats[hslFormatName])); formatsUnordered.Add(new ColorFormatModel(hsvFormatName, "hsv(294, 59%, 100%)", visibleFormats.ContainsKey(hsvFormatName) && visibleFormats[hsvFormatName])); @@ -210,6 +212,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels formatsUnordered.Add(new ColorFormatModel(ncolFormatName, "R10, 50%, 75%", visibleFormats.ContainsKey(ncolFormatName) && visibleFormats[ncolFormatName])); formatsUnordered.Add(new ColorFormatModel(cielabFormatName, "CIELab(66, 72, -52)", visibleFormats.ContainsKey(cielabFormatName) && visibleFormats[cielabFormatName])); formatsUnordered.Add(new ColorFormatModel(ciexyzFormatName, "xyz(59, 35, 98)", visibleFormats.ContainsKey(ciexyzFormatName) && visibleFormats[ciexyzFormatName])); + formatsUnordered.Add(new ColorFormatModel(hex2FormatName, "#ef68ff", visibleFormats.ContainsKey(hex2FormatName) && visibleFormats[hex2FormatName])); foreach (var storedColorFormat in _colorPickerSettings.Properties.VisibleColorFormats) {