public static BufferedImage paletteSwapARGB8(Color[] colorSet,Color clearToColorRequested,BufferedImage argbMappedBufferedImage){
  if (argbMappedBufferedImage == null) {
    return null;
  }
  final Color BLACK_NO_ALPHA=new Color(0x00000000);
  final Color WHITE_NO_ALPHA=new Color(0x00FFFFFF);
  final int ALPHA=3;
  final int RED=0;
  final int GREEN=1;
  final int BLUE=2;
  final int[] orderedBands={ALPHA,RED,GREEN,BLUE};
  Color[] cMap={BLACK_NO_ALPHA,BLACK_NO_ALPHA,BLACK_NO_ALPHA,BLACK_NO_ALPHA};
  boolean clearColorFound=false;
  Color clearToColor=BLACK_NO_ALPHA;
  if (colorSet != null) {
    if (colorSet.length > cMap.length) {
      for (int i=0; i < cMap.length; i++) {
        if (colorSet[i] != null) {
          if (!clearColorFound) {
            clearColorFound=true;
            clearToColor=colorSet[i];
          }
          cMap[orderedBands[i]]=colorSet[i];
        }
      }
    }
 else {
      int startOffset=0;
      if (colorSet.length < 4) {
        startOffset++;
      }
      for (int i=0; i < colorSet.length; i++) {
        if (colorSet[i] != null) {
          if (!clearColorFound) {
            clearColorFound=true;
            clearToColor=colorSet[i];
          }
          cMap[orderedBands[i + startOffset]]=colorSet[i];
        }
      }
    }
  }
  if (clearToColorRequested != null) {
    clearToColor=clearToColorRequested;
  }
  WritableRaster outRaster, inRaster;
  int w=argbMappedBufferedImage.getWidth();
  int h=argbMappedBufferedImage.getHeight();
  BandedSampleModel inSM=new BandedSampleModel(DataBuffer.TYPE_FLOAT,w,h,4);
  DataBufferFloat inDBF=new DataBufferFloat((w * h),4);
  inRaster=Raster.createWritableRaster(inSM,inDBF,null);
  outRaster=inRaster.createCompatibleWritableRaster(w,h);
  float[] cMaptoFlArray, outColortoFlArray, clearColortoFlArray;
  float inBandAsAlpha;
  Color paletteColor;
  outColortoFlArray=new float[]{0f,0f,0f,0f};
  clearColortoFlArray=clearToColor.getRGBComponents(new float[4]);
  clearColortoFlArray[ALPHA]=0f;
  for (int y=0; y < h; y++) {
    for (int x=0; x < w; x++) {
      int packedPixel=argbMappedBufferedImage.getRGB(x,y);
      int testing;
      float ftesting;
      testing=packedPixel;
      testing=testing >> 24;
      testing=testing & 0xFF;
      ftesting=testing;
      ftesting=ftesting / 255;
      outColortoFlArray[ALPHA]=ftesting;
      testing=packedPixel;
      testing=testing >> 16;
      testing=testing & 0xFF;
      ftesting=testing;
      ftesting=ftesting / 255;
      outColortoFlArray[RED]=ftesting;
      testing=packedPixel;
      testing=testing >> 8;
      testing=testing & 0xFF;
      ftesting=testing;
      ftesting=ftesting / 255;
      outColortoFlArray[GREEN]=ftesting;
      testing=packedPixel;
      testing=testing & 0xFF;
      ftesting=testing;
      ftesting=ftesting / 255;
      outColortoFlArray[BLUE]=ftesting;
      inRaster.setPixel(x,y,outColortoFlArray);
      outRaster.setPixel(x,y,clearColortoFlArray);
    }
  }
  for (  int band : orderedBands) {
    paletteColor=cMap[band];
    cMaptoFlArray=paletteColor.getRGBComponents(new float[4]);
    if (paletteColor != BLACK_NO_ALPHA) {
      for (int y=0; y < h; y++) {
        for (int x=0; x < w; x++) {
          inBandAsAlpha=inRaster.getSampleFloat(x,y,band);
          outColortoFlArray=outRaster.getPixel(x,y,new float[4]);
          outColortoFlArray[RED]=(outColortoFlArray[RED] * (1f - (inBandAsAlpha * cMaptoFlArray[ALPHA]))) + (cMaptoFlArray[RED] * (inBandAsAlpha * cMaptoFlArray[ALPHA]));
          outColortoFlArray[GREEN]=(outColortoFlArray[GREEN] * (1f - (inBandAsAlpha * cMaptoFlArray[ALPHA]))) + (cMaptoFlArray[GREEN] * (inBandAsAlpha * cMaptoFlArray[ALPHA]));
          outColortoFlArray[BLUE]=(outColortoFlArray[BLUE] * (1f - (inBandAsAlpha * cMaptoFlArray[ALPHA]))) + (cMaptoFlArray[BLUE] * (inBandAsAlpha * cMaptoFlArray[ALPHA]));
          outColortoFlArray[ALPHA]=(outColortoFlArray[ALPHA] * (1f - (inBandAsAlpha * cMaptoFlArray[ALPHA]))) + (cMaptoFlArray[ALPHA] * (inBandAsAlpha * cMaptoFlArray[ALPHA]));
          outRaster.setPixel(x,y,outColortoFlArray);
        }
      }
    }
  }
  BufferedImage returnBI=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
  for (int y=0; y < h; y++) {
    for (int x=0; x < w; x++) {
      outColortoFlArray=outRaster.getPixel(x,y,new float[4]);
      int packedColor=((int)(outColortoFlArray[ALPHA] * 255f) << 24) | ((int)(outColortoFlArray[RED] * 255f) << 16) | ((int)(outColortoFlArray[GREEN] * 255f) << 8)| ((int)(outColortoFlArray[BLUE] * 255f));
      returnBI.setRGB(x,y,packedColor);
    }
  }
  return returnBI;
}

