Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- from colorsys import hls_to_rgb, rgb_to_hls
- import sys
- # setting up arguments
- parser = argparse.ArgumentParser(
- description='Function that outputs a pleasant text color hex '
- 'given a background color hex')
- parser.add_argument('color', help='Color in #xxxxxx or xxxxxx format')
- args = parser.parse_args()
- color = args.color
- # removing #
- if color.startswith('#'):
- color = color[1:]
- # splitting, converting to decimal and then to 0-1 range
- try:
- r, g, b = [int(color[i:i+2], 16) / 255.0 for i in range(0, len(color), 2)]
- except ValueError:
- print 'Wrong color supplied'
- sys.exit()
- # shifting
- h, l, s = [(i + 0.5) % 1 for i in rgb_to_hls(r, g, b)]
- # converting back to rgb
- result = [format(int(i*255), 'x') for i in hls_to_rgb(h, l, s)]
- print '#{}'.format(''.join(result))
Add Comment
Please, Sign In to add comment