我有代码十六进制代码“FFB800”,我需要在 WinRT 中转换为“颜色”。
提前致谢。
提问的目的是什么?是否可以在纯 XAML 中执行此操作?XAML 确实采用十六进制代码。
<Grid Background="#FFB800">
否则,在代码隐藏中,我在 Windows 8 应用程序中或多或少地使用了以下内容:
var hexCode = "#FFFFB800";
var color = new Color();
color.A = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
color.R = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
color.G = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);
color.B = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);
在推文中执行此操作的简短方法:
(Color)XamlReader.Load(string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\">{0}</Color>", c));
推荐的方法是从 NuGet获取WinRT XAML Toolkit并调用
WinRTXamlToolkit.Imaging.ColorExtensions.FromString(c);
这比 using 运行得更快XamlReader
,因此如果您需要多次调用它,建议您使用它。您也可以从GitHub克隆它或从此处复制和粘贴:
#region FromString()
/// <summary>
/// Returns a Color based on XAML color string.
/// </summary>
/// <param name="c">The color string. Any format used in XAML should work.</param>
/// <returns></returns>
public static Color FromString(string c)
{
if (string.IsNullOrEmpty(c))
throw new ArgumentException("Invalid color string.", "c");
if (c[0] == '#')
{
switch (c.Length)
{
case 9:
{
//var cuint = uint.Parse(c.Substring(1), NumberStyles.HexNumber);
var cuint = Convert.ToUInt32(c.Substring(1), 16);
var a = (byte)(cuint >> 24);
var r = (byte)((cuint >> 16) & 0xff);
var g = (byte)((cuint >> 8) & 0xff);
var b = (byte)(cuint & 0xff);
return Color.FromArgb(a, r, g, b);
}
case 7:
{
var cuint = Convert.ToUInt32(c.Substring(1), 16);
var r = (byte)((cuint >> 16) & 0xff);
var g = (byte)((cuint >> 8) & 0xff);
var b = (byte)(cuint & 0xff);
return Color.FromArgb(255, r, g, b);
}
case 5:
{
var cuint = Convert.ToUInt16(c.Substring(1), 16);
var a = (byte)(cuint >> 12);
var r = (byte)((cuint >> 8) & 0xf);
var g = (byte)((cuint >> 4) & 0xf);
var b = (byte)(cuint & 0xf);
a = (byte)(a << 4 | a);
r = (byte)(r << 4 | r);
g = (byte)(g << 4 | g);
b = (byte)(b << 4 | b);
return Color.FromArgb(a, r, g, b);
}
case 4:
{
var cuint = Convert.ToUInt16(c.Substring(1), 16);
var r = (byte)((cuint >> 8) & 0xf);
var g = (byte)((cuint >> 4) & 0xf);
var b = (byte)(cuint & 0xf);
r = (byte)(r << 4 | r);
g = (byte)(g << 4 | g);
b = (byte)(b << 4 | b);
return Color.FromArgb(255, r, g, b);
}
default:
throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", c));
}
}
else if (
c.Length > 3 &&
c[0] == 's' &&
c[1] == 'c' &&
c[2] == '#')
{
var values = c.Split(',');
if (values.Length == 4)
{
var scA = double.Parse(values[0].Substring(3));
var scR = double.Parse(values[1]);
var scG = double.Parse(values[2]);
var scB = double.Parse(values[3]);
return Color.FromArgb(
(byte)(scA * 255),
(byte)(scR * 255),
(byte)(scG * 255),
(byte)(scB * 255));
}
else if (values.Length == 3)
{
var scR = double.Parse(values[0].Substring(3));
var scG = double.Parse(values[1]);
var scB = double.Parse(values[2]);
return Color.FromArgb(
255,
(byte)(scR * 255),
(byte)(scG * 255),
(byte)(scB * 255));
}
else
{
throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", c));
}
}
else
{
var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(c);
return (Color)prop.GetValue(null);
}
}
#endregion
var hexCode = "#FFFFB800";
var color = new Color();
color.A = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);
color.R = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
color.G = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
color.B = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);
如何设置为 xaml 矩形对象的填充
rect.Fill = new SolidColorBrush(color);
像这样的另一种解决方案有效,但是如果您只有 6 位十六进制而不是完整的 8 位,则将参数乱序返回,只需将 a 设置为 255
试试这个:
public struct MyColor : Windows.UI.Color
{
/// <summary>
/// Convert hexdecimal value into color.
/// </summary>
/// <param name="hexCode">hexdecimal of color.</param>
/// <returns></returns>
public Windows.UI.Xaml.Media.Brush ColorToBrush(string hexCode)
{
hexCode = hexCode.Replace("#", "");
if (hexCode.Length == 6)
return new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255,
byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)));
else if (hexCode.Length == 8)
{
var color = new Windows.UI.Color();
color.A = byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
color.R = byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
color.G = byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
color.B = byte.Parse(hexCode.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return null;
}
}