我有一个代码只能在图像上的触摸像素上给出十六进制代码。 现在我也想给出颜色的名称。 这怎么可能使用 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;
}
});
最佳答案
您的要求有点棘手,因为尽管有许多颜色的名称得到广泛认可(例如,FFFFFF 到处都被称为“白色”),但大多数其他颜色名称并不是全局惯例,而是由指定颜色的特定人员开发。
也就是说,有几种工具可以做到这一点。查看this link , 这是一个可以用十六进制代码提供颜色的网站,它会根据最接近的预定义颜色名称列表来命名它。
您可以查看 javascript code并适配安卓。这是一种非常直接的算法,它通过测量 RGB 和 HSL 中的距离来测量您提供的颜色与预定义颜色列表中最接近的匹配项的距离。
关于android - 我如何根据十六进制颜色代码给出颜色名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924251/