>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.
While reading this thread I was about to propose something similar, but not using the difference (specially that your pair2 plus the difference may go over 255). I'd go with
newR2=newR1*(oldR2/oldR1)
and same for G and B.
You could maybe achieve a similar result by taking the average of the quotients for each R, G and B.