import matplotlib.pyplot as plt
from colour import Color

def rainbow_gradient(num_colors):
    colors = []
    gradient = list(Color("violet").range_to(Color("red"), num_colors))
    for color in gradient:
        colors.append(color.hex_l)
    return colors

framenb = 1

keyvalues = []

with open('distance.txt', 'r') as f:
    lignes = f.readlines()
    for ligne in lignes:
        line = eval(ligne)

        allkeys = list(line.keys())
        allkeys.sort()
        linedict = {i: line[i] for i in allkeys}
        
        linedict = {cle: valeur for cle, valeur in linedict.items() if valeur >= 0}

        for key, value in linedict.items():
            if key not in keyvalues:
                keyvalues.append(key)

keypositions = {}

for key, i in zip(keyvalues, range(1, len(keyvalues)+1)):
    keypositions[key] = i
print('keypositions: ', keypositions)
colors = rainbow_gradient(len(keypositions)+1)

with open('distance.txt', 'r') as f:
    lignes = f.readlines()
    for ligne in lignes:
        line = eval(ligne)

        allkeys = list(line.keys())
        allkeys.sort()
        linedict = {i: line[i] for i in allkeys}
        
        linedict = {cle: valeur for cle, valeur in linedict.items() if valeur >= 0}     

        plt.figure(1,figsize=[16,9])
        plt.xlim([-1,780])
        plt.ylim([-20, 150])

        plt.xlabel("Numéro de l'image", fontsize = 16)
        plt.ylabel("Distance au point d'origine", fontsize = 16)
        plt.xticks(fontsize = 14)
        plt.yticks(fontsize = 14)

        plt.grid()

        nb = 0

        for key, value in linedict.items():
            if key == 13:
                # print('value: ', value)
                # print(framenb)
                with open("pt4only.txt", 'a', encoding='utf-8') as file:
                    file.write('\n' + str(framenb) + ';' + str(value))

                plt.plot([framenb],[value], marker='o', linestyle='-', color=colors[keypositions[key]])       # , color=colors[key]

            nb += 1

        # plt.draw()
        # plt.pause(0.0001)
        if framenb == 780:
            plt.savefig('position-temps-pt4.png')
        framenb += 1 
        # plt.clf()