# Understanding the function of CheckAlpha boolean PGraphics argument

**URL:** <https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777>\
**Category:** Coding Questions\
**Created:** [February 1, 2023, 9:53am UTC](https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777 "2023-02-01T09:53:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [February 1, 2023, 9:53am UTC](https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777/1 "2023-02-01T09:53:51Z")

</div>

I am trying to figure out what the CheckAlpha argument to Pgraphics does - and how and why - and have chased my confusion down to line 281 of LINK UPDATED [the core docs](https://github.com/ruby-processing/processing-core/blob/master/src/main/java/processing/core/PImage.java)

```auto
   if ((pixels[i] & 0xff000000) != 0xff000000)

```

It’s checking for transparency, and setting the mode to ARGB if it finds it. But:

- why is it checking for transparency?
- how does this conditional work?

It’s ANDing the value of the pixel with a hex value, and checking that the result isn’t that value. Somehow, this tells you about whether there is transparency.

Can someone unpack this for me?

thannks!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 1, 2023, 10:05am UTC](https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777/2 "2023-02-01T10:05:52Z")

</div>

Color in encoded in a 32 bit integer 8 bits for each channel alpha, red, green and blue like this

0xAARRGGBB

Now if we perform the AND in the conditional we are doing

```auto
     0xAARRGGBB
& 0xFF000000 we get
     0xAA000000

```

All the bits in the RGB chanels become 0 but the bits in the alpha channel are unchanged.

If fact the conditional statement is checking that the alpha channel is 0xFF in other words fully opaque.

> [@MuddyGardener](#):
>
> why is it checking for transparency?

No idea the link didn’t work 😉

---

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [February 1, 2023, 10:12am UTC](https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777/3 "2023-02-01T10:12:13Z")

</div>

Tx let me check the link and fix if I can - UPDATE: link fixed  
Basically it seems to be bailing out silently if it detects transparency?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 1, 2023, 10:33am UTC](https://discourse.processing.org/t/understanding-the-function-of-checkalpha-boolean-pgraphics-argument/40777/4 "2023-02-01T10:33:09Z")

</div>

```auto
  /**
   * Check the alpha on an image, using a really primitive loop.
   */
  private void checkAlpha() {
    if (pixels == null) {
      return;
    }

    for (int i = 0; i < pixels.length; i++) {
      // since transparency is often at corners, hopefully this
      // will find a non-transparent pixel quickly and exit
      if ((pixels[i] & 0xff000000) != 0xff000000) {
        format = ARGB;
        break;
      }
    }
  }

```

The PImage constructor initializes the `format` attribute to `RGB` and this method changes the `format` attribute to `ARGB` if it finds a pixel with transparency i.e. `0xAA != 0xFF`

Obviously some other methods in this class need to be aware of `format` for their implementation

It is also a `private` function so we can’t use it directly.
