打印像素十六进制值的颜色名称

时间:2013-03-06 11:47:01

标签: java

我有包含颜色名称和十六进制值的xml文件。我试图匹配xml文件十六进制值与系统使用给定代码

返回的十六进制值
 robot=new Robot();
 Point  coord = MouseInfo.getPointerInfo().getLocation(); 
 color = robot.getPixelColor((int)coord.getX(), (int)coord.getY());

 hexString = Integer.toHexString(color.getRGB());
 hexString = hexString.substring(2, hexString.length());
 hexString = hexString.toUpperCase(); 

但此代码的十六进制值并存储在xml文件中永远不会匹配.. xml文件有超过2000种颜色,这就是为什么我不能粘贴整个xml文件

<?xml version="1.0" standalone='no'?>
<!DOCTYPE ColorDefinition SYSTEM "color.dtd">

<ColorDefinition>
<ColorSet id="VGA" language="English">

<Property>
<description>Choose a color name from 16 colors of VGA.</description>
<gray>Gray</gray>
</Property>

<color name="Black" rgb="000000"/>
<color name="White" rgb="ffffff"/>
<color name="Green" rgb="008000"/>
<color name="Silver" rgb="c0c0c0"/>
<color name="Lime" rgb="00ff00"/>
<color name="Gray" rgb="808080"/>
<color name="Olive" rgb="808000"/>
<color name="Yellow" rgb="ffff00"/>
<color name="Maroon" rgb="800000"/>
<color name="Navy" rgb="000080"/>
<color name="Red" rgb="ff0000"/>
<color name="Blue" rgb="0000ff"/>
<color name="Purple" rgb="800080"/>
<color name="Teal" rgb="008080"/>

我知道为什么两个十六进制值都不匹配的原因是什么....

读取xml文件并匹配两个十六进制值的代码(xml文件十六进制值和像素的十六进制值)

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File file = new File("colore.xml");
        if (file.exists()) {
            Document doc = db.parse(file);
            Element docEle = doc.getDocumentElement();

            NodeList colorList = docEle.getElementsByTagName("color");

            if (colorList != null && colorList.getLength() > 0) {
                for (int i = 0; i < colorList.getLength(); i++) {

                    Node node = colorList.item(i);                     

                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                       NodeList mycolorList = doc.getElementsByTagName("color");
                        Element colorElement = (Element) mycolorList.item(i);

                        String colorName = colorElement.getAttribute("name");
                        String rgbvalue = colorElement.getAttribute("rgb");

                            if(rgbvalue.equalsIgnoreCase(hexString))
                            {
                               System.out.println("color name: " + colorName + " Hex: "      


                            }
                            else
                            {  
                                System.out.println("comparsion if statement not working 
                                                     here XML FIle HEX: "      
                                        + rgbvalue + " mouse click hex: " + hexString);            
                            }

现在这个else语句显示在输出....像素十六进制值与xml文件中的每种颜色(2222种颜色)进行比较,并在上面显示其他语句2222次..(如果两个十六进制值匹配则相应的颜色名称应为在输出中打印...)

0 个答案:

没有答案