I would be more careful with this initialization. The final image should result in 1 band and therefore it should have no transparecy. This code preserve the transparency of the input image and therefore it is wrong since if the colormap add transparency different from mask or translucent this would be reflected in the final image.