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 updated version of #1711 (moved into separate branch to avoid sync mess-up).