Here’s my attempt, for both Java & Python modes:
/**
* PShape Change Alpha Color (v1.0)
* GoToLoop (2018/Jul/02)
*
* https://Discourse.Processing.org/t/
* is-it-possible-to-change-the-transparency-of-a-pshape-object-in-draw/1429/2
*/
static final int DOTS = 300, DIAM = 020, RAD = DIAM >> 1;
PShape shp;
void setup() {
size(800, 600, P2D);
smooth(8);
noLoop();
strokeWeight(DIAM);
shp = createShape();
shp.beginShape(POINTS);
for (int w = width - RAD, h = height - RAD, i = 0; i < DOTS; ++i) {
shp.stroke((color) random(PImage.ALPHA_MASK));
shp.vertex(random(RAD, w), random(RAD, h));
}
shp.endShape();
}
void draw() {
clear();
shape(shp, 0, 0);
}
void mousePressed() {
if (mouseButton == LEFT) for (int i = 0; i < DOTS; ++i) {
final color stroke = shp.getStroke(i);
final color rawStroke = stroke << 010 >>> 010;
final color alpha = (color) random(0x100) << 030;
final color alphaStroke = alpha | rawStroke;
shp.setStroke(i, alphaStroke);
} else if (mouseButton == RIGHT) for (int i = 0; i < DOTS;
shp.setStroke(i++, (color) random(PImage.ALPHA_MASK)));
else for (int w = width - RAD, h = height - RAD, i = 0; i < DOTS;
shp.setVertex(i++, random(RAD, w), random(RAD, h)));
redraw();
}
"""
PShape Change Alpha Color (v1.0.3)
GoToLoop (2018/Jul/02)
https://Discourse.Processing.org/t/
is-it-possible-to-change-the-transparency-of-a-pshape-object-in-draw/1429/2
"""
ALL_COLORS = PImage.ALPHA_MASK
NO_ALPHA = ~PImage.ALPHA_MASK
DOTS = 300
RANGE = tuple(range(DOTS))
DIAM = 020
RAD = DIAM >> 1
def setup():
size(800, 600, P2D)
smooth(8)
noLoop()
strokeWeight(DIAM)
global shp
shp = createShape()
w, h = width - RAD, height - RAD
shp.beginShape(POINTS)
for i in RANGE:
shp.stroke(int(random(ALL_COLORS)))
shp.vertex(random(RAD, w), random(RAD, h))
shp.endShape()
def draw():
clear()
shape(shp, 0, 0)
def mousePressed():
if mouseButton == LEFT:
for i in RANGE:
stroke = shp.getStroke(i)
rawStroke = stroke & NO_ALPHA
alpha = int(random(0x100)) << 030
alphaStroke = alpha | rawStroke
shp.setStroke(i, alphaStroke)
elif mouseButton == RIGHT:
for i in RANGE: shp.setStroke(i, int(random(ALL_COLORS)))
else:
w, h = width - RAD, height - RAD
for i in RANGE: shp.setVertex(i, random(RAD, w), random(RAD, h))
redraw()