public static BufferedImage paletteSwapARGB8 ( Color [ ] colorSet , Color clearToColorRequested , BufferedImage argbMappedBufferedImage ) { if ( argbMappedBufferedImage == null ) { return null ; } final Color BLACK_NO_ALPHA = new Color ( 0x00000000 ) ; final Color WHITE_NO_ALPHA = new Color ( 0x00FFFFFF ) ; final int ALPHA = 3 ; final int RED = 0 ; final int GREEN = 1 ; final int BLUE = 2 ; final int [ ] orderedBands = { ALPHA , RED , GREEN , BLUE } ; Color [ ] cMap = { BLACK_NO_ALPHA , BLACK_NO_ALPHA , BLACK_NO_ALPHA , BLACK_NO_ALPHA } ; boolean clearColorFound = false ; Color clearToColor = BLACK_NO_ALPHA ; if ( colorSet != null ) { if ( colorSet . length > cMap . length ) { for ( int i = 0 ; i < cMap . length ; i ++ ) { if ( colorSet [ i ] != null ) { if ( ! clearColorFound ) { clearColorFound = true ; clearToColor = colorSet [ i ] ; } cMap [ orderedBands [ i ] ] = colorSet [ i ] ; } } } else { int startOffset = 0 ; if ( colorSet . length < 4 ) { startOffset ++ ; } for ( int i = 0 ; i < colorSet . length ; i ++ ) { if ( colorSet [ i ] != null ) { if ( ! clearColorFound ) { clearColorFound = true ; clearToColor = colorSet [ i ] ; } cMap [ orderedBands [ i + startOffset ] ] = colorSet [ i ] ; } } } } if ( clearToColorRequested != null ) { clearToColor = clearToColorRequested ; } WritableRaster outRaster , inRaster ; int w = argbMappedBufferedImage . getWidth ( ) ; int h = argbMappedBufferedImage . getHeight ( ) ; BandedSampleModel inSM = new BandedSampleModel ( DataBuffer . TYPE_FLOAT , w , h , 4 ) ; DataBufferFloat inDBF = new DataBufferFloat ( ( w * h ) , 4 ) ; inRaster = Raster . createWritableRaster ( inSM , inDBF , null ) ; outRaster = inRaster . createCompatibleWritableRaster ( w , h ) ; float [ ] cMaptoFlArray , outColortoFlArray , clearColortoFlArray ; float inBandAsAlpha ; Color paletteColor ; outColortoFlArray = new float [ ] { 0f , 0f , 0f , 0f } ; clearColortoFlArray = clearToColor . getRGBComponents ( new float [ 4 ] ) ; clearColortoFlArray [ ALPHA ] = 0f ; for ( int y = 0 ; y < h ; y ++ ) { for ( int x = 0 ; x < w ; x ++ ) { int packedPixel = argbMappedBufferedImage . getRGB ( x , y ) ; int testing ; float ftesting ; testing = packedPixel ; testing = testing > > 24 ; testing = testing & 0xFF ; ftesting = testing ; ftesting = ftesting / 255 ; outColortoFlArray [ ALPHA ] = ftesting ; testing = packedPixel ; testing = testing > > 16 ; testing = testing & 0xFF ; ftesting = testing ; ftesting = ftesting / 255 ; outColortoFlArray [ RED ] = ftesting ; testing = packedPixel ; testing = testing > > 8 ; testing = testing & 0xFF ; ftesting = testing ; ftesting = ftesting / 255 ; outColortoFlArray [ GREEN ] = ftesting ; testing = packedPixel ; testing = testing & 0xFF ; ftesting = testing ; ftesting = ftesting / 255 ; outColortoFlArray [ BLUE ] = ftesting ; inRaster . setPixel ( x , y , outColortoFlArray ) ; outRaster . setPixel ( x , y , clearColortoFlArray ) ; } } for ( int band : orderedBands ) { paletteColor = cMap [ band ] ; cMaptoFlArray = paletteColor . getRGBComponents ( new float [ 4 ] ) ; if ( paletteColor != BLACK_NO_ALPHA ) { for ( int y = 0 ; y < h ; y ++ ) { for ( int x = 0 ; x < w ; x ++ ) { inBandAsAlpha = inRaster . getSampleFloat ( x , y , band ) ; outColortoFlArray = outRaster . getPixel ( x , y , new float [ 4 ] ) ; outColortoFlArray [ RED ] = ( outColortoFlArray [ RED ] * ( 1f - ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ) + ( cMaptoFlArray [ RED ] * ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ; outColortoFlArray [ GREEN ] = ( outColortoFlArray [ GREEN ] * ( 1f - ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ) + ( cMaptoFlArray [ GREEN ] * ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ; outColortoFlArray [ BLUE ] = ( outColortoFlArray [ BLUE ] * ( 1f - ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ) + ( cMaptoFlArray [ BLUE ] * ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ; outColortoFlArray [ ALPHA ] = ( outColortoFlArray [ ALPHA ] * ( 1f - ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ) + ( cMaptoFlArray [ ALPHA ] * ( inBandAsAlpha * cMaptoFlArray [ ALPHA ] ) ) ; outRaster . setPixel ( x , y , outColortoFlArray ) ; } } } } BufferedImage returnBI = new BufferedImage ( w , h , BufferedImage . TYPE_INT_ARGB ) ; for ( int y = 0 ; y < h ; y ++ ) { for ( int x = 0 ; x < w ; x ++ ) { outColortoFlArray = outRaster . getPixel ( x , y , new float [ 4 ] ) ; int packedColor = ( ( int ) ( outColortoFlArray [ ALPHA ] * 255f ) << 24 ) | ( ( int ) ( outColortoFlArray [ RED ] * 255f ) << 16 ) | ( ( int ) ( outColortoFlArray [ GREEN ] * 255f ) << 8 ) | ( ( int ) ( outColortoFlArray [ BLUE ] * 255f ) ) ; returnBI . setRGB ( x , y , packedColor ) ; } } return returnBI ; }
this method transforms the inputed bufferedimage by the supplied color [ ] . the behavior treats the color [ ] as ordered passes a , r , g , b for . length 4 or more . it treats it as ordered passes r , g , b only for . length 3 or less .
