Clamped rgba format color output

Update for #1693.
Also tried to clean up related stuff a bit by collecting duplicated code into separate functions.