A simpler way to dim an RGB value is to multiply the red, green and blue component values by the same percentage. For example
Red: 240 * 50% = 120
Blue: 170 * 50% = 85
Green: 40* 50% = 20
When the dimming reaches the lowest levels, the colour may change, but there is nothing that can easily be done about this, because the Arduino's PWM control is normally only 8 bit.
Red: 240 * 5% = 12
Blue: 170 * 5% = 8 <-- ideally this would be 8.5 to maintain the same colour
Green: 40* 5% = 2
PS.
//Print out debug info at Serial output window
Serial.print("R=");
Serial.println( constrain(R.toInt(),0,255));
Serial.print("G=");
Serial.println(constrain(G.toInt(),0,255));
Serial.print("B=");
Serial.println( constrain(B.toInt(),0,255));
The above may appear to limit the r, g & b values to the range 0 to 255. But in reality it does nothing...