0

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)

leo666
  • 27
  • 3

3 Answers3

1

You can use Integer.toHexString() :

colourbbox2.setText(Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b));
Rod
  • 754
  • 11
  • 21
0

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

Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

Convert the int values into hexadecimal representations:

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

Add to your TextView:

colourbbox2.setText(hexadecimal);
CzarMatt
  • 1,773
  • 18
  • 22
  • This worked perfectly thank you, may I ask why its formatted like "%02x%02x%02x" this ? I suppose something similar will work for HSV conversion ? – leo666 Apr 10 '15 at 19:16