-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
969bb26
commit 73e0d61
Showing
5 changed files
with
163 additions
and
35 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/SamplesApp/UITests.Shared/Microsoft_UI/ColorHelperTests.xaml
This file contains 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,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> |
51 changes: 51 additions & 0 deletions
51
src/SamplesApp/UITests.Shared/Microsoft_UI/ColorHelperTests.xaml.cs
This file contains 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,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(); | ||
} | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -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); | ||
} |