How does Plotly determine if the color of the hover label is white or black?

I noticed that if I set
hovertemplate = 'some text'
Plotly automatically sets the color of the text (white/black) depending on the background color of the hover box, to get a good contrast. How is that implemented specifically? I would like to replicate the same functionality in another part of my application, but I am not able to find it in the source code.