Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Create a hexadecimal colour based on a string with python

def FloatToHex(number, base = 16):
    if number < 0:                          # Check if the number is negative to manage the sign
        sign = "-"                          # Set the negative sign, it will be used later to generate the first element of the result list
        number = -number                    # Change the number sign to positive
    else:
        sign = ""                           # Set the positive sign, it will be used later to generate the first element of the result list

    s = [sign + str(int(number)) + '.']     # Generate the list, the first element will be the integer part of the input number
    number -= int(number)                   # Remove the integer part from the number

    for i in range(base):                   # Iterate N time where N is the required base
        y = int(number * 16)                # Multiply the number by 16 and take the integer part
        s.append(hex(y)[2:])                # Append to the list the hex value of y, the result is in format 0x00 so we take the value from postion 2 to the end
        number = number * 16 - y            # Calculate the next number required for the conversion

    return ''.join(s).rstrip('0') 

def charCodeAt(testS):
    l = list(bytes(testS, 'utf-16'))[2:]
    for i, c in enumerate([(b<<8)|a for a,b in list(zip(l,l[1:]))[::2]]):
        return c



def stringToColour(text):
    hash = 0
    for x in text:
        hash = charCodeAt(x) + ((hash << 5) - hash)

    colour = '#';
    for i in range(3-1):
        value = (hash >> (i * 8)) & 0xFF;
        colour += ('00' + FloatToHex(value,16)[-2])
    return colour



print(stringToColour('триллер'))
Source by pastebin.com #
 
PREVIOUS NEXT
Tagged: #Create #hexadecimal #colour #based #string #python
ADD COMMENT
Topic
Name
5+9 =