How to get the colour name by RGB value?

How to get closest colour name from RGB value

  • I am trying to get a color pixel from a image and then compare it with some other colors using switch case and display using toast but the problem is I am not able to match it using switch case. can any one help me. here is my code public boolean onTouch(View v, MotionEvent event) { int x = (int)event.getX(); int y = (int)event.getY(); int pixel = bitmap.getPixel(x,y); switch (pixel) { case Color.RED: Toast.makeText(getApplicationContext(), "color", Toast.LENGTH_LONG).show(); break; default: Toast.makeText(getApplicationContext(), "default", Toast.LENGTH_LONG).show(); break; } return false; }

  • Answer:

    I think the reason why the case never match is because the pixel you get is not exactly the Color.RED. Can you print some log to check what's the pixel really is. The really problem should be how to get the colour name for RGB value, and http://stackoverflow.com/a/20670056/2873453 is the answer you are looking for. Below I'll show you a more straight forward but not that serious function: public boolean onTouch(View v, MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); int pixel = bitmap.getPixel(x, y); int distanceRed = Math.abs(Color.red(pixel) - 255) + Math.abs(Color.blue(pixel) - 0) + Math.abs(Color.green(pixel) - 0); if (distanceRed < RED_THESHOLD) { Toast.makeText(getActivity(), "RED", Toast.LENGTH_LONG).show(); } return false;// }

Jatin Malhotra at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.