从十六进制十进制值 windows 应用程序中获取接近或等效的颜色名称
Get near or equivalent color name from hex decimal value windows apps
我正在使用 SfColorPalette 从用户那里获取颜色输入,这个 returns 我选择的颜色是十六进制十进制颜色代码。但我需要这些颜色作为等效或准确的颜色名称,因为它将用于在搜索中进行过滤。
尝试了以下解决方法
Convert Hex to color
Get color from HexColor
这是我的解决方案:
首先,我做了一个自定义Class:
public class ColorReference
{
public string Name { get; set; }
public Vector3 Argb { get; set; }
}
这是构造从这个site
得到的已知颜色
private static ColorReference[] GetColorReferences()
{
return new ColorReference[] {
new ColorReference() { Name="AliceBlue", Argb=new Vector3 (
240,248,255) },
new ColorReference() { Name="LightSalmon", Argb=new Vector3 (
255,160,122) },
......
};
}
其次,我把这些Vector当作三维向量,对于单个向量,我可以根据Vector3.Distance
方法得到最接近的向量。
private static ColorReference GetClosestColor(ColorReference[] colorReferences, Vector3 currentColor)
{
ColorReference tMin = null;
float minDist = float.PositiveInfinity;
foreach (ColorReference t in colorReferences)
{
float dist = Vector3.Distance(t.Argb, currentColor);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
使用上述方法获取最接近的颜色名称:
public static string GetNearestColorName(Vector3 vect)
{
var cr = GetClosestColor(GetColorReferences(), vect);
if( cr != null )
{
return cr.Name;
}
else
return string.Empty;
}
还需要此方法从十六进制值中提取 argb 值:
public static Vector3 GetSystemDrawingColorFromHexString(string hexString)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(hexString, @"[#]([0-9]|[a-f]|[A-F]){6}\b"))
throw new ArgumentException();
int red = int.Parse(hexString.Substring(1, 2), NumberStyles.HexNumber);
int green = int.Parse(hexString.Substring(3, 2), NumberStyles.HexNumber);
int blue = int.Parse(hexString.Substring(5, 2), NumberStyles.HexNumber);
return new Vector3(red, green, blue);
}
截图:
从这里查看我完成的演示: Github Link
------------2016 年 7 月 26 日更新--------
对于Windows/Phone8.1,因为缺少Vector3class,在你的项目中使用下面的class:
public class Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public static float Distance(Vector3 a, Vector3 b)
{
return (float)Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2) + Math.Pow(a.Z - b.Z, 2)); ;
}
}
我正在使用 SfColorPalette 从用户那里获取颜色输入,这个 returns 我选择的颜色是十六进制十进制颜色代码。但我需要这些颜色作为等效或准确的颜色名称,因为它将用于在搜索中进行过滤。
尝试了以下解决方法
Convert Hex to color
Get color from HexColor
这是我的解决方案:
首先,我做了一个自定义Class:
public class ColorReference
{
public string Name { get; set; }
public Vector3 Argb { get; set; }
}
这是构造从这个site
得到的已知颜色private static ColorReference[] GetColorReferences()
{
return new ColorReference[] {
new ColorReference() { Name="AliceBlue", Argb=new Vector3 (
240,248,255) },
new ColorReference() { Name="LightSalmon", Argb=new Vector3 (
255,160,122) },
......
};
}
其次,我把这些Vector当作三维向量,对于单个向量,我可以根据Vector3.Distance
方法得到最接近的向量。
private static ColorReference GetClosestColor(ColorReference[] colorReferences, Vector3 currentColor)
{
ColorReference tMin = null;
float minDist = float.PositiveInfinity;
foreach (ColorReference t in colorReferences)
{
float dist = Vector3.Distance(t.Argb, currentColor);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
使用上述方法获取最接近的颜色名称:
public static string GetNearestColorName(Vector3 vect)
{
var cr = GetClosestColor(GetColorReferences(), vect);
if( cr != null )
{
return cr.Name;
}
else
return string.Empty;
}
还需要此方法从十六进制值中提取 argb 值:
public static Vector3 GetSystemDrawingColorFromHexString(string hexString)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(hexString, @"[#]([0-9]|[a-f]|[A-F]){6}\b"))
throw new ArgumentException();
int red = int.Parse(hexString.Substring(1, 2), NumberStyles.HexNumber);
int green = int.Parse(hexString.Substring(3, 2), NumberStyles.HexNumber);
int blue = int.Parse(hexString.Substring(5, 2), NumberStyles.HexNumber);
return new Vector3(red, green, blue);
}
截图:
从这里查看我完成的演示: Github Link
------------2016 年 7 月 26 日更新--------
对于Windows/Phone8.1,因为缺少Vector3class,在你的项目中使用下面的class:
public class Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public static float Distance(Vector3 a, Vector3 b)
{
return (float)Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2) + Math.Pow(a.Z - b.Z, 2)); ;
}
}