factor out common parts of the colorspace conversion