读书人

jxl 自定义背景色彩十六进制转化为

发布时间: 2012-11-23 22:54:33 作者: rapoo

jxl 自定义背景颜色——十六进制转化为Colour .

转自:http://blog.csdn.net/dongaoyuan/article/details/7616940

?

?

import?java.awt.Color;??

import?jxl.format.Colour;??

/**?

?*?将十六进制颜色转换为jxl可用的颜色?

?*?@author?AoYuan.Dong*/??

public?class?ColourUtil?{?

public static Colour getNearestColour(String strColor) {
??Color cl = Color.decode(strColor);
??Colour color = null;
??Colour[] colors = Colour.getAllColours();
??if ((colors != null) && (colors.length > 0)) {
???Colour crtColor = null;
???int[] rgb = null;
???int diff = 0;
???int minDiff = 999;
???for (int i = 0; i < colors.length; i++) {
????crtColor = colors[i];
????rgb = new int[3];
????rgb[0] = crtColor.getDefaultRGB().getRed();
????rgb[1] = crtColor.getDefaultRGB().getGreen();
????rgb[2] = crtColor.getDefaultRGB().getBlue();

????diff = Math.abs(rgb[0] - cl.getRed())
??????+ Math.abs(rgb[1] - cl.getGreen())
??????+ Math.abs(rgb[2] - cl.getBlue());
????if (diff < minDiff) {
?????minDiff = diff;
?????color = crtColor;
????}
???}
??}
??if (color == null)
???color = Colour.BLACK;
??return color;
?}

?

?????

?public?static?void?main(String[]?args){??

????System.out.print(getNearestColour("#FFFFFF"));??

}??

}

读书人网 >Web前端

热点推荐