什么是“圆形”的最佳方式一个Color对象到最近的Color Constant? [英] What's the best way to "round" a Color object to the nearest Color Constant?

查看:208
本文介绍了什么是“圆形”的最佳方式一个Color对象到最近的Color Constant?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将检索像素的确切颜色,并希望将确切的颜色与像 Color.blue 的常量相关联。有一个简单的方法来圆到最近的颜色常数?另外,是否有一种方法来定义您自己的颜色常量?

I will be retrieving the exact color of a pixel and would like to relate that exact color to a constant like Color.blue. Is there an easy way to "round" to the nearest Color Constant? Additionally, is there a way to define your own Color Constants?

推荐答案

基本方法是找到最接近您的通过简单地将样品与每个样品进行比较。当然,问题在于定义最接近。最明显的是使用RGB空间中的欧几里得距离。问题是这个距离与我们对最接近的颜色的感知感觉不是很好地对应。有关此问题的讨论以及一个很好的(容易计算的)指标(包括伪代码!)可以在这个纸张

The basic approach is to find the closest standard color to your sample by simply comparing the sample to each of them. The problem, of course, is in defining "closest." The most obvious would be use the Euclidean distance in RGB space. The problem is that this distance does not correspond very well with our perceptual sense of "closest color". A discussion of this problem, along with a nice (easily computed) metric (including pseudocode!) can be found in this paper.

编辑:为了防止指向该纸张的链接失效(或者如果您懒惰并且愿意使用代码而不理解它)这是我的Java版本的颜色距离函数,纸张建议作为他们的推荐距离函数(在RGB空间中的加权欧几里得距离)的低成本近似:

Just in case the link to that paper goes dead (or if you're lazy and are willing to use code without understanding what it does), here's my Java version of the "color distance function" the paper suggests as a "low-cost approximation" to their recommended distance function (a weighted Euclidean distance in RGB space):

double colorDistance(Color c1, Color c2)
{
    int red1 = c1.getRed();
    int red2 = c2.getRed();
    int rmean = (red1 + red2) >> 1;
    int r = red1 - red2;
    int g = c1.getGreen() - c2.getGreen();
    int b = c1.getBlue() - c2.getBlue();
    return Math.sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
}

请注意,如果你只是要排名颜色距离,调用 Math.sqrt(),节省一些计算成本。

Note that if you're just going to rank color distances, you can dispense with the call to Math.sqrt(), saving some computation costs.

这篇关于什么是“圆形”的最佳方式一个Color对象到最近的Color Constant?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