Skip to content

BUG: Bg gradient map text color fix #39889

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

Merged
merged 5 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ Other
- Bug in :class:`Styler` where ``subset`` arg in methods raised an error for some valid multiindex slices (:issue:`33562`)
- :class:`Styler` rendered HTML output minor alterations to support w3 good code standard (:issue:`39626`)
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)


Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ def relative_luminance(rgba) -> float:
The relative luminance as a value from 0 to 1
"""
r, g, b = (
x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055 ** 2.4)
x / 12.92 if x <= 0.04045 else ((x + 0.055) / 1.055) ** 2.4
for x in rgba[:3]
)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
Expand Down
28 changes: 18 additions & 10 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,28 +1994,36 @@ def test_background_gradient(self):
assert result[(1, 0)] == ["background-color: #fff7fb", "color: #000000"]

@pytest.mark.parametrize(
"c_map,expected",
"cmap, expected",
[
(
None,
"PuBu",
{
(0, 0): ["background-color: #440154", "color: #f1f1f1"],
(1, 0): ["background-color: #fde725", "color: #000000"],
(4, 5): ["background-color: #86b0d3", "color: #000000"],
(4, 6): ["background-color: #83afd3", "color: #f1f1f1"],
},
),
(
"YlOrRd",
{
(0, 0): ["background-color: #ffffcc", "color: #000000"],
(1, 0): ["background-color: #800026", "color: #f1f1f1"],
(4, 8): ["background-color: #fd913e", "color: #000000"],
(4, 9): ["background-color: #fd8f3d", "color: #f1f1f1"],
},
),
(
None,
{
(7, 0): ["background-color: #48c16e", "color: #f1f1f1"],
(7, 1): ["background-color: #4cc26c", "color: #000000"],
},
),
],
)
def test_text_color_threshold(self, c_map, expected):
df = DataFrame([1, 2], columns=["A"])
result = df.style.background_gradient(cmap=c_map)._compute().ctx
assert result == expected
def test_text_color_threshold(self, cmap, expected):
df = DataFrame(np.arange(100).reshape(10, 10))
result = df.style.background_gradient(cmap=cmap, axis=None)._compute().ctx
for k in expected.keys():
assert result[k] == expected[k]

@pytest.mark.parametrize("text_color_threshold", [1.1, "1", -1, [2, 2]])
def test_text_color_threshold_raises(self, text_color_threshold):
Expand Down