title | author | date | CreateTime | categories |
---|---|---|---|---|
win10 uwp color conversion
|
lindexi
|
2019-11-29 10:18:27 +0800
|
2018-2-13 17:23:3 +0800
|
Win10 UWP
|
This article tells you how to turn color from a string, the string transferred from the color
String turn color
You can use the following code in the WPF hexadecimal color string rotation Color
Color color = (Color) ColorConverter.ConvertFromString("#FFDFD991");
string hex = "#FFFFFF";
Color color = System.Drawing.ColorTranslator.FromHtml(hex);
UWP but this method did not, so we need a way to write their own
public SolidColorBrush GetSolidColorBrush(string hex)
{
hex = hex.Replace("#", string.Empty);
byte a = (byte) (Convert.ToUInt32(hex.Substring(0, 2), 16));
byte r = (byte) (Convert.ToUInt32(hex.Substring(2, 2), 16));
byte g = (byte) (Convert.ToUInt32(hex.Substring(4, 2), 16));
byte b = (byte) (Convert.ToUInt32(hex.Substring(6, 2), 16));
return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
}
If there is an incoming junior partner without a transparent, then the above code will appear abnormal, because without a transparent color only six characters, so you can not use the code above, I modified the following code can be converted color
public SolidColorBrush GetSolidColorBrush(string hex)
{
hex = hex.Replace("#", string.Empty);
bool existAlpha = hex.Length == 8;
if (!existAlpha && hex.Length != 6)
{
the throw new new ArgumentException The ( " hex entered is not a valid color " );
}
int n = 0;
byte a;
if (existAlpha)
{
n = 2;
a = (byte) ConvertHexToByte(hex, 0);
}
else
{
the = 0xFF ;
}
var r = (byte) ConvertHexToByte(hex, n);
var g = (byte) ConvertHexToByte(hex, n + 2);
var b = (byte) ConvertHexToByte(hex, n + 4);
return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
}
private static uint ConvertHexToByte(string hex, int n)
{
return Convert.ToUInt32(hex.Substring(n, 2), 16);
}
We can see from the above code ConvertHexToByte this is the hexadecimal int transfer method, see C # 16 hexadecimal strings turn int
But there is written color #FD92
#DAC
color, so it needs to continue to modify the algorithm
public SolidColorBrush GetSolidColorBrush(string hex)
{
hex = hex.Replace("#", string.Empty);
// # FFDFD991
// # DFD991
// # FD92
// #DAC
bool existAlpha = hex.Length == 8 || hex.Length == 4;
bool isDoubleHex = hex.Length == 8 || hex.Length == 6;
if (!existAlpha && hex.Length != 6 && hex.Length != 3)
{
the throw new new ArgumentException The ( " hex entered is not a valid color " );
}
int n = 0;
byte a;
int hexCount = isDoubleHex ? 2 : 1;
if (existAlpha)
{
n = hexCount;
a = (byte) ConvertHexToByte(hex, 0, hexCount);
if (!isDoubleHex)
{
a = (byte) (a * 16 + a);
}
}
else
{
the = 0xFF ;
}
var r = (byte) ConvertHexToByte(hex, n, hexCount);
var g = (byte) ConvertHexToByte(hex, n + hexCount, hexCount);
var b = (byte) ConvertHexToByte(hex, n + 2 * hexCount, hexCount);
if (!isDoubleHex)
{
// # = # FD92 FFDD9922
r = (byte) (r * 16 + r);
g = (byte) (g * 16 + g);
b = (byte) (b * 16 + b);
}
return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
}
private static uint ConvertHexToByte(string hex, int n, int count = 2)
{
return Convert.ToUInt32(hex.Substring(n, count), 16);
}
If you want to see Microsoft's conversion, see https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Parsers.cs
You can copy the source code:
<script src=" https://gist.github.com/lindexi/36c5e223ff77cfb8adc4909dec1576b5.js"></script>If you do not see the code above, please click < https://gist.github.com/lindexi/36c5e223ff77cfb8adc4909dec1576b5 >
Color to String
If you need to switch from color string is very simple
Color.ToString()
The above codes can be output string