# Do BW (alpha) values need to be int \[0..255\]?

**URL:** <https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489>\
**Category:** Beginners\
**Created:** [March 28, 2023, 8:24am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489 "2023-03-28T08:24:07Z")\
**Posts on this page:** 8\
**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:** [March 28, 2023, 8:24am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/1 "2023-03-28T08:24:07Z")

</div>

How many levels of Black&White are allowed in the pixel alpha values? I’m trying to work this out by trial and error, and confusing myself.

The reference says the value needs to be between 0…255, but not as far as I can tell what kind of data type it needs to be.

If I define a BW color value with a _literal_ non-integer between 0…255, it runs. But in the display window I don’t see any difference between the two greys. Whereas that shade difference should be perceptible. So, what is going on here? Is it being forgiving here and casting my float to int?

```auto
background(0);
noStroke();
 size(600, 600);
 fill(200.5);
 rect(0,0,300, 600);
 fill(200);
 rect(300, 0, 300, 600);

```

However, if I define the BW value I am testing as a color, then it will only run if it’s an int.

```auto
color THISWORKS = 200;
background(0);
noStroke();
 size(600, 600);
 background(THISWORKS);

```

```auto
color THISDOESNTWORK = 200.5;
background(0);
noStroke();
 size(600, 600);
 background(THISDOESNTWORK);

```

These two ways of creeping up on the question seem to give different answers. The end use here is that I am exploring an animation where I change an alpha value for an image that I am using in a blend.

Thanks for your advice!

---

<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:** [March 28, 2023, 9:51am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/2 "2023-03-28T09:51:47Z")

</div>

```auto
color THISDOESNTWORK = 200.5;

```

This does not work because the data type `color` is an alias for `int` and you can’t store a float in an integer variable.

The function `fill(...)` accepts floats and will convert to an int depending on `colorMode` (look it up in the reference.

My last point is that you are confusing greyscale with alpha (transparency) so the statements

```auto
background(200);
fill(200);

```

are both using a fully opaque greyscale value of 200.

```auto
fill(200, 25);

```

here the fill is set to 200 greyscale with an alpha value 25

Note: the `background` method ignores alpha / transparency values.

---

<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:** [March 28, 2023, 10:17am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/3 "2023-03-28T10:17:27Z")

</div>

Yes, that’s the info I needed. Thank you. (Re confusion of greyscale and alpha, my intent was to use this a simplified example. Maybe it was wrongly oversimplified. The thinking here was that when you apply a greyscale image as a mask, it then acts as an alpha. So in troubleshooting the alpha, I’ve been playing with using a greyscale then compositing.)

---

<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:** [March 28, 2023, 12:20pm UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/4 "2023-03-28T12:20:40Z")

</div>

Boiled down a bit more, my original question was whether allowable values for a colour spec are only the _discrete elements_ of the range[0…255] - or whether they can they be anything _within_ that range? I am still puzzled about whether Processing is being forgiving and coercing the fractional value to int[0…255], or whether it actually is storing additional “resolution” on the colour space.

---

<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:** [March 28, 2023, 1:30pm UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/5 "2023-03-28T13:30:09Z")

</div>

> [@MuddyGardener](#):
>
> I am still puzzled about whether Processing is being forgiving and coercing the fractional value to int[0…255]

It depends on the [colorMode](https://processing.org/reference/colorMode_.html) so I strongly recommend you read the reference.

You can use `colorMode` to can change how parameters to `fill`, `stroke` are interpreted. For instance a colour using other modes such as HSB and different ranges such as 0.0 \le V \le 1.0

The default mode is (R,G,B,A) where each of the 4 channels is represented by an integer in the range 0 \le V \le 255

Processing will accept both `float` and `int` parameters and convert them to the default mode for storage in RAM based on the `colorMode` currently in force.

---

<div class="post-metadata">

**Author:** ![esc746](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/esc746/32/4258_2.png) [@esc746](https://discourse.processing.org/u/esc746)\
**Post date:** [March 28, 2023, 1:54pm UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/6 "2023-03-28T13:54:46Z")

</div>

Color has only one byte per component and does not have further resolution. By default each component is an integer from 0 to 255. The Color type is just a convenient interface to the actual data, which is stored internally as an integer number, e.g. 0xffFFCC00.

I often find it easier to work with a different colour model and scale. To do this, stick a colorMode() statement in setup(). [reference](https://processing.org/reference/colorMode_.html)

The following snippet changes to the hue-saturation-brightness model that’s far more logical (to my way of thinking). And it also scales each value from 0 to 1 using floating point arguments. Again, much simpler to know where you are on the scale.

```auto
colorMode(HSB, 1.)
color c = color(.2, 1, 1);

```

As you can see, this works even if you forget the dot after the “1” since Processing tends to be forgiving about such things. (Java is not.)

---

<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:** [March 31, 2023, 10:26am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/7 "2023-03-31T10:26:21Z")

</div>

That’s very clear. And much clearer than the Reference. (I have read the Reference many many times but still had questions.)

---

<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:** [March 31, 2023, 10:42am UTC](https://discourse.processing.org/t/do-bw-alpha-values-need-to-be-int-0-255/41489/8 "2023-03-31T10:42:02Z")

</div>

Thank you. Yes, HSB I find a lot easier to work with, conceptually and perceptually. Re my original question - it makes perfect sense that allowable values depend on the colorMode, and of course you must be able to have floats if you can set the range 0…1. Of course. And it also makes perfect sense that data type of a color depends at the end of the day on the fundamental underlying representation, which is some mysterious flavour of int. Again, of course. But I found it genuinely confusing how those two things play out against each other: which is that the allowable actions differ by context in a way that’s genuinely weird at first glance. (And even after reading the documentation a dozen times.)
