@staticmethod<EOL><INDENT>def _background_gradient(s, cmap='<STR_LIT>', low=<NUM_LIT:0>, high=<NUM_LIT:0>,<EOL>text_color_threshold=<NUM_LIT>):<DEDENT>
if (not isinstance(text_color_threshold, (float, int)) or<EOL>not <NUM_LIT:0> <= text_color_threshold <= <NUM_LIT:1>):<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>with _mpl(Styler.background_gradient) as (plt, colors):<EOL><INDENT>smin = s.values.min()<EOL>smax = s.values.max()<EOL>rng = smax - smin<EOL>norm = colors.Normalize(smin - (rng * low), smax + (rng * high))<EOL>rgbas = plt.cm.get_cmap(cmap)(norm(s.values))<EOL>def relative_luminance(rgba):<EOL><INDENT>"""<STR_LIT>"""<EOL>r, g, b = (<EOL>x / <NUM_LIT> if x <= <NUM_LIT> else ((x + <NUM_LIT>) / <NUM_LIT> ** <NUM_LIT>)<EOL>for x in rgba[:<NUM_LIT:3>]<EOL>)<EOL>return <NUM_LIT> * r + <NUM_LIT> * g + <NUM_LIT> * b<EOL><DEDENT>def css(rgba):<EOL><INDENT>dark = relative_luminance(rgba) < text_color_threshold<EOL>text_color = '<STR_LIT>' if dark else '<STR_LIT>'<EOL>return '<STR_LIT>'.format(<EOL>b=colors.rgb2hex(rgba), c=text_color<EOL>)<EOL><DEDENT>if s.ndim == <NUM_LIT:1>:<EOL><INDENT>return [css(rgba) for rgba in rgbas]<EOL><DEDENT>else:<EOL><INDENT>return pd.DataFrame(<EOL>[[css(rgba) for rgba in row] for row in rgbas],<EOL>index=s.index, columns=s.columns<EOL>)<EOL><DEDENT><DEDENT>
Color background in a range according to the data.