-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Adds function to sample colorscales #3136
Merged
nicolaskruchten
merged 7 commits into
plotly:master
from
CarlAndersson:colorscale-sampling
May 10, 2021
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b6a594
Adds function to sample colorscales
CarlAndersson 1b419ad
Minor fixes
CarlAndersson e774d3f
Adds tests
CarlAndersson 282ed0d
Merge remote-tracking branch 'origin/master' into colorscale-sampling
CarlAndersson fb2c139
Code formatting
CarlAndersson 10be9df
Merge branch 'master' into colorscale-sampling
CarlAndersson 1b8ec25
Handle string inputs with get_colorscale
CarlAndersson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -806,3 +806,54 @@ def named_colorscales(): | |
from _plotly_utils.basevalidators import ColorscaleValidator | ||
|
||
return [c for c in ColorscaleValidator("", "").named_colorscales] | ||
|
||
|
||
def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rgb"): | ||
""" | ||
Samples a colorscale at specific points. | ||
|
||
Interpolates between colors in a colorscale to find the specific colors | ||
corresponding to the specified sample values. The colorscale can be specified | ||
as a list of `[scale, color]` pairs, as a list of colors, or as a named | ||
plotly colorscale. The samplepoints can be specefies an iterable of specific | ||
points in the range [0.0, 1.0], or as an integer number of points which will | ||
be spaced equally between the low value (default 0.0) and the high value | ||
(default 1.0). The output is a list of colors, formatted according to the | ||
specified colortype. | ||
""" | ||
from bisect import bisect_left | ||
|
||
try: | ||
validate_colorscale(colorscale) | ||
except exceptions.PlotlyError: | ||
if isinstance(colorscale, str): | ||
if colorscale in PLOTLY_SCALES: | ||
colorscale = PLOTLY_SCALES[colorscale] | ||
else: | ||
raise exceptions.PlotlyError( | ||
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'm not sure I understand this error message... Under what circumstances would we accept "an rgb color or a hex color" here? |
||
"If your colors variable is a string, it must be a " | ||
"Plotly scale, an rgb color or a hex color." | ||
) | ||
else: | ||
colorscale = make_colorscale(colorscale) | ||
|
||
scale = colorscale_to_scale(colorscale) | ||
validate_scale_values(scale) | ||
colors = colorscale_to_colors(colorscale) | ||
colors = validate_colors(colors, colortype="tuple") | ||
|
||
if isinstance(samplepoints, int): | ||
samplepoints = [ | ||
low + idx / (samplepoints - 1) * (high - low) for idx in range(samplepoints) | ||
] | ||
elif isinstance(samplepoints, float): | ||
samplepoints = [samplepoints] | ||
|
||
sampled_colors = [] | ||
for point in samplepoints: | ||
high = bisect_left(scale, point) | ||
low = high - 1 | ||
interpolant = (point - scale[low]) / (scale[high] - scale[low]) | ||
sampled_color = find_intermediate_color(colors[low], colors[high], interpolant) | ||
sampled_colors.append(sampled_color) | ||
return validate_colors(sampled_colors, colortype=colortype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
typo here: "specefies an"