【问题标题】:Get color name from RGB code?从 RGB 代码中获取颜色名称?
【发布时间】:2015-05-09 22:12:40
【问题描述】:

我有这段代码可以在单击图像时获取 RGB 颜色:

public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                final Bitmap bitmap = ((BitmapDrawable) image.getDrawable())
                        .getBitmap();
                int pixel = bitmap.getPixel(x, y);
                redValue = Color.red(pixel);
                blueValue = Color.blue(pixel);
                greenValue = Color.green(pixel); 


                tv_selected_colour.setText(""+redValue+""+blueValue+""+greenValue);

                return false;
            }
        });

我需要从 RGB 值中挑选出颜色名称(红色、绿色等)。这可能吗?

【问题讨论】:

  • 颜色名称?这在现代有点荒谬,谁有时间查找 1670 万个不同的名字?
  • 是的,但这是我的项目要求
  • 诸如此类:help.eclipse.org/indigo/…??
  • 在 SQLite 中用十六进制的颜色名称和定义创建一个表?通过颜色并获得名称。但是,由于我不相信您会插入超过 1600 万个颜色名称,因此大多数情况下您会得到“未定义”(或 NULL)
  • 也许他只是想要“近似”颜色名称,所以非常非常深的灰色会给出“黑色”

标签: android image-processing rgb color-picker


【解决方案1】:

几年前,Randall Monroe(XKCD 的)做了一个large online survey of English-speakers,结果是list of over 900 colour names。您可以轻松地将此数据用作颜色命名函数的基础,该函数将 RGB 三元组转换为最接近颜色的名称。这是 C 语言中的简单实现,例如:

#include <stdio.h>
#define squared(X) ((X) * (X))

typedef struct {
  char *name;
  unsigned char r, g, b;
} color_name;

/* Source: http://xkcd.com/color/rgb.txt */
/* License: http://creativecommons.org/publicdomain/zero/1.0/ */
static const color_name xkcd_colors[] = {
  {"cloudy blue",0xac,0xc2,0xd9}, {"dark pastel green",0x56,0xae,0x57},
  {"dust",0xb2,0x99,0x6e}, {"electric lime",0xa8,0xff,0x04},
           :
      (et cetera)
           :
  {"blue",0x03,0x43,0xdf}, {"green",0x15,0xb0,0x1a},
  {"purple",0x7e,0x1e,0x9c}
};

int main(int argc, char *argv[]) {
  int red, green, blue, d2, mind2, i, result;
  if (argc != 2 ||sscanf(argv[1],"%02x%02x%02x",&red,&green,&blue) != 3)
    return !puts("Provide 6 hex chars as command line argument.");

  mind2 = 256 * 256 * 3;
  for (i=0; i<sizeof(xkcd_colors)/sizeof(color_name); i++) {
    d2 = squared(red - xkcd_colors[i].r) +    /* Calculate squared  */
         squared(green - xkcd_colors[i].g) +  /* distance from each */
         squared(blue - xkcd_colors[i].b);    /* color in list.     */
    if (d2 < mind2) {
      mind2 = d2;   /* Find the minimum distance and */
      result = i;   /* store the index of this color */
    }
  }
  printf("That color is called \"%s\"\n",xkcd_colors[result].name);
  return 0;
}

注意:如果您不希望函数返回像“baby shit brown”(#ad900d)或“puke”(# a5a502),但原理是一样的。

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 2013-12-02
    • 2012-03-02
    • 2020-04-07
    • 2018-12-23
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多