Do pixels[] follow the same colour model as fill()?

edited November 2015 in Python Mode

I'm writing some code that generates a bitmap, pixel by pixel with some partial transparency. The pixels that are partially transparent don't show the colour I expect. Here's an example to illustrate the problem:

background(0,0,0)
size(100,100)
noStroke()
c=color(0x7F7F0000)
fill(c)
rect(50,50,50,50)
loadPixels()
for y in range(50):
    for x in range(50):
        pixels[x+width*y]=c
updatePixels()

I'd expect this to draw two identical squares. The square drawn by 'rect' is as I would expect (dull red). The square drawn by 'pixels' is unexpectedly bright. Is this expected behaviour? And if so, how should I modify my code?

Tagged:

Answers

  • pixeltest1

    To show the problem, I added a 'saveFrame()' to the end of my sample code. This generated a third colour which I have screen-shotted next to the output of the sample code.

    For the record, since I forgot to include this in the original message, this is on Processing 3.0.1 on OS X 10.11.1

  • Answer ✓

    If the fill color is semi-transparent then when you used fill the resulting colour is a merger of the background and fill colour.

    When you use pixels you are over-writing the background colour. In this case the alpha value has no effect because the main sketch canvas does not support transparency,

  • Thanks, quark. I've switched to using createGraphics, drawing to that, and saving from there. Everything now works as expected. I can ever use image to pop my work onto the canvas after it's complete. Thanks again for a speedy response.

Sign In or Register to comment.