What is the color contrast algorithm that Plotly uses to determine the label text color on a given background?

Here is an example of a labeled bar chart. Note that the label on the dark blue bar is colored white:

I’d like to duplicate this label contrast behavior in the annotations. I’ve implemented the contrast algorithm detailed here, but this yields slightly different results:

(Note that the label on the light blue bar is now white, in addition to the label on the dark blue bar.)

Is there a way to access the contrast/color value that Plotly is generating for each label? If not, where in the codebase (or online) might I be able to find the contrast algorithm that Plotly uses to determine the label text color?

This looks like the function you’re looking for: https://github.com/plotly/plotly.js/blob/915a52b6a999226fce7faed86107de3c83663952/src/components/color/index.js#L64

1 Like

Thank you, @RedShift! For posterity, here is the same question on StackOverflow: plotly.js - What is the color contrast algorithm that Plotly uses to determine the label text color on a given background? - Stack Overflow.