Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
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}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
mc:Ignorable="d">
<UserControl.Resources>
<converters:DateTimeToFriendlyStringConverter x:Key="DateTimeToFriendlyStringConverter" />
<converters:HexColorToBrushConverter x:Key="HexColorToBrushConverter" />
<tkconverters:BoolToVisibilityConverter x:Name="BoolToVisibilityConverter" />
</UserControl.Resources>
<Grid ColumnSpacing="12">
Expand All @@ -25,6 +26,26 @@
Source="{x:Bind ClipboardItem.Image, Mode=OneWay}"
Stretch="UniformToFill"
Visibility="{x:Bind HasImage, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
<!-- Color preview with text -->
<Grid Visibility="{x:Bind HasColor, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Ellipse
Grid.Column="0"
Width="8"
Height="8"
Margin="8,0,8,0"
Fill="{x:Bind ClipboardItem.Content, Mode=OneWay, Converter={StaticResource HexColorToBrushConverter}}" />
<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
FontSize="10"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind ClipboardItem.Content, Mode=OneWay}"
TextWrapping="NoWrap" />
</Grid>
<!-- Text preview -->
<TextBlock
Margin="8,0,0,0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public ClipboardItem ClipboardItem

public bool HasImage => ContentImage is not null;

public bool HasText => !string.IsNullOrEmpty(ContentText) && !HasImage;
public bool HasText => !string.IsNullOrEmpty(ContentText) && !HasImage && !HasColor;

public bool HasGlyph => !HasImage && !HasText && !string.IsNullOrEmpty(IconGlyph);
public bool HasGlyph => !HasImage && !HasText && !HasColor && !string.IsNullOrEmpty(IconGlyph);

public bool HasColor => ClipboardItemHelper.IsRgbHexColor(ContentText);

public ClipboardHistoryItemPreviewControl()
{
Expand Down
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
}

return null;
}
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AdvancedPaste.Models;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel.DataTransfer;

namespace AdvancedPaste.Helpers
{
internal static class ClipboardItemHelper
internal static partial class ClipboardItemHelper
{
// Compiled regex for better performance when checking multiple clipboard items
private static readonly Regex HexColorRegex = HexColorCompiledRegex();

/// <summary>
/// Creates a ClipboardItem from current clipboard data.
/// </summary>
Expand Down Expand Up @@ -55,6 +59,31 @@ public static async Task<ClipboardItem> CreateFromCurrentClipboardAsync(
return clipboardItem;
}

/// <summary>
/// Checks if text is a valid RGB hex color (e.g., #FFBFAB or #fff).
/// </summary>
public static bool IsRgbHexColor(string text)
{
if (text == null)
{
return false;
}

string trimmedText = text.Trim();
if (trimmedText.Length > 7)
{
return false;
}

if (string.IsNullOrWhiteSpace(trimmedText))
{
return false;
}

// Match #RGB or #RRGGBB format (case-insensitive)
return HexColorRegex.IsMatch(trimmedText);
}

/// <summary>
/// Creates a BitmapImage from clipboard data.
/// </summary>
Expand All @@ -80,5 +109,8 @@ private static async Task<BitmapImage> TryCreateBitmapImageAsync(DataPackageView

return null;
}

[GeneratedRegex(@"^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$")]
private static partial Regex HexColorCompiledRegex();
}
}