leo666
leo666

Reputation: 27

Int RGB to HEX values

Hi guys so basically im building an android application that can display colour information with use of the camera. currently the app is getting pixel information and displaying RGB values in a textview. I would like to expand it and add a textview that can show HEX values but im unsure how to convert it and display it. pretty sure I need to make changes below...

public void pix(){
        operation= Bitmap.createBitmap(bmp.getWidth(),
                bmp.getHeight(),bmp.getConfig());

        int height = bmp.getHeight();
        int width = bmp.getWidth();
        int p = bmp.getPixel(height / 2, width / 2);

        int r = Color.red(p);
        int g = Color.green(p);
        int b = Color.blue(p);

       // Toast.makeText(this, String.valueOf(r) + String.valueOf(g) + String.valueOf(b), Toast.LENGTH_LONG).show();
        colourbbox1.setText( String.valueOf(r) + String.valueOf(g) + String.valueOf(b));

        colourbbox2.setText( String.valueOf(r) + String.valueOf(g) + String.valueOf(b));

colorbbox2 is the intended textview. Any help would be much appreciated.

(still a java novice FYI)

Upvotes: 0

Views: 751

Answers (3)

CzarMatt
CzarMatt

Reputation: 1833

Convert the int values into hexadecimal representations:

String hexadecimal = String.format("#%02X%02X%02X", r, g, b);

Add to your TextView:

colourbbox2.setText(hexadecimal);

Upvotes: 0

Jemshit
Jemshit

Reputation: 10038

Try: String hexColor = String.format( "#%02x%02x%02x", r, g, b );

Upvotes: 0

Rod
Rod

Reputation: 804

You can use Integer.toHexString() :

colourbbox2.setText(Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b));

Upvotes: 1

Related Questions