Is there code you can use to convert $rgb color codes to hex color codes and vice versa?

$rgb(0,0,127) Turn this into #00007F.

And the other way around. Take a hex color code and convert it back to rgb. I don't mean having a list of all the rgb colors you want and all the hex colors you want and refering to it with the alias. I'm hoping for a tool that will convert these color codes.

Is this possible?