Wie konvertiert man richtig von rgb565 zu rgb888

Ich versuche, einen Wert von rgb565 in rgb888 umzuwandeln, aber ich scheine es nicht richtig zu machen. Ich habe auch ein paar vorhandene Antworten wie geprüftdieses Es gibt jedoch Werte, die anscheinend nicht richtig konvertiert werden.

Folgendes habe ich bisher versucht:

PImage picker;
int c;
void setup(){
  size(285,240);
  picker = loadImage("hsv.gif");
  int c = color(255,255,255);
  byte[] word = colorToWord(c);
  int[] rgb = wordToRGB(word);

  noStroke();
  fill(c);
  rect(0,0,50,100);
  fill(rgb[0],rgb[1],rgb[2]);
  rect(50,0,50,100);
}
void draw(){
  image(picker,0,0);
  if((mouseX >= 0 && mouseX <= picker.width)&&
     (mouseY >= 0 && mouseY <= picker.height)) c = picker.get(mouseX,mouseY);

  byte[] word = colorToWord(c);
  int[] rgb = wordToRGB(word);
//  int[] rgb = rgbWordToRGB(word);
  int cc = color(rgb[0],rgb[1],rgb[2]);

  fill(c);
  rect(0,159,142,80);
  fill(cc);
  rect(142,159,143,80);
  fill(0);
  text("original\n"+hex(c),10,185);
  text("converted\n"+hex(cc),152,185);
}

byte[] colorToWord(int c){
  int r = (c >> 16) & 0xFF;
  int g = (c >> 8)  & 0xFF;
  int b =  c        & 0xFF;
  return new byte[]{(byte)((r&248)|g>>5),(byte)((g&28)<<3|b>>3)};
}
int[] wordToRGB3(byte[] data){
// Reconstruct 16 bit rgb565 value from two bytes
  int rgb565 = (data[0] & 255) | ((data[1] & 255) << 8);

  // Extract raw component values (range 0..31 for g and b, 0..63 for g)  
  int b5 = rgb565 & 0x1f;
  int g6 = (rgb565 >> 5) & 0x3f;
  int r5 = (rgb565 >> 11) & 0x1f;

  // Scale components up to 8 bit: 
  // Shift left and fill empty bits at the end with the highest bits,
  // so 00000 is extended to 000000000 but 11111 is extended to 11111111
  int b = (b5 << 3) | (b5 >> 2);
  int g = (g6 << 2) | (g6 >> 4);
  int r = (r5 << 3) | (r5 >> 2); 
  return new int[]{r,g,b};
}
int[] wordToRGB2(byte[] word){
  int r = word[0]&248;
  int g = (word[0]<<5) | ((word[1]&28)>>3);
  int b = word[1] << 3;
  r <<= 3;
  g <<= 2;
  b <<= 3;
  return new int[]{r,g,b};
}
int[] wordToRGB(byte[] word){
  int c = (word[0] << 8) | (word[1]);
  //RGB565
  int r = (c >> (6+5)) & 0x01F;
  int g = (c >> 5) & 0x03F;
  int b = (c) & 0x01F;
  //RGB888 - amplify
  r <<= 3;
  g <<= 2;
  b <<= 3; 
  return new int[]{r,g,b};
}
final int RGB565_MASK_RED       = 0xF800;
final int RGB565_MASK_GREEN     = 0x07E0;  
final int RGB565_MASK_BLUE      = 0x001F; 

int[] rgbWordToRGB(byte[] word){
 int rgb565 = (word[0] << 8) | (word[1]);
 int[] rgb24 = new int[3];
 rgb24[2] = (rgb565 & RGB565_MASK_RED) >> 11;     
 rgb24[1] = (rgb565 & RGB565_MASK_GREEN) >> 5;  
 rgb24[0] = (rgb565 & RGB565_MASK_BLUE);  

 //amplify the image  
 rgb24[2] <<= 3;  
 rgb24[1] <<= 2;  
 rgb24[0] <<= 3; 
 return rgb24;
}

Verwenden dieses Bildes als Farbpalette:

Das Problem liegt bei bestimmten Werten:

Warum passiert das? Gibt es eine Möglichkeit, das zu beheben?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage