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 http://www.relsellglobal.in/android-development/picking-image-from-gallery-gallery-intent-way/.

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 = Color.red(pxl);
        int g1 = Color.green(pxl);
        int b1 = Color.blue(pxl);
        int alpha1 = Color.alpha(pxl);


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

        final StringBuilder builder1 = new StringBuilder();
        builder1.append("#");
        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 {

            dropper.setBackgroundColor(Color.parseColor(builder1.toString()));
            textView.setText(builder1.toString());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        res = true;
        break;
    case MotionEvent.ACTION_UP:
        selectedImage.setCursorToBeDrawn(false, evX, evY);
        res = true;
        break;


}

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.