I am calculating luminosity in Javascript using:
luminosity = (5036060 * backgroundColor.red + 9886846 * backgroundColor.green + 1920103 * backgroundColor.blue) >> 24;
For the case where the color is white, ie all 3 RGB values are 255, I am getting a result of -1. I tested explicitly and in Javascript the value "4294967295 >> 24" is -1.
Why?
JavaScript does use 32-bit integers for bitwise operations. The unsigned value +4294967295
has just the same representation as -1
for signed 32-bit integers - a series of 32 1
-bits. If you shift that using the signed right shift operator, it will get filled up with the sign bit, and -1 >> x
always ends up with -1
.
Use the unsigned right shift operator instead:
4294967295 >>> 24 // 255
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With