Convert a String to a Hexidecimal

So I have an excel spreadsheet with "6ef442", I load the string and put it into classColor[0], then here's where things get really stupid:

Tab1:

newEvent.coloring(classColor[0]);

Tab2:

void coloring(String chosen) { classColor = unhex(chosen); }

void draw() { eventColor = createShape(RECT, 0, 0, 40, 40); eventColor.setFill(classColor); eventColor.setStroke(false); }

No matter what obscure type of code I try, I cannot get that rectangle to turn 6ef442. Just... Help. If you know how to improve my ridiculous code or if you know how to get just my stupid rectangle to be the color I want it to be. I appreciate it a ton. Thank

Answers

  • we need a runnable example

    classColor[0] - this is an element of an array called classColor

    classColor - this is the whole array? another variable with the same name?

  • edited August 2017
    void coloring(String chosen) {
        classColor = parseInt(chosen, 16);
        print(classColor);
    }
    

    I'm not exactly sutr how to give a better runable example than that due to the way I've set up the script. And yes, sorry, the array is classColors[] and then I have a variable classColor that I JUST want to be my hexcode.

  • And what's chosen in that new example? Just a hex string? And classColor?

    As runable examples go it's not very runable.

  • Is classColor an int (i.e. a color), as per the unhex reference page?

  • edited August 2017
    String classColors = "6fe442";
    
    class Event {
      void coloring(String chosen) {
        classColor = parseInt(chosen, 16);
        print(classColor);
      }
    
      void draw() {
        eventSquare = createShape(RECT, 0, 0, 950, 50);
        eventSquare.setFill(color(0,0,0));
        eventSquare.setStroke(color(255,255,255));
    
        eventColor = createShape(RECT, 0, 0, 40, 40);
        eventColor.setFill(classColor);
        eventColor.setStroke(false);
    
        shape(eventSquare, posX, posY);
        shape(eventColor, posX + 5, posY + 5);
      }
    }
    
    Event newEvent = new Event();
    newEvent.coloring(classColors);
    newEvent.draw();
    
  • Thats the closest I could get to making a runable version.

  • As Jeremy points out, unhex() is designed for this.

    The only catch is that you have no alpha specified. You probably want full alpha but are getting no alpha. So try appending "ff" before calling unhex or adding it to the colour after conversion.

  • doesn't the unhex example work as a runnable example?

    String hs = "6fe442";
    int hi = unhex(hs);
    fill(hi);
    rect(30, 20, 55, 55);
    

    you see an empty rectangle because there's no alpha in your hex string.

    the fill() documentation suggests that changing that to fill(hi, 255); should work. but i found i had to use fill(0xff000000 | hi);

  • Ah, there seems to be a bigger problem with your use of parseInt

    String classColors = "6fe442";
    int c = parseInt(classColors, 16);
    println(c);
    fill(c);
    background(c);
    

    this prints 16, which is wrong.

    it looks like parseInt is being overriden within processing. if you force the use of the java parseInt (a static method on Integer) then this is ok...

    String classColors = "6fe442";
    int c = Integer.parseInt(classColors, 16);
    println(c);
    fill(c);
    background(c);
    
  • https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L9524

    wtf! this is terrible.

    /**
       * Parse a String to an int, and provide an alternate value that
       * should be used when the number is invalid.
       */
    static final public int parseInt(String what, int otherwise) {
    ...
    
  • I'm not sure if you're saying my code is terrible or the difficulty of my problem is terrible. Either way, you're right.

  • No, adding a function with the same name and signature as a standard Java method, like the processing authors did with parseInt, is terrible. It'll just cause problems like yours.

  • edited August 2017

    ... adding a function with the same name and signature as a standard Java method, ...

    • Even in pure Java, parseInt() doesn't exist!
    • We 1st need to prefix it w/ its class: Integer.parseInt().
    • Also, given Processing already provides PApplet::unhex():
      https://Processing.org/reference/unhex_.html
    • There's no need for Integer::parseInt() in this particular case. :>
  • The fill() documentation suggests that changing that to fill(hi, 255); should work.

    • Specifying the 2nd alpha parameter has always overridden the alpha value from the 1st parameter since Processing 1.5.1 for me. $-)
    • Unless some bug had been introduced recently, I can't see why it didn't work for ya! :-/
    • Maybe the buggy implementation is in PShape::setFill(), not in PGraphics::fill()! ^#(^
  • Besides, PApplet::parseInt() isn't the same signature as Integer::parseInt()'s! The former has 2 parameters, while the latter just 1!

    https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

    processing should at least document their parseInt.

  • edited August 2017

    Specifying the 2nd alpha parameter has always overridden the alpha value from the 1st parameter since Processing 1.5.1 for me

    try it. this gives me a non-filled(?) rectangle.

    String hs = "6fe442";
    int hi = unhex(hs);
    fill(hi, 255);
    rect(30, 20, 55, 55);
    
  • edited August 2017

    Processing should at least document their parseInt().

    • Processing got lotsa undocumented public & protected stuff!
    • PApplet::parseInt() is what int() is transpiled into by Processing's "Pre-Processor", when the ".pde" files are concatenated as 1 ".java": :-B
      https://Processing.org/reference/intconvert_.html
    • B/c int is a Java's reserved keyword. And thus it's invalid inside a ".java" file! :ar!
    • Therefore, according to Processing devs' way of "thinking", PApplet::parseInt() (and other undocumented stuff) isn't supposed to be used by us for our own regular sketches.
    • Only for libraries which happen to rely on PApplet's API. 8-}
  • edited August 2017
    • BtW, AFAIK, by default, createShape() uses getGraphics()'s current styles, such as fill() & stroke().
    • So, rather than using buggy PShape::setFill(), invoke fill() before creating a PShape.
    • PShape::setFill() and other set()-ish methods are only needed when we need to redefine styles of already created PShape objects. ;)
  • edited August 2017
    • Indeed you're right @koogs! I've even tested it on Processing 1.5.1 and the result was the same.
    • Funnily, I always thought when using signature fill(rgb, alpha) its alpha parameter would be capable of overriding parameter rgb's alpha channel value. @-)
    • But it turns out it never could! 8-|
    • But then, if it can't, why does this particular signature even exist anyways?! :-q
  • edited September 2017

    Possibly relevant: unhex doesn't produce values that match the way RGB is packed into an int.

    // #6fe442 = RGB 111,228,66
    
    int hi = unhex("6fe442");    //  7332930
    int hi2 = color(111,228,66); // -9444286
    
    println(hi);
    println(hi2);
    
    fill(hi, 255);
    rect(10, 10, 40, 40);
    fill(hi2, 255);
    rect(50, 50, 40, 40);
    

    processing should at least document their parseInt

    This sounds like an issue to report to either the Processing repo or the Processing-docs repo....

  • edited September 2017

    Color format bit logic demo/test, redux:

    // #6fe442 = RGB 111,228,66 = color -9444286
    
    void setup(){
      size(600,200);
      frameRate(1);
    }
    
    void draw() {
      int ic[] = new int[7];
      color cc[] = new color[7];
    
      ic[0] = unhex("6fe442");     //    7332930 - transparent
      ic[1] = unhex("ff6fe442");   //   -9444286 - green
      ic[2] = unhex("6fe442ff");   // 1877230335 - ?
      ic[3] = 0x6fe442;            //    7332930 - transparent
      ic[4] = 0xff6fe442;          //   -9444286 - green
      ic[5] = #6fe442;             //   -9444286 - green
      ic[6] = color(111, 228, 66); //   -9444286 - green
    
      cc[0] = unhex("6fe442");     //    7332930 - transparent
      cc[1] = unhex("ff6fe442");   //   -9444286 - green
      cc[2] = unhex("6fe442ff");   // 1877230335 - ?
      cc[3] = 0x6fe442;            //    7332930 - transparent
      cc[4] = 0xff6fe442;          //   -9444286 - green
      cc[5] = #6fe442;             //   -9444286 - green
      cc[6] = color(111, 228, 66); //   -9444286 - green
    
      // for instance color c = 0xCC006699. In that example, CC (the hex value of 204) is the alpha value, and the remainder is identical to a web color
    
      float hunit;
    
      background(0);
      color c = color(random(255), random(255), random(255));
      background(c);
      fill(0);
      text(c, width/2-30, 12);
    
      pushMatrix();
      hunit = (float)(height-1)/(ic.length+1);
      for (int i=0; i<ic.length; i++) {
        translate(0, hunit);
        fill(ic[i]);
        rect(0, 0, width/2, hunit);
        fill(0);
        text("i"+i+" "+ic[i],5,12);
      }
      popMatrix();
    
      translate(width/2,0);
    
      pushMatrix();
      hunit = (float)(height-1)/(cc.length+1);
      for (int i=0; i<cc.length; i++) {
        translate(0, hunit);
        fill(cc[i]);
        rect(0, 0, width/2-1, hunit);
        fill(0);
        text("c"+i+" "+cc[i],5,12);
      }
      popMatrix();
    
    }
    

    From a technical standpoint, colors are 32 bits of information ordered as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB where the A's contain the alpha value, the R's are the red value, G's are green, and B's are blue.

    -- https://processing.org/reference/color_datatype.html

  • edited August 2017
    int hi = unhex("6fe442");    //  7332930
    int hi2 = color(111,228,66); // -9444286
    

    same thing. only color has automatically added 0xff000000 making it negative

    int hi = unhex("ff6fe442");    // -9444286
    int hi2 = color(111,228,66); // -9444286
    
  • edited September 2017

    @Tallen121 -- so, in summary:

    Don't unhex("6ef442"). Instead, prepend ff (or whatever your alpha / transparency should be.

    unhex("ff6ef442");
    

    Because unhex doesn't prepend ff, any 6-character hex string that you unhex and read as a color will have an alpha of 0 (the first two characters out of 8), and will be transparent. Prepend ff to see the color, as per the example on the unhex reference page:

    String hs = "FF006699";
    

    If you want to use 6-character color hexes, for convenience, you could wrap that in a function :)

    int colorUnhex(String s){
      s = "ff" + s;
      return unhex(s);
    }
    
Sign In or Register to comment.