RGB - HSI Transformation (and reverse)

Hi Folks

Before I go off and try to code a conversion between RGB and HSI values (and reverse transformation) does any one have the bones of a script?

Many thanks
Andrew