getting color value while colored object is tranparent (alpha == 0);

edited November 2013 in Programming Questions

Here is the thing. Lets say there are the squares. The one is on the top of the other (it is rendered in the same spot). And the top (last rendered shape) is set to alpha=0,so we see the first one. How do we get the color of the top one (transparent)? example:

void setup() {
size(800,800);
    }
void draw() {
    color mouseColor = get(mouseX, mouseY);

    fill(255,0, 0, 255); 
    rect(0, 0, 50, 50);  // Opaque red square.

    fill(0, 0, 255, 0); 
    rect(0, 0, 50, 50); // Transparent blue square at the top of the red one.

    println( red(mouseColor), green(mouseColor), blue(mouseColor), alpha(mouseColor));
    }

On the top code we can see a red square, although there is a transparent blue one over it. When running the code and I "mouse over" the shaped square, I get printed the values: 255.0, 0.0,0.0, 255.0 that is the visible square. How do I get the values of the blue square?

Answers

  • edited November 2013

    When something is drawn to canvas, Processing automatically blends the top color being used
    w/ the 1 already present in the background.
    And any transparent color becomes its analogous opaque 1!

    And if you need to know the color of each unit drawn, you are better off storing that info in some structure!

  • The real use of this thing was the using of one transparent color over duplicated shapes so to imitate object picking. I have one shape, and one copy over it with a specific color and alpha==0, so with a simple if (colorA == colorB) {... I can recognize my shape whenever it might be transposed. picking* library only works with proc1.5. Is there any alternative property instead of color that I can attach to a shape and recognize it with a simple method and given coordinates (x,y) ?

  • One common way is to draw (in opaque colors!) the shapes on a non-visible PGraphics. You can then pick up the color corresponding to coordinates.

  • Is this being done by creating a PShape and setting it un-visible ?

  • No, by creating a PGraphics and by drawing the shapes on it. By shapes, I mean circles, rectangles or anything you need to draw.

  • Ok. Thanx.Any tutorial for PGraphics? cause I cannot understand anything. As many examples I check, still nothing.

  • When you use the rect() function, for example, you are drawing on the main PGraphics, hidden in most sketches, and at the end of draw(), it is just displayed on the sketch area.

    Now, you can create your own PGraphics object, say graphics, and when you need to draw on it, you prefix the Processing functions with its name:

    graphics.beginDraw(); // Don't forget this one!
    graphics.fill(#55AAFF);
    graphics.noStroke();
    graphics.rect(10, 10, graphics.width - 20, graphics.height - 20);
    graphics.endDraw(); // Necessary too
    

    Then, you can draw this PGraphics like you would do with a PImage, or use graphics.get() to have the color of a pixel on it, etc.

Sign In or Register to comment.