如何在Android中找到RGB滚轮内特定颜色的X,Y坐标

时间:2018-05-03 10:19:27

标签: java android rgb palette android-color

我在Android Activity上有RGB Wheel Here 个PNG图片,颜色为#6DFFE0

我想在RGB滚轮中找到颜色的X,Y坐标(位置),以便我可以在Android中动态移动我的指示器。 代码应该只在Android / Java中。

1 个答案:

答案 0 :(得分:0)

您可以循环所有像素并获取与您给出的颜色匹配的像素

    ArrayList<String> pixels_matching_color = new ArrayList<>();
    int color_to_find = Color.RED; //#FF0000 
    ImageView imageView = new ImageView(this);
    Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    int total_width = bitmap.getWidth();
    int total_height = bitmap.getHeight();
    for (int y = 0; y < total_height; y++) {
        for (int x = 0; x < total_width; x++) {
            int pixel = bitmap.getPixel(x,y);
            //Reading colors
            int redValue = Color.red(pixel);
            int blueValue = Color.blue(pixel);
            int greenValue = Color.green(pixel);

            //finally creating the color for pixel
            int pixel_color = Color.rgb(redValue, blueValue, greenValue);
            if (pixel_color == color_to_find){
                pixels_matching_color.add(String.format("%s,%s",x,y));
            }
        }
    }
    //the array will contans the pixels that are matching the color you given
    System.out.println(pixels_matching_color);