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
Related Q & A:
- How to get the Field Name,Field Length and Field Type?Best solution by Stack Overflow
- How to retrieve a variable name given its value?Best solution by Stack Overflow
- How to get textbox by name?Best solution by Stack Overflow
- How to get a host name?Best solution by Stack Overflow
- How to change the colour msn name?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.