Clamped rgba format color output

Update for #1693.
 Also tried to clean up related stuff a bit by collecting duplicated code into separate functions.
---
This is just an updeted version of #1711 (moved into separate branch to avoid synch mess-up).