Loading...
Logo
Processing Forum
Given a color, I want to use bitshifting to change the alpha value.

I am not sure how to use >> and << to do this.

I currently create the new color like this:

color newAlphaColor = color( red(oldColor), green(oldColor), blue(oldColor), newAlpha );

How would I use >> and << to do this?

Thanks!

Replies(2)

The article What is a color in Processing? (first one of the Technical FAQ, in case Zoho messes up the link) should help you.
Copy code
  1. int alpha = 255;

  2. void setup() {
  3.   size(200, 200, P2D);
  4.   loadPixels();
  5. }

  6. void draw() {
  7.   // Alpha-Alpha-Red-Red-Green-Green-Blue-Blue
  8.   background(0xffff0000); // Red underneath pixels, full opacity
  9.   
  10.   for (int i = 0; i < width*height; i++) {
  11.     /* Note: This normally should be one line like this:
  12.     pixels[i] = (alpha<<24)|(red<<16)|(green<<8)|blue
  13.     But I separated it to make it easier to see */
  14.     
  15.     pixels[i] = alpha<<24; // Alpha
  16.     pixels[i] += 0<<16; // Red
  17.     pixels[i] += 255<<8; // Green
  18.     pixels[i] += 0; // Blue
  19.   }
  20.   updatePixels();
  21.   
  22.   alpha++;
  23.   if (alpha > 255) alpha = 0;
  24. }