如何根据十六进制颜色代码给出颜色名称?

时间:2015-03-08 07:50:17

标签: android image-processing color-picker

我的代码只能在图像上的触摸像素上提供十六进制代码。 现在我想给出颜色的名称。 这怎么可能使用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;
            }
        });

1 个答案:

答案 0 :(得分:3)

你所要求的是有点棘手,因为虽然有许多颜色的名字被广泛认同(例如,FFFFFF被称为“白色”无处不在),但大多数其他颜色名称不是全球惯例而是由指定颜色的特定人员开发。

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

您可以查看javascript code并将其调整为Android。这是一个非常简单的算法,它通过测量RGB和HSL中的距离来测量您给预定颜色列表中最接近命中的颜色的距离。

相关问题