Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ All of the color scales in colorlover

### cl.to_numeric( scale )

Converts scale of RGB or HSL strings to list of tuples with RGB integer values
Converts scale of HEX, RGB or HSL strings to list of tuples with RGB integer values

```
>>> cl.to_numeric( cl.scales['3']['div']['RdYlBu'] )
Expand All @@ -48,7 +48,7 @@ Converts scale of RGB or HSL strings to list of tuples with RGB integer values

### cl.to_hsl( scale )

Converts a string RGB or numeric RGB colorscale to HSL
Converts a string RGB, HEX or numeric RGB colorscale to HSL

```
>>> cl.to_hsl( cl.scales['3']['div']['RdYlBu'] )
Expand All @@ -58,14 +58,24 @@ Converts a string RGB or numeric RGB colorscale to HSL

### cl.to_rgb( scale )

Convert an HSL or numeric RGB color scale to string RGB color scale
Convert an HSL, HEX or numeric RGB color scale to string RGB color scale

```
>>> cl.to_rgb( cl.scales['3']['div']['RdYlBu'] )

['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)']
```

### cl.to_hex( scale )

Converts an HSL, string RGB, numeric RGB colorscale to HEX

```
>>> cl.to_rgb( cl.scales['3']['div']['RdYlBu'] )

[''#fc8d59', '#ffffbf', '#91bfdb']
```

### cl.to_html( scale )

Traverse color scale dictionary and return available color scales as HTML string
Expand Down
38 changes: 37 additions & 1 deletion colorlover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand All @@ -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':

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?

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
Expand Down Expand Up @@ -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 )
Expand All @@ -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':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that could be elif s_t in ['numeric', 'rgb', 'hsl']

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)" ] -->
Expand All @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@


class UsageTests(unittest.TestCase):
def test_scale_type(self):
test_colors = {'rgb': ['rgb(252, 141, 89)', 'rgb(255, 255, 191)', 'rgb(145, 191, 219)'],
'hsl': ['hsl(19,96,67)', 'hsl(60,100,87)', 'hsl(203,51,71)'],
'hex': ['#fc8d59', '#ffffbf', '#91bfdb'],
'numeric': [(252, 141, 89), (255, 255, 191), (145, 191, 219)],
}

for type_scale, scale in test_colors.items():
self.assertEqual(
cl.scale_type(test_colors.get(type_scale)),
type_scale
)

def test_scales(self):
scales = cl.scales['3']['div']['RdYlBu']
self.assertEqual(
Expand All @@ -39,6 +52,13 @@ def test_to_numeric(self):
(255, 255, 191), (145, 191, 219)]
)

def test_to_hex(self):
scales = cl.to_hex(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
scales,
['#fc8d59', '#ffffbf', '#91bfdb']
)

def test_to_hsl(self):
scales = cl.to_hsl(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
Expand Down