🌐 AI搜索 & 代理 主页
Skip to content
Closed
Changes from all 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
12 changes: 12 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@ def is_gray(self):
return (np.alltrue(self._lut[:, 0] == self._lut[:, 1]) and
np.alltrue(self._lut[:, 0] == self._lut[:, 2]))

def to_grayscale(self):
"""Return a grayscale version of the colormap"""
colors = self(np.arange(self.N))

# convert RGBA to perceived greyscale luminance
# cf. http://alienryderflex.com/hsp.html
RGB_weight = [0.299, 0.587, 0.114]
luminance = np.sqrt(np.dot(colors[:, :3] ** 2, RGB_weight))
colors[:, :3] = luminance[:, np.newaxis]

return self.from_list(self.name + "_gray", colors, self.N)


class LinearSegmentedColormap(Colormap):
"""Colormap objects based on lookup tables using linear segments.
Expand Down