# Bit masking (or not)?

**URL:** <https://discourse.processing.org/t/bit-masking-or-not/45177>\
**Category:** Processing.py\
**Created:** [October 14, 2024, 9:17am UTC](https://discourse.processing.org/t/bit-masking-or-not/45177 "2024-10-14T09:17:50Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 27, 2024, 4:56pm UTC](https://discourse.processing.org/t/bit-masking-or-not/45177/11 "2024-10-27T16:56:33Z")

</div>

> [@paulstgeorge](#):
>
> The widely used algorithms mentioned have floats for RGB values. For example,  
> `Luminance = 0.2126 * Red + 0.7152 * Green + 0.0722 * Blue`

The R, G and B did not need to be floats and can be efficiently extracted from the color integer with bit manipulation.  
The luminance will be a float.  
You may need to use floating point math in such cases and can cast results as required.

I edited my previous post for clarity on context.

@solub made some approximations [here](https://discourse.processing.org/t/color-to-grayscale-algorithm/45171/5) to optimize performance:

> [@Color to grayscale algorithm](https://discourse.processing.org/t/color-to-grayscale-algorithm/45171/5):
>
> ```auto
> # Calculate the true relative luminance using scaled weights:
> # Luminance = 0.2126 * Red + 0.7152 * Green + 0.0722 * Blue
> # Approximation: 0.2126 * 256 = 54, 0.7152 * 256 = 183, 0.0722 * 256 = 18
> lum = (54 * r + 183 * g + 18 * b) >> 8 # Bit-shift by 8 (dividing by 256)
> 
> ```

There are also examples of extracting and setting colors without the helper functions:  
_[Color to grayscale algorithm - #5 by solub](https://discourse.processing.org/t/color-to-grayscale-algorithm/45171/5)_

`:)`

---

_[View the full topic](https://discourse.processing.org/t/bit-masking-or-not/45177)._
