Setting alpha value

edited April 2014 in How To...

I see that with the alpha() function we can extract the alpha value of a color. But is there a way to set this value without using fill? I would like to set the alpha value to an unknown color.



  • Answer ✓

    If I understand your question correctly, you can do something like this:

    //Red, no alpha
    color c = color(255, 0, 0);
    //Change the alpha (not the most elegant, perhaps...)
    c = color(red(c), green(c), blue(c), 127);

    There is also a way to do this by shifting bits, but I don't know how to do that off of the top of my head...

  • edited April 2014
    color c = pixels[0];
    println(hex(c));              // pixel color
    color a = c >>> 030;
    println(hex(a, 2));           // extracted alpha octet
    println(hex(a >>= 1, 2));     // half of alpha
    println(hex(c &= 0xFFFFFF));  // clear alpha octet
    println(hex(c |= a << 030));  // reinserts modified alpha
Sign In or Register to comment.