Search code examples
javaandroidhexrgbgetpixel

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)


Solution

  • Convert the int values into hexadecimal representations:

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

    Add to your TextView:

    colourbbox2.setText(hexadecimal);