-
Notifications
You must be signed in to change notification settings - Fork 18
to_hex method #29
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
Open
yohann84L
wants to merge
3
commits into
plotly:master
Choose a base branch
from
yohann84L:to_hex_method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
to_hex method #29
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -1639,6 +1639,8 @@ def scale_type( scale ): | |
return s_t | ||
elif isinstance(swatch,tuple) and len(swatch) == 3: | ||
return 'numeric' | ||
elif swatch[0] == '#' and len(swatch) == 7: | ||
return 'hex' | ||
raise Exception('Could not determine type of input colorscale.\n\ | ||
Colorscales must be in one of these 3 forms:\n\ | ||
[ (255, 255, 255), (255, 255, 255), (255, 255, 255) ]\n\ | ||
|
@@ -1651,10 +1653,20 @@ def to_numeric( scale ): | |
[ (255, 255, 255), (255, 255, 255), (255, 255, 255) ] ''' | ||
numeric_scale = [] | ||
s_t = scale_type( scale ) | ||
if s_t in ['rgb','hsl']: | ||
if s_t == 'rgb': | ||
for s in scale: | ||
s = s[s.find("(")+1:s.find(")")].replace(' ','').split(',') | ||
numeric_scale.append( ( float(s[0]), float(s[1]), float(s[2]) ) ) | ||
elif s_t == 'hsl': | ||
scale = to_rgb( scale ) | ||
for s in scale: | ||
s = s[s.find("(")+1:s.find(")")].replace(' ','').split(',') | ||
numeric_scale.append( s ) | ||
elif s_t == 'hex': | ||
for s in scale: | ||
s = s.lstrip('#') | ||
lv = len(s) | ||
numeric_scale.append(tuple(int(s[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))) | ||
elif s_t == 'numeric': | ||
numeric_scale = scale | ||
return numeric_scale | ||
|
@@ -1690,6 +1702,9 @@ def to_hsl( scale ): | |
elif s_t == 'rgb': | ||
scale = to_numeric( scale ) | ||
|
||
elif s_t == 'hex': | ||
scale = to_numeric( scale ) | ||
|
||
for ea in scale: | ||
r,g,b = [ x/255.0 for x in ea ] | ||
h,l,s = colorsys.rgb_to_hls( r,g,b ) | ||
|
@@ -1699,6 +1714,22 @@ def to_hsl( scale ): | |
|
||
return hsl | ||
|
||
def to_hex( scale ): | ||
''' convert an hsl, numeric or rgb color to string hex color. ie, | ||
[ "hsl(360,100,100)", "hsl(360,100,100)", "hsl(360,100,100)" ] --> | ||
[ "#FFFFFF", "#FFFFFF", "#FFFFFF" ] | ||
''' | ||
s_t = scale_type(scale) | ||
|
||
if s_t == 'hex': | ||
return scale | ||
elif s_t == 'numeric': | ||
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. that could be |
||
return ['#%02x%02x%02x' % tuple(map(int, s)) for s in to_numeric( scale )] | ||
elif s_t == 'rgb': | ||
return ['#%02x%02x%02x' % tuple(map(int, s)) for s in to_numeric( scale )] | ||
elif s_t == 'hsl': | ||
return ['#%02x%02x%02x' % tuple(map(int, s)) for s in to_numeric( scale )] | ||
|
||
def to_rgb( scale ): | ||
''' convert an hsl or numeric rgb color scale to string rgb color scale. ie, | ||
[ "hsl(360,100,100)", "hsl(360,100,100)", "hsl(360,100,100)" ] --> | ||
|
@@ -1714,6 +1745,11 @@ def to_rgb( scale ): | |
for ea in scale: | ||
rgb.append( 'rgb'+str(ea) ) | ||
return rgb | ||
elif s_t == 'hex': | ||
scale = to_numeric( scale ) | ||
for ea in scale: | ||
rgb.append( 'rgb'+str(ea) ) | ||
return rgb | ||
elif s_t == 'hsl': | ||
numeric_hsl_scale = [] | ||
for s in scale: | ||
|
This file contains hidden or 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.
Why does the rgb and hsl behaviour need to diverge now?