sinek
sinek

Reputation: 2488

Trying to understand Bitmap.Config.ARBG_8888

I'm trying to understand packing of color bytes for ARGB_8888 format.

The documentation states that packing should be done using this formula:

int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff);

But shouldn't it be instead:

int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);

When I unpack a sample pixel from a ARGB_8888 color encoded bitmap that has all red pixels, I'm using:

    final int r = (p >> 16) & 0xff;
    final int g = (p >> 8) & 0xff;
    final int b = p & 0xff;

Which indeed returns me correct values for every color.

My point is, is it the documentation wrong or I'm missing something?

Upvotes: 2

Views: 175

Answers (1)

samgak
samgak

Reputation: 24417

Yes, you are correct and the documentation is wrong. If you look at the Android source code for Color.java it's done the second way:

* <h4>Encoding</h4>
* <p>The four components of a color int are encoded in the following way:</p>
* <pre class="prettyprint">
* int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
* </pre>

and further down...

@ColorInt
public static int argb(
        @IntRange(from = 0, to = 255) int alpha,
        @IntRange(from = 0, to = 255) int red,
        @IntRange(from = 0, to = 255) int green,
        @IntRange(from = 0, to = 255) int blue) {
    return (alpha << 24) | (red << 16) | (green << 8) | blue;
}

Upvotes: 2

Related Questions