import re


def get_color( string ):
    """
    Parses color strings into numbers.

    @param string A string representing a 24-bit RGB color:
                    Hex literal: 0x123456
                    CSS-style:   #123456
                    CSS-abbr:    #A5B or 0xA5B
                    Octet:       #a5  or 0xa5
                    Octet-abbr:  #a   or 0xa
                    RGB decimal: rgb(127,0,255)
    @return       The integer value
    """

    # look for hex strings as either #123456 or 0x123456
    match = re.match( r'^(?:#|0(?:x|X))([0-9a-fA-F]+)', string )
    if match is not None:

        # strip off the '#' or '0x' prefix
        string = match.group( 1 )

    # this doesn't have a hex prefix, check for 'rgb( r, g, b )' string
    else:
        octet = r'\s*(\d{1,3})\s*'
        pattern = r'rgb\(' + ','.join( [ octet ] * 3 ) + r'\)'
        match = re.match( pattern, string )
        if match is not None:
            c = [ int( o ) for o in match.groups()[ 1 : ] ]
            return ( c[ 0 ] << 16 ) | ( c[ 1 ] << 8 ) | c[ 2 ]

    # assume string is hex digits
    length = len( string )

    # expand a single hex digit to six of the same
    #   'a' => 'aaaaaa'
    if length == 1:
        string = string * 6

    # expand two hex digits to three octets of the same
    #   'a5' => 'a5a5a5'
    elif length == 2:
        string = string * 3

    # expand three hex digits to three octets, each repeating one digit
    #   'a5b' => 'aa55bb'
    elif length == 3:
        string = ''.join( c * 2 for c in string )

    # convert hex string to native integer
    return int( string, 16 )