XLS调色面板HSSFPalette

1.调色板HSSFPlalette

/**
 * workbook调色板
 * 调色板的第一个颜色索引从0x8开始,第二个索引是0x9...一直到0x40, 
 */
public final class HSSFPalette {
	/**
	 * 可编辑的调色板,调色板的颜色可以自定义,当调色板颜色没有填充满时用黑色填充
	 * 可编辑的调色板定义了56种颜色,索引值范围:[0x8, 0x]
	 */
    private PaletteRecord _palette;

    protected HSSFPalette(PaletteRecord palette)
    {
        _palette = palette;
    }

    /**
     * 获取给定索引的颜色
     *
     * @param index 调色面板中颜色索引,索引从0x8开始,在0x8到0x40之间:[8,64]
     * @return 返回颜色,如果索引未填充,则返回null
     */
    public HSSFColor getColor(short index) {
        // 处理特例:默认黑色
        if (index == HSSFColorPredefined.AUTOMATIC.getIndex()) {
            return HSSFColorPredefined.AUTOMATIC.getColor();
        }
		// 从自定义
        byte[] b = _palette.getColor(index);
        return (b == null) ? null : new CustomColor(index, b);
    }

    /**
     * 获取给定索引的颜色
     *
     * @param index 调色面板中颜色索引,在0x8到0x40之间:[8,64]
     * @return 返回颜色,如果索引未填充,则返回null
     */
    public HSSFColor getColor(int index) {
    	return getColor((short)index);
    }

    /**
     * 查找给定颜色的第一次出现,即查找给定颜色是否在调色板中存在
     *
     * @param red RGB颜色红色分量:[0, 255]
     * @param green RGB颜色绿色分量:[0, 255]
     * @param blue RGB颜色蓝色分量:[0, 255]
     * @return 返回颜色,如果此调色板中不存在此颜色,返回null
     */
    public HSSFColor findColor(byte red, byte green, byte blue)
    {
        byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
        for (short i = PaletteRecord.FIRST_COLOR_INDEX; b != null;
            b = _palette.getColor(++i))
        {
            if (b[0] == red && b[1] == green && b[2] == blue)
            {
                return new CustomColor(i, b);
            }
        }
        return null;
    }

    /**
	 * 在自定义调色板中找到最接近的匹配颜色。 找到颜色之间的距离的方法是相当重要的。
     * @param red   匹配的颜色的红色分量。
     * @param green 匹配的颜色的绿色分量。
     * @param blue  匹配的颜色的蓝色分量。
     * @return 返回最接近的颜色,如果没有当前的自定义颜色,则返回null
     */
    public HSSFColor findSimilarColor(byte red, byte green, byte blue) {
    	return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue));
    }
	
    /**
     * 在自定义调色板中找到最接近的匹配颜色。 找到颜色之间的距离的方法是相当重要的
     *
     * @param red   匹配的颜色的红色分量。
     * @param green 匹配的颜色的绿色分量。
     * @param blue  匹配的颜色的蓝色分量。
     * @return 返回最接近的颜色,如果没有当前的自定义颜色,则返回null
     */
    public HSSFColor findSimilarColor(int red, int green, int blue) {
        HSSFColor result = null;
        int minColorDistance = Integer.MAX_VALUE;
        byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
        for (short i = PaletteRecord.FIRST_COLOR_INDEX; b != null;
            b = _palette.getColor(++i))
        {
            int colorDistance = Math.abs(red - unsignedInt(b[0])) +
            	Math.abs(green - unsignedInt(b[1])) +
            	Math.abs(blue - unsignedInt(b[2]));
            if (colorDistance < minColorDistance)
            {
                minColorDistance = colorDistance;
                result = getColor(i);
            }
        }
        return result;
    }
    
    /**
     * Turn a byte of between -127 and 127 into something between
     *  0 and 255, so distance calculations work as expected.
     */
    private int unsignedInt(byte b) {
    	return 0xFF & b;
    }

    /**
     * 设置调色板中索引index处的颜色
     *
     * @param index 调色板的索引
     * @param red RGB红色分量
     * @param green RGB绿色分量
     * @param blue RGB蓝色分量
     */
    public void setColorAtIndex(short index, byte red, byte green, byte blue) {
        _palette.setColor(index, red, green, blue);
    }

    /**
	 * 将一个新的颜色添加到一个空的颜色插槽
     * @param red       The red component
     * @param green     The green component
     * @param blue      The blue component
     *
     * @return  返回新的颜色
     *
     * @throws RuntimeException if there are more more free color indexes.
     */
    public HSSFColor addColor( byte red, byte green, byte blue )
    {
        byte[] b = _palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
        short i;
        for (i = PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = _palette.getColor(++i))
        {
            if (b == null)
            {
                setColorAtIndex( i, red, green, blue );
                return getColor(i);
            }
        }
        throw new RuntimeException("Could not find free color index");
    }
	
    /**
	 * 内部类:自定义颜色类
	 */
	private static final class CustomColor extends HSSFColor {
		// 当前颜色的索引值
        private short _byteOffset;
        private byte _red;
        private byte _green;
        private byte _blue;

		/**
		 * @param byteOffset 
		 */
        public CustomColor(short byteOffset, byte[] colors)
        {
            this(byteOffset, colors[0], colors[1], colors[2]);
        }

        private CustomColor(short byteOffset, byte red, byte green, byte blue)
        {
            _byteOffset = byteOffset;
            _red = red;
            _green = green;
            _blue = blue;
        }

        @Override
        public short getIndex()
        {
            return _byteOffset;
        }

        @Override
        public short[] getTriplet()
        {
            return new short[]
            {
                (short) (_red   & 0xff),
                (short) (_green & 0xff),
                (short) (_blue  & 0xff)
            };
        }

        @Override
        public String getHexString()
        {
            StringBuffer sb = new StringBuffer();
            sb.append(getGnumericPart(_red));
            sb.append(':');
            sb.append(getGnumericPart(_green));
            sb.append(':');
            sb.append(getGnumericPart(_blue));
            return sb.toString();
        }

        private String getGnumericPart(byte color)
        {
            String s;
            if (color == 0)
            {
                s = "0";
            }
            else
            {
                int c = color & 0xff; //as unsigned
                c = (c << 8) | c; //pad to 16-bit
                s = Integer.toHexString(c).toUpperCase(Locale.ROOT);
                while (s.length() < 4)
                {
                    s = "0" + s;
                }
            }
            return s;
        }
    }
}

2.获取调色板方式

public class TestHSSFPalette {
	public static void main(String[] args) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 获取workbook的调色板
		HSSFPalette hssfPalette = workbook.getCustomPalette();
		// 获取调色板中索引为8的HSSFColor
		HSSFColor hssfColor = hssfPalette.getColor((short)8);
		System.out.println(hssfColor.getHexString());
		
		HSSFColor hssfColor2 = hssfPalette.getColor((short)0x30);
		System.out.println(hssfColor2.getHexString());
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值