Guest User

Untitled

a guest
Apr 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import argparse
  2. from colorsys import hls_to_rgb, rgb_to_hls
  3. import sys
  4.  
  5. # setting up arguments
  6. parser = argparse.ArgumentParser(
  7. description='Function that outputs a pleasant text color hex '
  8. 'given a background color hex')
  9. parser.add_argument('color', help='Color in #xxxxxx or xxxxxx format')
  10.  
  11. args = parser.parse_args()
  12. color = args.color
  13.  
  14. # removing #
  15. if color.startswith('#'):
  16. color = color[1:]
  17.  
  18. # splitting, converting to decimal and then to 0-1 range
  19. try:
  20. r, g, b = [int(color[i:i+2], 16) / 255.0 for i in range(0, len(color), 2)]
  21. except ValueError:
  22. print 'Wrong color supplied'
  23. sys.exit()
  24.  
  25. # shifting
  26. h, l, s = [(i + 0.5) % 1 for i in rgb_to_hls(r, g, b)]
  27.  
  28. # converting back to rgb
  29. result = [format(int(i*255), 'x') for i in hls_to_rgb(h, l, s)]
  30. print '#{}'.format(''.join(result))
Add Comment
Please, Sign In to add comment