Skip to content

Commit

Permalink
feat: Implement GetColorNameResourceId
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed605 committed Sep 2, 2024
1 parent 5e64bca commit d494270
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 0 deletions.
343 changes: 343 additions & 0 deletions src/Uno.UI/UI/ColorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,347 @@ public static WindowsColor FromARGB(byte a, byte r, byte g, byte b)
/// <returns>The generated Color value.</returns>
public static WindowsColor FromArgb(byte a, byte r, byte g, byte b)
=> WindowsColor.FromArgb(a, r, g, b);

#region Resource IDs Lookup Tables
private static ReadOnlySpan<int> HueLimitsForSatLevel4 =>
[
0,
11,
26,
0,
0,
38,
45,
0,
0,
56,
100,
121,
129,
0,
140,
0,
180,
0,
0,
224,
241,
0,
256
];

private static ReadOnlySpan<int> HueLimitsForSatLevel5 =>
[
0,
13,
27,
0,
0,
36,
45,
0,
0,
59,
118,
0,
127,
136,
142,
0,
185,
0,
0,
216,
239,
0,
256
];

private static ReadOnlySpan<int> HueLimitsForSatLevel3 =>
[
0,
8,
0,
0,
39,
46,
0,
0,
0,
71,
120,
0,
131,
144,
0,
0,
163,
0,
177,
211,
249,
0,
256
];

private static ReadOnlySpan<int> HueLimitsForSatLevel2 =>
[
0,
10,
0,
32,
46,
0,
0,
0,
61,
0,
106,
0,
136,
144,
0,
0,
0,
158,
166,
241,
0,
0,
256
];

private static ReadOnlySpan<int> HueLimitsForSatLevel1 =>
[
8,
0,
0,
44,
0,
0,
0,
63,
0,
0,
122,
0,
134,
0,
0,
0,
0,
166,
176,
241,
0,
256,
0
];

private static ReadOnlySpan<int> LumLimitsForHueIndexHigh =>
[
170,
170,
170,
155,
170,
170,
170,
170,
170,
115,
170,
170,
170,
170,
170,
170,
170,
170,
150,
150,
170,
140,
165
];

private static ReadOnlySpan<int> LumLimitsForHueIndexLow =>
[
130,
100,
115,
100,
100,
100,
110,
75,
100,
90,
100,
100,
100,
100,
80,
100,
100,
100,
100,
100,
100,
100,
100
];

private static ReadOnlySpan<uint> ColorNamesMid =>
[
5119,
5135,
5136,
5137,
5122,
5138,
5139,
5140,
5140,
5141,
5141,
5142,
5143,
5126,
5144,
5129,
5145,
5146,
5147,
5148,
5134,
5137,
5135
];

private static ReadOnlySpan<uint> ColorNamesDark =>
[
5137,
5149,
5137,
5137,
5137,
5150,
5150,
5137,
5151,
5151,
5151,
5151,
5152,
5152,
5152,
5153,
5153,
5146,
5147,
5154,
5155,
5137,
5149
];

private static ReadOnlySpan<uint> ColorNamesLight =>
[
5119,
5120,
5121,
5122,
5122,
5123,
5123,
5122,
5124,
5125,
5124,
5124,
5126,
5127,
5128,
5129,
5130,
5131,
5132,
5133,
5134,
5122,
5120
];
#endregion

private unsafe static uint GetColorNameResourceId(WindowsColor color)
{
var hsl = color.ToHsl();
double h = hsl.H * 255.0;
double s = hsl.S * 255.0;
double l = hsl.L * 255.0;

if (l > 240.0)
{
return 5114;
}

if (l < 20.0)
{
return 5118;
}

if (s > 20.0)
{
ReadOnlySpan<int> hueLimits;

if (s > 240.0)
{
hueLimits = HueLimitsForSatLevel5;
}
else if (s > 150.0)
{
hueLimits = HueLimitsForSatLevel4;
}
else if (s > 115.0)
{
hueLimits = HueLimitsForSatLevel3;
}
else if (s > 75.0)
{
hueLimits = HueLimitsForSatLevel2;
}
else
{
hueLimits = HueLimitsForSatLevel1;
}

var hueIndex = 0;
while (hueIndex < 23 && hueLimits[hueIndex] <= h)
{
hueIndex++;
}

if (l > LumLimitsForHueIndexHigh[hueIndex])
{
return ColorNamesLight[hueIndex];
}
else if (l >= LumLimitsForHueIndexLow[hueIndex])
{
return ColorNamesMid[hueIndex];
}
else
{
return ColorNamesDark[hueIndex];
}
}
else if (l <= 170.0)
{
return (l <= 100.0) ? (uint)5117 : 5116;
}
else
{
return 5115;
}
}
}
Loading

0 comments on commit d494270

Please sign in to comment.