【问题标题】:How can I give a color name against a hex color code?如何根据十六进制颜色代码给出颜色名称?
【发布时间】:2015-03-08 07:50:16
【问题描述】:

我的代码只能在图像上的触摸像素上提供十六进制代码。 现在我也想给出颜色的名称。 这怎么可能使用Android?请提出一种方法。

public boolean onTouch(View v, MotionEvent event) {
                // get x coordinates
                int x = (int) event.getX();
                // get y coordinates
                int y = (int) event.getY();
                // get bitmap in touch listener
                final Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
                //xand y coordinates stored in to pixel
                int pixel = bitmap.getPixel(x, y);
                //RGB values
                redValue = Color.red(pixel);
                blueValue = Color.blue(pixel);
                greenValue = Color.green(pixel); 

                selected_colour.setText(""+redValue+""+blueValue+""+greenValue);
                selected_colour.setText("touched color:" + "#" + Integer.toHexString(redValue) + Integer.toHexString(greenValue) + Integer.toHexString(blueValue));

                selected_colour.setTextColor(pixel);

                return false;
            }
        });

【问题讨论】:

  • 16777215 不同的十六进制颜色代码。我怀疑有足够的颜色名称来命名所有这些。
  • 是的,但我必须尽可能多地添加..
  • android 中是否有任何库返回十六进制颜色代码的颜色名称?

标签: android image-processing color-picker


【解决方案1】:

您的要求有点棘手,因为尽管有许多颜色的名称被广泛认同(例如,FFFFFF 在任何地方都称为“白色”),但大多数其他颜色名称不是全球约定,而是由命名颜色的特定人员开发。

话虽如此,有几种工具可以做到这一点。查看this link,这是一个您可以提供十六进制代码颜色的网站,它会根据最接近的预定义颜色名称列表对其进行命名。

您可以查看javascript code 并使其适应Android。这是一种非常简单的算法,通过测量 RGB 和 HSL 中的距离,测量您给定的颜色与预定义颜色列表中最接近的颜色的距离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 2015-04-12
    • 1970-01-01
    • 2011-10-02
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多