c# - 将随机颜色分类为 Windows Phone 原色之一

标签 c# colors windows-phone-8

我正在尝试创建一种接受随机颜色值的方法,然后检查它最接近的原色。

enter image description here

我想我必须比较它的 RGB 值,看看它在这些原色范围内的位置,然后将其设置为红色、黄色或蓝色等。

执行此操作的最佳方法是什么?谢谢

编辑 这个问题被标记为重复,但我检查了所述重复问题的其他答案,但它没有提供我想要的答案。

使用公式

d = sqrt((r2-r1)^2 + (g2-g1)^2 + (b2-b1)^2)

有时不同颜色会产生相同的值。例如,输入颜色为 R170 : G0 : B255。使用公式,白色 R255 : G255 : B255 和红色 R255 : G0 : B0 的距离产生 (int)269。那么红色和白色哪个颜色更接近呢?我从列表中删除了黑色和白色,但其他一些颜色仍然出现问题。

我正在比较几种颜色,即:黑色、白色、红色、石灰。蓝色、黄色、青色。品红色和灰色如这里的 RGB 颜色表中所列:rapidtables.com/web/color/RGB_Color.htm

最佳答案

我面前没有 IDE,但这是我的伪评论(如有必要,可以稍后更新)

var closestColor

Get vector magnitude of( myR + 255, myG, myB)
set closestColor to Red

Get vector magnitude of( myR , myG+ 255, myB)
If(Green is closer than red)
 set closestColor to Green

Get vector magnitude of( myR , myG, myB + 255)
If(Blue is closer than closestColor)
 set closestColor to Blue

Return closest color

更新

我做了一个带有扩展的快速 linq 示例

    public static double VectorMagnitude(this Color c, Color otherC)
    {
        return Math.Sqrt(((int)(c.R + otherC.R))^2 + 
            ((int)(c.G + otherC.G))^2 + 
            ((int)(c.B + otherC.B))^2); 
    }

    private static List<Color> Colors()
    {
        return new List<Color>()
        {
            Color.Red,
            Color.Blue,
            Color.FromArgb(0,255,0)
        };
    }

 var l = Colors().OrderBy(x => x.VectorMagnitude(
                Color.FromArgb(255, R, G, B))).FirstOrDefault();

关于c# - 将随机颜色分类为 Windows Phone 原色之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17269535/

相关文章:

c# - Java 是否像 C# 一样具有 'Debug' 和 'Release' 构建模式?

c# - 如何将 CancellationTokenSource 附加到 DownloadStringTaskAsync 方法并取消异步调用?

bash - Grep - 循环显示每场比赛的颜色

Android修改不同主题的colors.xml

c# - 更新后 css iframe 未在客户端上更新

c# - 如何正确创建对 Unity 对象的引用?

node.js - 这是什么\u001b[9...选择控制台上显示的颜色文本的语法,以及如何添加更多颜色?

windows-phone-8 - 在 Windows phone 8 中使用 App.Current.Terminate() 方法

azure - Windows Azure 访问控制和 Windows Phone 8

c# - XAML 布局更改未在生成时更新