From b5977485d95b2b338c8ff12f719678273d69369f Mon Sep 17 00:00:00 2001 From: Carl Andersson Date: Fri, 7 May 2021 16:50:45 +0200 Subject: [PATCH] Implements getting a colorscale by name A utility function to get a colorscale by name. --- .../plotly/_plotly_utils/colors/__init__.py | 27 +++++++++++++++++++ .../python/plotly/plotly/colors/__init__.py | 1 + .../test_core/test_colors/test_colors.py | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/packages/python/plotly/_plotly_utils/colors/__init__.py b/packages/python/plotly/_plotly_utils/colors/__init__.py index a44171ef9a..515e7c99f6 100644 --- a/packages/python/plotly/_plotly_utils/colors/__init__.py +++ b/packages/python/plotly/_plotly_utils/colors/__init__.py @@ -806,3 +806,30 @@ def named_colorscales(): from _plotly_utils.basevalidators import ColorscaleValidator return [c for c in ColorscaleValidator("", "").named_colorscales] + + +def get_colorscale(name): + """ + Returns the colorscale for a given name. See `named_colorscales` for the + built-in colorscales. + """ + from _plotly_utils.basevalidators import ColorscaleValidator + + if not isinstance(name, str): + raise exceptions.PlotlyError("Name argument have to be a string.") + + name = name.lower() + if name[-2:] == "_r": + should_reverse = True + name = name[:-2] + else: + should_reverse = False + + if name in ColorscaleValidator("", "").named_colorscales: + colorscale = ColorscaleValidator("", "").named_colorscales[name] + else: + raise exceptions.PlotlyError(f"Colorscale {name} is not a built-in scale.") + + if should_reverse: + return colorscale[::-1] + return colorscale diff --git a/packages/python/plotly/plotly/colors/__init__.py b/packages/python/plotly/plotly/colors/__init__.py index 2e3dc753e6..b08d52c87e 100644 --- a/packages/python/plotly/plotly/colors/__init__.py +++ b/packages/python/plotly/plotly/colors/__init__.py @@ -45,4 +45,5 @@ "plotlyjs", "DEFAULT_PLOTLY_COLORS", "PLOTLY_SCALES", + "get_colorscale", ] diff --git a/packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py b/packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py index e382e9bfd7..0d393b33b9 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py +++ b/packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py @@ -137,3 +137,30 @@ def test_make_colorscale(self): self.assertRaisesRegexp( PlotlyError, pattern2, colors.make_colorscale, color_list2, scale ) + + def test_get_colorscale(self): + + # test for incorrect input type + pattern = "Name argument have to be a string." + name = colors.sequential.haline + + self.assertRaisesRegexp(PlotlyError, pattern, colors.get_colorscale, name) + + # test for non-existing colorscale + pattern = r"Colorscale \S+ is not a built-in scale." + name = "foo" + + self.assertRaisesRegex(PlotlyError, pattern, colors.get_colorscale, name) + + # test non-capitalised access + self.assertEqual(colors.sequential.haline, colors.get_colorscale("haline")) + # test capitalised access + self.assertEqual(colors.diverging.Earth, colors.get_colorscale("Earth")) + # test accessing non-capitalised scale with capitalised name + self.assertEqual(colors.cyclical.mrybm, colors.get_colorscale("Mrybm")) + # test accessing capitalised scale with non-capitalised name + self.assertEqual(colors.sequential.Viridis, colors.get_colorscale("viridis")) + # test accessing reversed scale + self.assertEqual( + colors.diverging.Portland_r, colors.get_colorscale("portland_r") + )