Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import string
  4.  
  5. def h2i(rgbOctet):
  6.     """
  7.        Return the Base 10 value for a hex (base 0) octet
  8.    """
  9.     return (int('0x%s' % ( rgbOctet ), 0))
  10.  
  11. def validate_hex_string( hexString ):
  12.     """
  13.        Loop through a hex string and validate that each character
  14.            in the string is a valid hex character.
  15.    """
  16.     # Set default return status to True
  17.     status = True
  18.     # If the hexString is 4 or 7 characters long, strip off
  19.     # the leading character, as it is going to be a '#'
  20.     if hexString[0] == '#':
  21.         if len( hexString ) in ( 4, 7 ):
  22.             hexString = hexString[1:]
  23.     # If the hexString is not 6 characters long
  24.     if len( hexString ) not in ( 3, 6 ):
  25.         # Print error to stderr
  26.         print >> sys.stderr, 'Error: \n\tArguement should be 6 hexidecimal characters, and may contain a leading # symbol.\n\tAnything else is considered invalid.\n\tExiting.'
  27.         # Exit with non-zero status code
  28.         sys.exit(1)
  29.     # If the length is 3, add padding
  30.     if len( hexString ) == 3:
  31.         hexString = '%s%s%s%s%s%s' % ( hexString[0], hexString[0], hexString[1], hexString[1], hexString[2], hexString[2] )
  32.     # Loop through the string and validate character-by-character
  33.     # Set return value to false if invalid character(s) detected
  34.     for i in hexString:
  35.         # If character is not in string.hexdigits ( a list of valid hex characters )
  36.         if i not in string.hexdigits:
  37.             # Print which character and its placement to stderr
  38.             print >> sys.stderr, 'Error: \n\tCharacter %d (%c) not a valid hex character.' % ( sys.argv[1].index(i), i )
  39.             # Set return status to false
  40.             status = False
  41.     # If status is still true, return hexString
  42.     if status:
  43.         return hexString
  44.     # If status is false, return False
  45.     else:
  46.         return status
  47.  
  48. # If the validation returns non-False, run the conversion
  49. if validate_hex_string( sys.argv[1] ):
  50.     # Set a to the return status of validator
  51.     a = validate_hex_string( sys.argv[1] )
  52.     # Break up each color's octet
  53.     red     = a[:2]
  54.     green   = a[2:4]
  55.     blue    = a[4:6]
  56.     # Print the original value
  57.     print "Hex value: %s" % ( a )
  58.     # Print the converted value
  59.     print "RGB Value: rgb( %d, %d, %d )" % ( h2i(red), h2i(green), h2i(blue) )
  60. # If errors are detected during validation, Exit
  61. else:
  62.     sys.exit('\nExiting due to invalid H\exidecimal characters being detected.')
Add Comment
Please, Sign In to add comment