Another small release, this time just so I can save another 1% of storage space. The actual change was pretty simple but interesting so I will mention it here. This was the dim() function in the previous Custom Blinklib version (pretty close to the one in the official Blinklib):
Color dim(Color color, byte brightness) {
return {1, (byte)((color.r * brightness) / MAX_BRIGHTNESS),
(byte)((color.g * brightness) / MAX_BRIGHTNESS),
(byte)((color.b * brightness) / MAX_BRIGHTNESS)};
}
MAX_BRIGHTNESS is 255.
One way to do faster division is using bit-shifts instead, but that has the annoying property of only working with powers of two, which 255 is not. BUT it is close enough that we can do a simple approximation taking advantage of the fact that integer divisions always rounds the number down. Here is the new version:
Color dim(Color color, byte brightness) {
return {1, (byte)((color.r * (brightness + 1)) >> 8),
(byte)((color.g * (brightness + 1)) >> 8),
(byte)((color.b * (brightness + 1)) >> 8)};
}
One simple way to show how this performs is testing the bounds (as it is easy to see that the middle ranges would be ok). Let’s assume we are dealing with a mid-brightness white. Each of the colors components will be 15 (it is 5 a bit color component, so it goes from 0 to 31):
brightness = 0 : {0, 0, 0}
brightness = 255 : {15, 15, 15}
Which is what we expected.
New In This Release
- Reduce storage requirements by another 1% by slightly rewriting the dim() and lighten() functions.