rgb565 to rgb888 and vice versa?

Hey everyone, I’ve been kind of stuck on this problem for the past few hours, I’m trying to figure out a way to convert a rgb tuple (byte, byte, byte) to rgb565 (16 bits) and vice versa.

I’m pretty sure i’ll need some bitshifts but my usage of bitshifting has been rusty as of late

Can anyone lend me a hand? :slight_smile:

Here’s my attempt so far:

public static void main(String[] args) {
        // TODO code application logic here
        System.out.println(Integer.toHexString(RGB888ToRGB565(0x11ffffff)));
        System.out.println(Integer.toHexString(RGB565ToRGB888(RGB888ToRGB565(0xffeeff))));
    }

   static int RGB888ToRGB565(int red, int green, int blue) {
        int B = (blue >>> 3) & 0x001F;
        int G = ((green >>> 2) << 5) & 0x07E0;
        int R = ((red >>> 3) << 11) & 0xF800;

        return (R | G | B);
    }

    static int RGB888ToRGB565(int aPixel) {
        //aPixel <<= 8;
        //System.out.println(Integer.toHexString(aPixel));
        int red = (aPixel >> 16) & 0xFF;
        int green = (aPixel >> 8) & 0xFF;
        int blue = (aPixel) & 0xFF;
        return RGB888ToRGB565(red, green, blue);
    }

    static int RGB565ToRGB888(int aPixel) {
        int b = (((aPixel) & 0x001F) << 3) & 0xFF;
        int g = (((aPixel) & 0x07E0) >>> 2) & 0xFF;
        int r = (((aPixel) & 0xF800) >>> 8) & 0xFF;
        // return RGBA
        return 0x000000ff | (r << 24) | (g << 16) | (b << 8);
    }