如何在缓冲图像中设置IndexColorModel以具有某些颜色和一个透明?

时间:2015-10-23 12:24:10

标签: java graphics bufferedimage

我正在尝试绘制一个显示某种品质的条形图。

我创建了一个像这样的缓冲图像:

 private static BufferedImage createBufferedImage(int width, int height) {
        int[] palette = {0x00ff00ff, 0xffff00ff, 0xff0000ff , 0xffff0000};
        IndexColorModel colorModel = new IndexColorModel(2, 4,
            palette, 0, true, 0, DataBuffer.TYPE_BYTE);
        return new BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED, colorModel);
    }  

colorModel应该有颜色:绿色,黄色,红色和一个透明。 酒吧的颜色取决于这样的质量:

 private static Color getBarColor(double quality) {
        if (quality >= 0.70) {
            return Color.GREEN;
        } else if (quality >= 0.40) {
            return Color.YELLOW;
        } else {
            return Color.RED;
        }
    }

我创建图形'绘制条形图并设置颜色:

Graphics2D g = image.createGraphics();
g.setColor(getBarColor(quality));

然而,当我将质量的颜色设置为大于0.7时,它会绘制蓝色条,而对于低于0.7的质量,则会绘制红色。

我认为问题在于我的托盘,我没有正确设置颜色,当我试图获得绿色并且它不存在时,设置最接近的颜色。我认为应该使用格式RRGGBBAA,其中AA设置透明度,ff不透明,00透明。我理解正确吗?问题在哪里?

我很感谢你的帮助。

1 个答案:

答案 0 :(得分:4)

您的问题是您假设alpha是您输入调色板的整数的低位部分。事实上,它是ARGB,而不是RGBA。

您可以通过简单的循环测试:

for ( int i = 0; i < 4; i++ ) {
    int red = colorModel.getRed(i);
    int green = colorModel.getGreen(i);
    int blue = colorModel.getBlue(i);
    int alpha = colorModel.getAlpha(i);

    System.out.printf("For index %d, red=%d, green=%d, blue=%d, alpha=%d%n", i,red,green,blue,alpha);
}

结果将是:

For index 0, red=255, green=0, blue=255, alpha=0
For index 1, red=255, green=0, blue=255, alpha=255
For index 2, red=0, green=0, blue=255, alpha=255
For index 3, red=255, green=0, blue=0, alpha=255

现在,如果您希望颜色为绿色,黄色,红色和透明黄色,则应使用:

int[] palette = { 0xff00ff00, 0xffffff00, 0xffff0000, 0x00ffff00 };

但是你应该注意到你还告诉它将第一个像素值视为透明而不是最后一个。您的颜色模型创建应该是:

IndexColorModel colorModel = new IndexColorModel(2,         // bits per pixel
                                                 4,         // size of color component array
                                                 palette,   // color map
                                                 0,         // offset in the map
                                                 true,      // has alpha
                                                 3,         // the pixel value that should be transparent
                                                 DataBuffer.TYPE_BYTE);

当你这样做时,如果再次运行上面的循环,你将得到结果:

For index 0, red=0, green=255, blue=0, alpha=255
For index 1, red=255, green=255, blue=0, alpha=255
For index 2, red=255, green=0, blue=0, alpha=255
For index 3, red=255, green=255, blue=0, alpha=0

事实上,如果您根本不在调色板值中使用alpha,那将会更加直截了当,因为您只想在一个特定值中指示透明度。为此,请将hasAlpha参数设置为false而不是true

int[] palette = { 0x00ff00, 0xffff00, 0xff0000, 0xffff00 };
IndexColorModel colorModel = new IndexColorModel(2,
                                                 4,
                                                 palette,
                                                 0,
                                                 false,
                                                 3,
                                                 DataBuffer.TYPE_BYTE);

这会给你相同的结果,但它更容易阅读和预测结果。