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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ All of the color scales in colorlover
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)']
```

### cl.get_scale( scale_type='qual', scale_seq='Accent', length=5 )

Get scales using method instead of dictionary.

```
>>> cl.get_scale('qual', 'div', 3)

['rgb(127,201,127)', 'rgb(190,174,212)', 'rgb(253,192,134)']
```

### cl.to_numeric( scale )

Converts scale of RGB or HSL strings to list of tuples with RGB integer values
Expand Down
21 changes: 21 additions & 0 deletions colorlover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,27 @@
'rgb(189,0,38)',
'rgb(128,0,38)']}}}

def get_scale(scale_type='qual', scale_seq='Accent', length=5):
"""return the corresponding scale without using dictionnary directly. Raise error if wrong argument
is givent and print available argument.
"""
try:
assert str(length) in scales.keys()
try:
assert scale_type in scales[str(length)].keys()
try:
assert scale_seq in scales[str(length)][scale_type].keys()
return scales[str(length)][scale_type][scale_seq]

except AssertionError:
print('get_scale error : scale_type must be one of the following strings {}'.format(
list(scales[str(length)][scale_type].keys())))
except AssertionError:
print('get_scale error : scale_seq must be one of the following strings {}'.format(
list(scales[str(length)].keys())))
except AssertionError:
print('get_scale error : length must be an integer comprised between 3 and 12')

def scale_type( scale ):
''' returns "rbg", "hsl", "numeric", or raises exception. ie,
[ "rgb(255, 255, 255)", "rgb(255, 255, 255)", "rgb(255, 255, 255)" ] --> "rgb" '''
Expand Down
6 changes: 6 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_scales(self):
scales,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)'])

def test_get_scales(self):
scales = cl.get_scale(scale_type='div', scale_seq='RdYlBu', length=3)
self.assertEqual(
scales,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)'])

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