I did find one adjustment so far. The last line should be:
pair2Y = rgb(pair2Yred, pair2Ygreen , pair2Yblue )
This fixes that line but still results in another color than the one expected.
I am continuing to verify the code.
Let me know if you find what it is in the meantime.