I don't know, I see that you extract the hue value into variable
h, but you convert the wrong value from a different variable
hue:
RGBtoHLS(hi(curFPColor.red),hi(curFPColor.green),hi(curFPColor.blue),h,l,s);
HLStoRGB(hue,l,s,r,g,b);
Edit:
IIRC, setting the dimensions might delete the image. Your code seems to do that.