How to get the color from a PShape object?

edited April 2018 in How To...

Hey,

is there a way to get the color from a PShape object? I couldn't find an answer in the refrences and also the search didn't spit out something useful.

Thank you so much for your answers!

Answers

  • Hey, thanks for the answer but isn't the get() function only part of the PImage class? I think so because i couldn't get it work. :(

    Perhaps some code is helpful? Voila, that's what i'm trying:

      PShape svg;
    
      void setup() {
      size(640, 360);
    
      svg = loadShape("any.svg");
    
      PShape ch = svg.getChild(10);
    
      //System.out.println("Shape => " + ch.getVertex(0));
      System.out.println("FARBE => " + ch.get());
    
    } 
    
    void draw(){
      background(255);
    
    }
    
  • edited May 2014

    You should have specified it was about a PShape created from SVG...

    Note that a SVG shape can be compound, so can be made of several colors. And even a primitive shape / path can have a gradient color.

    Note that PShape has a fillColor field, but it is protected (not usable directly) and there is no getter for it... :-(

  • edited May 2014

    If fillColor is a protected field, it means we can access it if we inherit the class.
    Problem is that loadShape() is the 1 which instantiates the PShape.
    And we'd need to hack it in order to force it to instantiate our modified PShape subclass! :o3

  • edited May 2014
    public PShape loadShape(String filename) {
      return g.loadShape(filename);
    }
    

    It's much worse, internal loadShape() is called upon an already instantiate PGraphics!
    I believe we'd have to use reflection to reach it now then! :-SS

  • Wuhuu, thanks for the answers!

    Well, i think the easiest way would be to parse the svg file as a xml and handle the data manually. Shouldn't be that hard.

    But could you tell me where you've got the information about the fillColor field? I'm new to processing but familiar with java. Where can i find the references for the processing core?

    Thank you all! :)

  • edited May 2014

    double post. Whenever i post something i get an error that the xml could'nt be read. :( Sorry

  • Thank you! :-h

  • edited May 2014

    Before you go, I've finally succeeded in capturing PShape's fillColor field: :ar!

    static final color getPShapeFillColor(final PShape sh) {
      try {
        final java.lang.reflect.Field f = 
          PShape.class.getDeclaredField("fillColor");
    
        f.setAccessible(true);
        return f.getInt(sh);
      }
    
      catch (ReflectiveOperationException cause) {
        throw new RuntimeException(cause);
      }
    }
    
  • Wohoo awesome!

    Thank you so much! This really helps a lot! :)

  • Hello, this post was from 2014.

    I am just curious.

    Why the follow example will only work if a rendered (like P2D) is chosen

    void setup(){
      size(200,200,P2D);
      PShape bli = createShape();
      bli.beginShape();
      bli.fill(#ff0000);
      bli.vertex(0,0);
      bli.vertex(100,100);
      bli.vertex(10,100);
      bli.endShape(CLOSE);
    
      shape(bli);
    
      fill( bli.getFill(0) ); //this only works with a renderer (p2d, etc)
      rect(40,40,40,40);
    }
    
  • edited April 2018

    PShape.getFill() doesn't seem to be included in the reference:

    ...I'm assuming because it isn't (consistently / completely) implemented?

    Other than defining a renderer that isn't Java2D:

    In general with states in Processing that don't have getters, the workaround is to save the state in a separate variable, then retrieve it later from there:

    color c = color(#ff0000);
    bli.fill(c);
    

    ...or, if you could many create an array or arraylist of colors.

    Possibly related issues about calling PShape.getFill():

Sign In or Register to comment.