My solution to the problem

There may be a better solution.

(May be a solution using the set_color_conversion function.)

My solution is not elegant but it works.

This program performs the following sequence of operations: