Getting Colour Code from Image in Android : Is it required ?

1 min read

Question : Getting Colour Codes in Android From a Image ? Is it required ?


Yes . Not only it is required but also very important for UI Designers to know about colour codes ,to build that stunning UX for the application. Even android studio team understand this so now in the design view of any xml layout you can see colour codes if you have used colours in layout.


Now the question arise why we want to do it. The way I see it ( you might have your own opinions)


I am a software engineer and always think about how can i make life easier by software programs. Actually I am bit lazy :).


So when for the first time i saw colour picker few years back when i was working with Graphics Designing software , that day I though some day i will come up with an app which can work on android device and give color codes taken from image.


So thats how i did it in android check below classes. I first picked an image from phone’s gallery. Link to blog Post

Then image gets displayed on screen. Then whenever i touched the image or any area inside image , i get a callback in onTouchEvent() callback for Activity. Inside onTouchEvent callback. Remember below is pseudo code.


final int action = ev.getAction();

final int evX = (int) ev.getX();
final int evY = (int) ev.getY();

boolean res = false;

switch (action) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
        selectedImage.setCursorToBeDrawn(true, evX, evY);
        int pxl = bitmap.getPixel(evX, evY);
        int r1 =;
        int g1 =;
        int b1 =;
        int alpha1 = Color.alpha(pxl);

        Log.v("TAG", "R G B " + r1 + " " + g1 + " " + b1);

        final StringBuilder builder1 = new StringBuilder();
        builder1.append(r1 > 9 ? Integer.toHexString(r1) : "0" + Integer.toHexString(r1)); // Real computation here
        builder1.append(g1 > 9 ? Integer.toHexString(g1) : "0" + Integer.toHexString(g1)); // Real computation here
        builder1.append(b1 > 9 ? Integer.toHexString(b1) : "0" + Integer.toHexString(b1)); // Real computation here

        Log.v("TAG", "Hex Color is " + builder1.toString());
        //Toast.makeText(MainActivity.this,"Selected Color is "+builder.toString(),Toast.LENGTH_LONG).show();

        try {

        } catch (IllegalArgumentException e) {
        res = true;
    case MotionEvent.ACTION_UP:
        selectedImage.setCursorToBeDrawn(false, evX, evY);
        res = true;


return res;


A working code of application is available as Link


And also you can download an application doing the same from Google play store.