Search
 
SCRIPT & CODE EXAMPLE
 

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('триллер'))
Comment

PREVIOUS NEXT
Code Example
Python :: python web scraping 
Python :: sorted multiple keys python 
Python :: python tkinter focus on entry 
Python :: determinant of 3x3 numpy 
Python :: flask app run 
Python :: How to find the maximum subarray sum in python? 
Python :: copy python 
Python :: pandas weighted average groupby 
Python :: how to find the last occurrence of a character in a string in python 
Python :: _ in python 
Python :: full form of api 
Python :: python docstring 
Python :: django-filter for multiple values parameter 
Python :: comentar codigo en python 
Python :: python ignore first value in generator 
Python :: python floor function 
Python :: run ansible playbook python 
Python :: python remove  
Python :: create payment request in stripe 
Python :: How to solve not in base 10 in python when using decimals 
Python :: string comparison in python 
Python :: django create object from dict 
Python :: standard error of mean 
Python :: how to write something in python 
Python :: generator expression 
Python :: how to activate venv python 
Python :: type() in python 
Python :: python while loop 
Python :: create a virtual environment python 3 
Python :: python string equals 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =