[50高分悬赏] 如何找到最接近的颜色(RGB)

我有一批颜色(RGB和HEX), 如:

darkolivegreen|暗橄榄绿|#556b2f|85,107,47|
darkorange|暗桔黄色|#ff8c00|255,140,0|
darkorchid|暗紫色|#9932cc|153,50,204|
darkred|暗红色|#8b0000|139,0,0|
darksalmon|暗肉色|#e9967a|233,150,122|
darkseagreen|暗海兰色|#8fbc8f|143,188,143|
darkslateblue|暗灰蓝色|#483d8b|72,61,139|
darkslategray|暗瓦灰色|#2f4f4f|47,79,79|
darkslategrey|暗瓦灰色|#2f4f4f|47,79,79|
darkturquoise|暗宝石绿|#00ced1|0,206,209|

然后我这边有个RGB的值, 如150,68,80

我如何找到最接近这个RGB值的颜色(可以是多个)
[b]问题补充:[/b]
:x 我是说如何在程序里面判断...
[b]问题补充:[/b]

我是说在程序里, 如何根据输入的一个RGB值, 判断最接近这个RGB值的颜色(我有大概256种颜色), 但RGB可以有16millioin的combination... 找个range倒是一个不错的死路, 但好像效率好像不高...

[b]问题补充:[/b]
多谢walsh大哥, 我这就试试, 然后告诉你, 谢谢! :idea: :lol:

首先说明如下:
[color=red]给定RGB,确定颜色名称,只能是大概范围,也就是说该RGB最接近的名称,因为在你的列表中不可能列举出所有RGB对应的颜色名称。[/color]

我的思路:
[color=red]遍历你保存的颜色,然后分别和给定的RGB(R,G,B)做比较,然后求平均数,平均数最小的就是最接近的颜色。[/color]

需要两个类:
[code="java"]/**

  • 实体类
  • @author zhq
  • /
    public class ColorBean {
    /
    * 颜色的英文名称 /
    private String colorEnName;
    /
    * 颜色的中文名称 /
    private String colorChName;
    /
    * 颜色的Hex /
    private String colorHex;
    /
    * R /
    private int r;
    /
    * G /
    private int g;
    /
    * B */
    private int b;

    public ColorBean(int r, int g, int b, String colorName, String colorHex) {
    this.r = r;
    this.g = g;
    this.b = b;
    this.colorChName = colorName;
    this.colorHex = colorHex;
    }

    public String getColorEnName() {
    return colorEnName;
    }

    public void setColorEnName(String colorEnName) {
    this.colorEnName = colorEnName;
    }

    public String getColorChName() {
    return colorChName;
    }

    public void setColorChName(String colorChName) {
    this.colorChName = colorChName;
    }

    public String getColorHex() {
    return colorHex;
    }

    public void setColorHex(String colorHex) {
    this.colorHex = colorHex;
    }

    public int getR() {
    return r;
    }

    public void setR(int r) {
    this.r = r;
    }

    public int getG() {
    return g;
    }

    public void setG(int g) {
    this.g = g;
    }

    public int getB() {
    return b;
    }

    public void setB(int b) {
    this.b = b;
    }

    public String toString() {
    return colorChName + "(" + r + " ," + g + " ," + b + ")";
    }
    }[/code]

[color=red]
测试类:[/color]

[code="java"]import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**

  • 测试类
  • @author zhq
  • */
    public class ColorDemo {
    private static Random rand = new Random(47);
    private static String[] hexNum = { "0", "1", "2", "3", "4", "5", "6", "7",
    "8", "9", "A", "B", "C", "D", "E", "F" };

    public static List getColorList() {

    List<ColorBean> result = new ArrayList<ColorBean>();
    for (int i = 0; i < 10; i++) {
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < 6; k++)
            sb.append(hexNum[rand.nextInt(16)]);
        result.add(new ColorBean(rand.nextInt(255), rand.nextInt(255), rand
                .nextInt(255), "颜色" + i, "#" + sb.toString()));
    }
    return result;
    

    }

    public static String getColorName(int r, int g, int b) {
    String colorName = "";
    int minRGB = 765;
    ColorBean minColorBean = null;
    System.out.println("==========颜色列表========");
    for (ColorBean cb : getColorList()) {
    System.err.println(cb);
    int tempR = Math.abs(r - cb.getR());
    int tempG = Math.abs(g - cb.getG());
    int tempB = Math.abs(b - cb.getB());
    int tempRGB = (tempR + tempG + tempB) / 3;
    if (tempRGB <= minRGB) {
    minRGB = tempRGB;
    minColorBean = cb;
    }
    colorName = minColorBean.getColorChName();
    }
    return colorName;
    }

    public static void main(String[] args) {
    String hexStr = getColorName(28, 203, 31);
    System.out.println("\nRGB(28, 203, 31)最接近颜色名称是:" + hexStr);
    }
    }[/code]

[color=red]运行结果:[/color]
==========颜色列表========
颜色0(253 ,35 ,192)
颜色1(28 ,28 ,1)
颜色2(134 ,183 ,26)
颜色3(96 ,112 ,201)
颜色4(198 ,112 ,111)
颜色5(208 ,64 ,20)
颜色6(87 ,209 ,207)
颜色7(54 ,57 ,215)
颜色8(43 ,233 ,41)
颜色9(228 ,218 ,48)

RGB(28, 233, 41)最接近颜色名称是:颜色8

[color=red]注:因为我没有你的数据,所以颜色数据是模拟的。你用的时候,只需要换为你的颜色即可。[/color]

去网上找个取色器,你要的颜色周围的都是。

很简单,启动附件中的“画图”,从颜料盒点选一颜色,进入编辑颜色对话框,选择规定自定义颜色,选择你想要的颜色,其红,黄,蓝三色的值即为RGB的值

比如这个http://colorschemedesigner.com/

这个好办,rgb相当于空间一个坐标点,计算比较一下(150,68,80)和其他各点的距离,哪个最近就是哪个。距离公式网上能查到...

规定一个范围呗,比如认为150+-3,68+-3,80+-3是接近的值。
取一下颜色你就会知道颜色代码是连续的。
当然也可以取成圆形区域。

系统自带的画图板就可以办到
开始——程序——附件——画图
然后选择颜色——编辑颜色——规定自定义颜色 输入你要的RGB 就行了
[img]D:\Administrator文档和收藏夹\My Pictures\111.bmp[/img]

楼主的意思是说使用程序进行获取吧。

我以前也写过相关的算法,但是后来发现有些问题。有些颜色值获取不准。

想根据RGB值判断出“darkturquoise|暗宝石绿”这种文字表示的颜色?

256种颜色很快的...

恩,挨个判断离这256个颜色的距离吧,应该不慢,虽然感觉不是最后的方法。

算欧式距离已经很简单了,不就 256 趟么?
计算机一秒钟可以做亿次运算,有什么好纠结的,你在屏幕上画个圈的时间复杂度都比这个大几个数量级。

欧式距离只反映了在RGB尺度上的相近程度,与其它模型相比它反映视觉感受上的相近程度不够准确。如果速度要求优先,那就直接算RGB模型上的欧式距离来比较,够用了。如果想在视觉上更接近,可以转换成YUV之类的模型再算相似度。

可以取接近值!!!如(85,107,47),取每个数的绝对值3范围内
R:82<=x<=88
G:104<=y<=110
B:44<=z<=50
这样,寻找颜色看是否有满足条件的,有就是接近的