diff --git a/README.md b/README.md index ad731e9..bcfe8fe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/colorlover/__init__.py b/colorlover/__init__.py index 4753d8b..5e533cc 100755 --- a/colorlover/__init__.py +++ b/colorlover/__init__.py @@ -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" ''' diff --git a/tests.py b/tests.py index 4a49d55..2b51d43 100644 --- a/tests.py +++ b/tests.py @@ -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(