-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Add RGB hex color preview to Advanced Paste clipboard history #43990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crramirez
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
crramirez:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80e6ebd
Add RGB hex color preview to Advanced Paste clipboard history (#1)
Copilot 278e785
Update src/modules/AdvancedPaste/AdvancedPaste/Helpers/ClipboardItemH…
crramirez 7bb08ca
Update src/modules/AdvancedPaste/AdvancedPaste.UnitTests/ConvertersTe…
crramirez ecd9aae
Fix spacing in XAML file for `ClipboardHistoryItemPreviewControl` to …
crramirez 9c9dfe8
Remove unnecessary blank line in `HexColorToColorConverterTests` to i…
crramirez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...es/AdvancedPaste/AdvancedPaste.UnitTests/ConvertersTests/HexColorToColorConverterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // 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 AdvancedPaste.Converters; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Windows.UI; | ||
|
|
||
| namespace AdvancedPaste.UnitTests.ConvertersTests; | ||
|
|
||
| [TestClass] | ||
| public sealed class HexColorToColorConverterTests | ||
| { | ||
| [TestMethod] | ||
| public void TestConvert_ValidSixDigitHex_ReturnsColor() | ||
| { | ||
| Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#FFBFAB"); | ||
| Assert.IsNotNull(result); | ||
|
|
||
| var color = (Windows.UI.Color)result; | ||
| Assert.AreEqual(255, color.R); | ||
| Assert.AreEqual(191, color.G); | ||
| Assert.AreEqual(171, color.B); | ||
| Assert.AreEqual(255, color.A); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestConvert_ValidThreeDigitHex_ReturnsColor() | ||
| { | ||
| Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#abc"); | ||
| Assert.IsNotNull(result); | ||
|
|
||
| var color = (Windows.UI.Color)result; | ||
|
|
||
| // #abc should expand to #aabbcc | ||
| Assert.AreEqual(170, color.R); // 0xaa | ||
| Assert.AreEqual(187, color.G); // 0xbb | ||
| Assert.AreEqual(204, color.B); // 0xcc | ||
| Assert.AreEqual(255, color.A); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestConvert_NullOrEmpty_ReturnsNull() | ||
| { | ||
| Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(null)); | ||
| Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(string.Empty)); | ||
| Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(" ")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestConvert_InvalidHex_ReturnsNull() | ||
| { | ||
| Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#GGGGGG")); | ||
| Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#12345")); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/modules/AdvancedPaste/AdvancedPaste.UnitTests/HelpersTests/ClipboardItemHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // 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 AdvancedPaste.Helpers; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace AdvancedPaste.UnitTests.HelpersTests; | ||
|
|
||
| [TestClass] | ||
| public sealed class ClipboardItemHelperTests | ||
| { | ||
| [TestMethod] | ||
| [DataRow("#FFBFAB", true)] | ||
| [DataRow("#000000", true)] | ||
| [DataRow("#FFFFFF", true)] | ||
| [DataRow("#fff", true)] | ||
| [DataRow("#abc", true)] | ||
| [DataRow("#123456", true)] | ||
| [DataRow("#AbCdEf", true)] | ||
| [DataRow("FFBFAB", false)] // Missing # | ||
| [DataRow("#GGGGGG", false)] // Invalid hex characters | ||
| [DataRow("#12345", false)] // Wrong length | ||
| [DataRow("#1234567", false)] // Too long | ||
| [DataRow("", false)] | ||
| [DataRow(null, false)] | ||
| [DataRow(" #FFF ", true)] // Whitespace should be trimmed | ||
| [DataRow("Not a color", false)] | ||
| [DataRow("#", false)] | ||
| [DataRow("##FFFFFF", false)] | ||
| public void TestIsRgbHexColor(string input, bool expected) | ||
| { | ||
| bool result = ClipboardItemHelper.IsRgbHexColor(input); | ||
| Assert.AreEqual(expected, result, $"IsRgbHexColor(\"{input}\") should return {expected}"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...dules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Converters/HexColorConverterHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // 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. | ||
|
|
||
| namespace AdvancedPaste.Converters | ||
| { | ||
| public static class HexColorConverterHelper | ||
| { | ||
| public static Windows.UI.Color? ConvertHexColorToRgb(string hexColor) | ||
| { | ||
| try | ||
| { | ||
| // Remove # if present | ||
| var cleanHex = hexColor.TrimStart('#'); | ||
|
|
||
| // Expand 3-digit hex to 6-digit (#ABC -> #AABBCC) | ||
| if (cleanHex.Length == 3) | ||
| { | ||
| cleanHex = $"{cleanHex[0]}{cleanHex[0]}{cleanHex[1]}{cleanHex[1]}{cleanHex[2]}{cleanHex[2]}"; | ||
| } | ||
|
|
||
| if (cleanHex.Length == 6) | ||
| { | ||
| var r = System.Convert.ToByte(cleanHex.Substring(0, 2), 16); | ||
| var g = System.Convert.ToByte(cleanHex.Substring(2, 2), 16); | ||
| var b = System.Convert.ToByte(cleanHex.Substring(4, 2), 16); | ||
|
|
||
| return Windows.UI.Color.FromArgb(255, r, g, b); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Invalid color format - return null | ||
| } | ||
crramirez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
...ules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Converters/HexColorToBrushConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // 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 System; | ||
| using Microsoft.UI.Xaml.Data; | ||
| using Microsoft.UI.Xaml.Media; | ||
|
|
||
| namespace AdvancedPaste.Converters | ||
| { | ||
| public sealed partial class HexColorToBrushConverter : IValueConverter | ||
| { | ||
| public object ConvertBack(object value, Type targetType, object parameter, string language) | ||
| => throw new NotSupportedException(); | ||
|
|
||
| public object Convert(object value, Type targetType, object parameter, string language) | ||
| { | ||
| if (value is not string hexColor || string.IsNullOrWhiteSpace(hexColor)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| Windows.UI.Color? color = HexColorConverterHelper.ConvertHexColorToRgb(hexColor); | ||
|
|
||
| return color != null ? new SolidColorBrush((Windows.UI.Color)color) : null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.