android - 从 RGB 代码中获取颜色名称?

标签 android image-processing rgb color-picker

我有这段代码可以在单击图像时获取 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 值中找出颜色名称(红色、绿色等)。这可能吗?

最佳答案

几年前,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), 但原理是一样的。

关于android - 从 RGB 代码中获取颜色名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930422/

相关文章:

python - Python 中的图像点检测

PHP IMagick RGB 到 CMYK 反转?

android - 使用导航组件动态设置工具栏标题

android - Linux 授予 Android Studio (AVD) 权限/dev/kvm(权限被拒绝)

android - 是否可以仅通过 onPause 终止 Activity ?

algorithm - 追踪图片中的像素

java - Android 中的 R.java 类

python - 需要使用 Contour 检测图像的第二个极值点

ffmpeg - ffmpegpalettgen rgb565可能吗?

ios - 如何从 RGB 值中获取 CGColor?