micycle1 / PeasyGradients

Easy and sophisticated gradients in Processing
https://micycle1.github.io/PeasyGradients/
MIT License
16 stars 1 forks source link

Gradient with transparency in a colorstop #6

Closed mheesakkers closed 2 years ago

mheesakkers commented 2 years ago

Hi Micycle!

I was playing around with your library and tried adding a transparency / alpha to my gradients, this doesn't seem to work. F.e. gradient = new Gradient( color(200, 100, 100, 10), color(0, 0, 0) ); I thought this feature may be missing?

micycle1 commented 2 years ago

It does support transparency. But transparency will only show up in 2D gradients when they're written directly to a PImage (with .setRenderTarget(PImage), then drawn afterwards. By default, gradients are rendered to the pixels[] array of the canvas, but transparency doesn't really work here since the previous pixels[] values are overwritten. This is something I should mention in docs.

pGradients = new PeasyGradients(this);
gradientLayer = createGraphics(WIDTH, HEIGHT);
pGradients.setRenderTarget(gradientLayer);
gradient = new Gradient(color(255,0,0,0), color(0,255,0,255));
pGradients.linearGradient(gradient, 0);
image(gradientLayer, 0, 0);
mheesakkers commented 2 years ago

Ah.. I was trying to mask the gradient layer and this causes the transparency to not work I think. Am I doing something wrong?

PeasyGradients peasyGradients = new PeasyGradients(this);

// PGraphics Gradient Holder
PGraphics gradientFill = createGraphics(floor(size), floor(size));

// Gradient objects
Gradient gradient = new Gradient(
    color(200, 100, 100, 10), 
    color(0, 0, 0, 50)
);

// Draw gradient into PGraphics
peasyGradients.setRenderTarget(gradientFill);
peasyGradients.radialGradient(
    gradient, 
    new PVector(gradientFill.width * 0.5, gradientFill.height * 0.5), 
    0.5
);

// Mask
mask = createGraphics(floor(size), floor(size));
mask.beginDraw();
mask.background(#000000);
mask.fill(#ffffff);
mask.ellipse(mask.width * 0.5, mask.height * 0.5, size, size);
mask.endDraw();

gradientFill.mask(mask);

image(gradientFill, 0, 0);
mheesakkers commented 2 years ago

Looking at your code it seems the transparency seems to be reversed around. Red should be transparant color(255,0,0,0)and green color(0,255,0,255) should be solid right? Maybe I should make a new issue - loving this library btw! ;)

Screenshot 2022-06-01 at 21 47 44