【问题标题】:Get near or equivalent color name from hex decimal value windows apps从十六进制十进制值 Windows 应用程序获取接近或等效的颜色名称
【发布时间】:2016-07-22 09:09:39
【问题描述】:

我正在使用 SfColorPalette 从用户那里获取颜色输入,这会将我选择的颜色作为十六进制十进制颜色代码返回。但我需要这些颜色作为等效或确切的颜色名称,因为它将用于在搜索中进行过滤。

在以下工作中尝试过

Convert Hex to color

Get color from HexColor

String hex to color in windows phone runtime

【问题讨论】:

  • 您需要像“红色”或“深蓝色”这样的颜色名称?大约有 1600 万种颜色(在 24 位 RGB 空间中)。并非所有人都有确切的名字......
  • 是的,我同意所有人都没有确切的名字,所以我在我的问题中提到了“接近”的名字
  • 为什么不在过滤 UI 中显示颜色(这也解决了色盲问题,即有人认为他们选择了红色和蓝色,而您询问绿色和蓝色)
  • 过滤是通过搜索框文本输入完成的,因此无法显示 UI

标签: c# colors windows-phone-8.1 win-universal-app


【解决方案1】:

这是我的解决方案:

首先,我做了一个自定义类:

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) },
        ......
        };
}

其次,我将这些向量视为三维向量,对于单个向量,我可以根据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

---------更新 07/26/2016--------

对于 Windows/Phone 8.1,由于缺少 Vector3 类,请在您的项目中使用以下 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)); ;
    }
}

【讨论】:

  • 聪明!我可能永远不需要这样做,但它为我提供了一种深入了解颜色之间关系的方法。
  • @Franklin,感谢您的回答。我需要这个用于 Windows 8.1 应用程序。似乎您的项目适用于 Windows 10。对于 Windows 8.1,我找不到 Vector3 类。
  • @MohanvelV 我为 Windows/Phone 8.1 创建了一个 Vector3 类,在你的项目中添加这个类。请检查我的更新答案
猜你喜欢
  • 1970-01-01
  • 2015-04-12
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
相关资源
最近更新 更多