0

I know that we can get color from bitmap.getPixel(x,y) method but I do not have x,y and I want to get x,y from given color code.

When onDraw Method will call at the first time I want to draw the line on vertical picker with default color but that time I do not have x,y so not able to call getPixel(x,y) because user-interaction does not happen.

public class VerticalSlideColorPicker extends View {

private String TAG = VerticalSlideColorPicker.class.getName();
private Paint paint;
private Paint strokePaint;
private Path path;
private Bitmap bitmap;
private int viewWidth;
private int viewHeight;
private int centerX;
private float colorPickerRadius;
private OnColorChangeListener onColorChangeListener;
private RectF colorPickerBody;
private float selectorYPos;
private int borderColor;
private float borderWidth;
private int[] colors;
private boolean cacheBitmap = true;
private Context mContext;

public VerticalSlideColorPicker(Context context) {
    super(context);
    mContext = context;
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.VerticalSlideColorPicker,
            0, 0);

    try {
        borderColor = a.getColor(R.styleable.VerticalSlideColorPicker_borderColor, Color.WHITE);
        borderWidth = a.getDimension(R.styleable.VerticalSlideColorPicker_borderWidth, 10f);
        int colorsResourceId = a.getResourceId(R.styleable.VerticalSlideColorPicker_colors, R.array.default_colors);
        colors = a.getResources().getIntArray(colorsResourceId);
    }
    finally {
        a.recycle();
    }
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init() {
    setWillNotDraw(false);
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);

    path = new Path();

    strokePaint = new Paint();
    strokePaint.setStyle(Paint.Style.STROKE);
    strokePaint.setColor(borderColor);
    strokePaint.setAntiAlias(true);
    strokePaint.setStrokeWidth(borderWidth);

    setDrawingCacheEnabled(true);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    path.addRect(colorPickerBody, Path.Direction.CW);
    path.addRect(colorPickerBody, Path.Direction.CW);

    canvas.drawPath(path, strokePaint);
    canvas.drawPath(path, paint);

    if (cacheBitmap) {
        bitmap = getDrawingCache();
        cacheBitmap = false;

        setColor(ContextCompat.getColor(getContext(), R.color.tag_layout_border_audioyes_darkblue));
        //invalidate();
    }
    else {

        canvas.drawLine(colorPickerBody.left, selectorYPos, colorPickerBody.right, selectorYPos, strokePaint);
    }

}
/**
 * Set the color this view should show.
 *
 * @param color The color that should be selected. #argb
 */
public void setColor(int color) {

    /*int[] pixels = new int[bitmap.getHeight()*bitmap.getWidth()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    for (int i=0; i<bitmap.getWidth()*5; i++)
        pixels[i] = ContextCompat.getColor(getContext(), R.color.blue_picker);
    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());*/

   /* int alpha = Color.alpha(color);
    int red = Color.red(color);
    int blue = Color.blue(color);
    int green = Color.green(color);

    float[] hsv = new float[3];

    Color.RGBToHSV(red, green, blue, hsv);*/
  /* selectorYPos = 584;
    int selectedColor = bitmap.getPixel(viewWidth / 2, (int) selectorYPos);*/


  /*  this.alpha = alpha;
    hue = hsv[0];
    sat = hsv[1];
    val = hsv[2];*/

    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(color);
    }

    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float yPos = Math.min(event.getY(), colorPickerBody.bottom);
    android.util.Log.e(TAG, "Float :" + yPos);
    yPos = Math.max(colorPickerBody.top, yPos);
    android.util.Log.e(TAG, "Normal :" + yPos);


    selectorYPos = yPos;
    int selectedColor = bitmap.getPixel(viewWidth / 2, (int) selectorYPos);
    android.util.Log.e(TAG, "Color :" + selectedColor);
    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(selectedColor);
    }
    invalidate();

    return true;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    viewWidth = w;
    viewHeight = h;

    centerX = viewWidth / 2;
    colorPickerRadius = (viewWidth / 2) - borderWidth;

    colorPickerBody = new RectF(centerX - colorPickerRadius, borderWidth + colorPickerRadius, centerX + colorPickerRadius, viewHeight - (borderWidth + colorPickerRadius));

    LinearGradient gradient = new LinearGradient(colorPickerBody.left, colorPickerBody.top,
            colorPickerBody.right,
            colorPickerBody.bottom, colors, null, Shader.TileMode.CLAMP);

    paint.setShader(gradient);

    resetToDefault();
}

public void setBorderColor(int borderColor) {
    this.borderColor = borderColor;
    invalidate();
}

public void setBorderWidth(float borderWidth) {
    this.borderWidth = borderWidth;
    invalidate();
}

public void setColors(int[] colors) {
    this.colors = colors;
    cacheBitmap = true;
    invalidate();
}

public void resetToDefault() {
    selectorYPos = borderWidth + colorPickerRadius;

    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(Color.TRANSPARENT);
    }
    invalidate();
}

public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
    this.onColorChangeListener = onColorChangeListener;
}

}

1
  • 2
    You could iterate through each (x,y) and filter them by your color code. Then, you'd get a list of matching coordinates.
    – Anatolii
    Jun 12, 2018 at 7:11

1 Answer 1

0

A brute force solution could be the following:

private List<Point> getPixelswithColor(Bitmap bitmap, int colorId) {
    List<Point> pixels = new ArrayList();
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            if (bitmap.getPixel(x, y) == colorId) {
                pixels.add(new Point(x, y));
            }
        }
    }
    return pixels;
}
1
  • Thanks for the help it works for me. I just changed getColor to getPixel
    – Mahi
    Jun 12, 2018 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.