How to pull hex color infomation from and XML file

Newbie here... I have hex color information in an XML file and want to fill my rectangles with the colors from the XML file. Can someone explain to me how I can accomplish this?

Answers

  • what xml file?

    what rectangles?

    you're asking us to guess a lot of information before we can help.

    https://processing.org/reference/XML.html
    https://processing.org/reference/fill_.html
    https://processing.org/reference/rect_.html

  • edited April 2016

    In my XML file the setup is like..

    <?xml version="1.0" encoding="UTF-8"?>
    <emotions>
      <feeling amount="872884" color="#FFA401">better</feeling>
      <feeling amount="529789" color="#07548A">bad</feeling>
      <feeling amount="648881" color="#FFF700">good</feeling>
    ....etc...
    

    and in the processing the rectangles i want to fill are from this code. i cant figure out how to take the hex color information from the XML file and use it to fill those rectangles

      myXML= loadXML("feelings.xml");
      XML[] emotions = myXML.getChildren("feeling");
    
      int numEmotions = emotions.length;
      float [] heightArray = new float[numEmotions];
    
     for (int i =0; i<numEmotions; i++) {
        heightArray[i] = emotions[i].getFloat("amount");
    
     for(int i =0; i<numEmotions; i++){
       float h= map(heightArray[i]/17457, minHeight, maxHeight, height*.5, height*.8)
       rect(xloc+=xlocOffset, height-h/2, 10, h/100); 
    
  • You may need to play with this further to get the exact effect you are looking for, but it will read out hex colours from XML and fill rectangles accordingly

        XML xml;
        int yPos = 0;
    
        void setup() {
          xml = loadXML("feelings.xml");
          XML[] children = xml.getChildren("feeling");
    
          for (int i = 0; i < children.length; i++) {
           int amount = children[i].getInt("amount");``
           String hex = children[i].getString("color");
           String name = children[i].getContent();
    
           //println(amount + ", " + hex + ", " + name);
    
           //convert hex
           //https://processing.org/discourse/beta/num_1268833732.html
           String c = "FF" + hex.substring(1);
           fill(unhex(c));
           noStroke();
           rect(0,yPos,width,10); //fill rectangle 
           yPos+=10; // move down the screen
          }
        }
    
  • edited April 2016

    line 17 is taking the #123456, removing the initial # and prepending FF to give "FF123456"

    line 18 is converting this from hex into a value the fill command understands.

    the initial FF is there to set the alpha to fully on, otherwise you'll get a transparent colour.

    it's a nice, short solution. it looks a bit odd but that's mostly getting the data from a string to something processing can use.

  • edited April 2016

    Dunno whether it's any faster or slower, but here's an alternative version w/ replace("#", "FF") in place of "FF" + substring(1): :ar!

    String webColor = "#07548A";
    color p5Color = unhex(webColor.replace("#", "FF"));
    background(p5Color);
    noLoop();
    
Sign In or Register to comment.