diff --git a/README.md b/README.md index ad731e9..65e5ae9 100644 --- a/README.md +++ b/README.md @@ -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'] ) @@ -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'] ) @@ -58,7 +58,7 @@ 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'] ) @@ -66,6 +66,16 @@ Convert an HSL or numeric RGB color scale to string RGB color scale ['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 diff --git a/colorlover/__init__.py b/colorlover/__init__.py index 4753d8b..f1794c4 100755 --- a/colorlover/__init__.py +++ b/colorlover/__init__.py @@ -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': + 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: diff --git a/tests.py b/tests.py index 4a49d55..4ef6fe2 100644 --- a/tests.py +++ b/tests.py @@ -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( @@ -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(