Skip to content

Commit

Permalink
feat: Implement ColorHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
morning4coffe-dev committed Aug 25, 2024
1 parent 969bb26 commit 73e0d61
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 35 deletions.
13 changes: 13 additions & 0 deletions src/SamplesApp/UITests.Shared/Microsoft_UI/ColorHelperTests.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<UserControl x:Class="UITests.Shared.Microsoft_UI.ColorHelperTests"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<StackPanel Spacing="20"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBox Text="{Binding ColorValue, Mode=TwoWay}" />
<Button Content="Convert"
Command="{Binding ConvertCommand}"/>
<TextBlock Text="{Binding ColorName}" />
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Drawing;
using Microsoft.UI;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Private.Infrastructure;
using Uno.UI.Samples.Controls;
using Uno.UI.Samples.UITests.Helpers;
using Windows.Devices.Sensors;

namespace UITests.Shared.Microsoft_UI;

[SampleControlInfo("Microsoft.UI", "ColorHelper", description: "Demonstrates use of Microsoft.UI.ColorHelper", viewModelType: typeof(ColorHelperTestsViewModel), ignoreInSnapshotTests: true)]
public sealed partial class ColorHelperTests : UserControl
{
public ColorHelperTests()
{
this.InitializeComponent();
}
}

[Bindable]
internal class ColorHelperTestsViewModel(UnitTestDispatcherCompat dispatcher) : ViewModelBase(dispatcher)
{
private string _colorName;
private string _colorValue = "6f2526";

public string ColorValue
{
get => _colorValue;
set
{
_colorValue = value;
RaisePropertyChanged();
}
}

public Command ConvertCommand => new((p) =>
{
ColorName = ColorHelper.ToDisplayName(ColorHelper.ConvertColorFromHexString(ColorValue));
});

public string ColorName
{
get => _colorName;
private set
{
_colorName = value;
RaisePropertyChanged();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,10 @@ public string ColorString

private void OnValuesChanged()
{
Color color = ConvertColorFromHexString(ColorString);
Color color = Microsoft.UI.ColorHelper.ConvertColorFromHexString(ColorString);
ElevatedElement.ShadowColor = color;
ElevatedElement.Elevation = Elevation;
ElevatedElement.CornerRadius = new CornerRadius(Radius);
}

private Color ConvertColorFromHexString(string colorString)
{
try
{
//Target hex string
colorString = colorString.Replace("#", string.Empty);
// from #RRGGBB string
var a = (byte)System.Convert.ToUInt32(colorString.Substring(0, 2), 16);
var r = (byte)System.Convert.ToUInt32(colorString.Substring(2, 2), 16);
var g = (byte)System.Convert.ToUInt32(colorString.Substring(4, 2), 16);
var b = (byte)System.Convert.ToUInt32(colorString.Substring(6, 2), 16);
//get the color
return Color.FromArgb(a, r, g, b);
}
catch
{
return Colors.Black;
}
}
}
}
8 changes: 8 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<Import_RootNamespace>UITests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI\ColorHelperTests.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Lottie\LottieEmbeddedJson.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5448,6 +5452,9 @@
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI\ColorHelperTests.xaml.cs">
<DependentUpon>ColorHelperTests.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindableBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\IWaitableSample.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\UWPViewHelper.cs" />
Expand Down Expand Up @@ -9736,6 +9743,7 @@
<PRIResource Include="$(MSBuildThisFileDirectory)UITestsStrings\en-US\Resources.resw" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Microsoft_UI\" />
<Folder Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ChatBox\" />
<Folder Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\DatePicker\Models\" />
<Folder Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\PersonPictureTests\" />
Expand Down
104 changes: 90 additions & 14 deletions src/Uno.UI/UI/ColorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,102 @@
using System;
using System.ComponentModel;
using Microsoft.UI;
using System.Linq;
using DirectUI;
using WindowsColor = Windows/*Intentional space for WinUI upgrade tool*/.UI.Color;

using Color = global::Windows/*Intentional space for WinUI upgrade tool*/.UI.Color;
namespace Microsoft.UI;

namespace Microsoft.UI
public static partial class ColorHelper
{
public static partial class ColorHelper
/// <summary>
/// Retrieves the display name of the specified color.
/// </summary>
/// <param name="color">The color to get the name for.</param>
/// <returns>The localized display name of the color.</returns>
public static string ToDisplayName(WindowsColor color)
{
#if __ANDROID__ || __IOS__ || IS_UNIT_TESTS || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static string ToDisplayName(Color color)
//uno specific

var closestKnownColor = Enum.GetValues<KnownColors>()
.OrderBy(c => ColorDistance(new WindowsColor((uint)c), color))
.First();

var resourceId = closestKnownColor.ToString();
var resource = DXamlCore.Current.GetLocalizedResourceString(resourceId);

if (string.IsNullOrEmpty(resource))
{
throw new global::System.NotImplementedException("The member string ColorHelper.ToDisplayName(Color color) is not implemented in Uno.");
return closestKnownColor.ToString();
}
#endif

[EditorBrowsable(EditorBrowsableState.Never)]
public static Color FromARGB(byte a, byte r, byte g, byte b)
=> Color.FromArgb(a, r, g, b);
return resource;
}

//uno specific
static double ColorDistance(WindowsColor color1, WindowsColor color2)
{
var r1 = (int)(color1.R);
var g1 = (int)(color1.G);
var b1 = (int)(color1.B);
var r2 = (int)(color2.R);
var g2 = (int)(color2.G);
var b2 = (int)(color2.B);

return Math.Sqrt(Math.Pow(r1 - r2, 2) + Math.Pow(g1 - g2, 2) + Math.Pow(b1 - b2, 2));
}

/// <summary>
/// Converts a color string in hex format to a Windows.UI.Color.
/// </summary>
internal static WindowsColor ConvertColorFromHexString(string colorString)
{
try
{
colorString = colorString.Replace("#", string.Empty);

byte a = 255;
byte r, g, b;

public static Color FromArgb(byte a, byte r, byte g, byte b)
=> Color.FromArgb(a, r, g, b);
if (colorString.Length == 6)
{
// #RRGGBB format
r = (byte)Convert.ToUInt32(colorString.Substring(0, 2), 16);
g = (byte)Convert.ToUInt32(colorString.Substring(2, 2), 16);
b = (byte)Convert.ToUInt32(colorString.Substring(4, 2), 16);
}
else if (colorString.Length == 8)
{
// #AARRGGBB format
a = (byte)Convert.ToUInt32(colorString.Substring(0, 2), 16);
r = (byte)Convert.ToUInt32(colorString.Substring(2, 2), 16);
g = (byte)Convert.ToUInt32(colorString.Substring(4, 2), 16);
b = (byte)Convert.ToUInt32(colorString.Substring(6, 2), 16);
}
else
{
return Colors.Black;
}

return WindowsColor.FromArgb(a, r, g, b);
}
catch
{
return Colors.Black;
}
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static WindowsColor FromARGB(byte a, byte r, byte g, byte b)
=> WindowsColor.FromArgb(a, r, g, b);

/// <summary>
/// Generates a Color structure, based on discrete Byte values for ARGB components. C# and Microsoft Visual Basic code should use Color.FromArgb instead.
/// </summary>
/// <param name="a">The A (transparency) component of the desired color. Range is 0-255.</param>
/// <param name="r">The R component of the desired color. Range is 0-255.</param>
/// <param name="g">The G component of the desired color. Range is 0-255.</param>
/// <param name="b">The B component of the desired color. Range is 0-255.</param>
/// <returns>The generated Color value.</returns>
public static WindowsColor FromArgb(byte a, byte r, byte g, byte b)
=> WindowsColor.FromArgb(a, r, g, b);
}

0 comments on commit 73e0d61

Please sign in to comment.