Question

Is there a method for changing the LSB value of java.awt.Color RGB components?

Was it helpful?

Solution

Turning a bit on:

int value = someValue | 0x1;

Turning a bit off:

int value = someValue & (~0x1);

Toggling the bit on or off if it was off or on before:

int value = someValue ^ 0x1;

In other words: someValue is binary OR'ed with a number with the LSB on so the resulting number will have its LSB on too.

someValue is AND'ed with a number with all bits except LSB on so the resulting number will have LSB OFF.

someValue is XOR'ed with with a number with the LSB on, so the resulting number will have its LSB toggled.

OTHER TIPS

The Color class is immutable, you can't change anything. However, you can create a new color with whatever value you want. For example,

   int oldValue = oldColor.getRGB();
   int newValue = (oldValue & 0xFFFFFF00) | (lsb & 0xFF);
   Color newColor = new Color(newValue);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top