user4440416
user4440416

Reputation: 71

How can I give a color name against a hex color code?

I have a code that can only give hex code on touch pixel on image. Now I want to give the name of the color as well. How is this possible using Android?? Please suggest a way.

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;
            }
        });

Upvotes: 4

Views: 1845

Answers (1)

Gil Moshayof
Gil Moshayof

Reputation: 16761

What you're asking for is a bit tricky, because although there are many colours whose names are widely agreed upon (for instance, FFFFFF is called "White" everywhere), most other colour names are not a global convention but rather developed by the specific people who are naming the colours.

That being said, there are several tools out there which can do this. Check out this link, it's a website which you can provide a colour in hex code, and it will name it according to the closest pre-defined list of colour names.

You can view the javascript code and adapt it to Android. It's a pretty straight-forward algorithm which measures the distance of the colour you gave to the closest hit in a predefined colour list, by measuring the distance in RGB and HSL.

Upvotes: 4

Related Questions