Computing Hue from RGB color space

Can we compute hue from an RGB tensor in a differentiable way?

My model is n RGB color space and I want to write a loss function to minimise the difference in hue component in the input image and the image generated by the model.

Hue Loss = |Hue Input - Hue output|

Thank you

You can find the transformation from RGB to HSV in this post. It looks like it should be differentiable, but let me know, if you run into any trouble implementing it.