-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Color text based on background color when using _background_gradient()
#21263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
36dd331
04d30f8
6ce03f3
a878359
e7e8444
ee79b10
075cd54
5023dd6
163c364
ba6f981
a6dc14d
8993dff
6eb0930
c30255b
2c34fb9
ae2e849
7be4559
41911af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1036,26 +1036,24 @@ def test_background_gradient(self): | |
'color: #000000'] | ||
|
||
@td.skip_if_no_mpl | ||
def test_text_color_threshold(self): | ||
@pytest.mark.parametrize("c_map", [None, 'YlOrRd']) | ||
def test_text_color_threshold(self, c_map): | ||
df = pd.DataFrame([[1, 2], [2, 4]], columns=['A', 'B']) | ||
for c_map in [None, 'YlOrRd']: | ||
result = df.style.background_gradient(cmap=c_map)._compute().ctx | ||
for res in result: | ||
bg_color = result[res][0].split(' ')[1] | ||
assert bg_color in ['#fde725', '#ffffcc', | ||
'#800026', '#440154'], ( | ||
"Unexpected background color returned from " | ||
"`style.background_gradient()`") | ||
text_color = result[(res)][1].split(' ')[1] | ||
if bg_color in ['#fde725', '#ffffcc']: | ||
assert text_color == '#000000' | ||
elif bg_color in ['#800026', '#440154']: | ||
assert text_color == '#f1f1f1' | ||
for res in result: | ||
if result[res][0].split(' ')[1] in ['#fde725', '#ffffcc']: | ||
assert result[(res)][1].split(' ')[1] == '#000000' | ||
elif result[res][0].split(' ')[1] in ['#800026', '#440154']: | ||
assert result[(res)][1].split(' ')[1] == '#f1f1f1' | ||
result = df.style.background_gradient(cmap=c_map)._compute().ctx | ||
test_colors = {None: {(0, 0): ('#440154', '#f1f1f1'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the variable here to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a nested dict you should also send this in via parametrization, so you would have |
||
(1, 0): ('#fde725', '#000000')}, | ||
'YlOrRd': {(0, 0): ('#ffffcc', '#000000'), | ||
(1, 0): ('#800026', '#f1f1f1')}} | ||
# Light text on dark background | ||
assert result[0, 0][0].split(' ')[1] == test_colors[c_map][0, 0][0], ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you splitting this? Just make your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was doing this to be able to raise different assertion messages for the background and foreground color. But since pytest shows the expected and current value anyways, this should be easy to trace done without separate assertion messages. |
||
'Unexpected background color returned from ' | ||
'`style.background_gradient()`') | ||
assert result[0, 0][1].split(' ')[1] == test_colors[c_map][0, 0][1] | ||
# Dark text on light background | ||
assert result[1, 0][0].split(' ')[1] == test_colors[c_map][1, 0][0], ( | ||
'Unexpected background color returned from ' | ||
'`style.background_gradient()`') | ||
assert result[1, 0][1].split(' ')[1] == test_colors[c_map][1, 0][1] | ||
|
||
@td.skip_if_no_mpl | ||
@pytest.mark.parametrize("text_color_threshold", [1.1, '1', -1, [2, 2]]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these tests need to be in the MatplotlibDep test class? I didn't think this required mpl but could be wrong.
If that is the case then move the
@td.skip_if_no_mpl
decorator to the class instead of on each function individually.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they both call
background_gradient()
which depends on mpl.