import time
import board
import neopixel
import random
from time import sleep
import numpy as np
pixels= neopixel.NeoPixel(board.D18, 30, auto_write=False) #define the pin
sleep_time=0.05 #time interval between the transition shades, change this to change how quick you want them to fade. 
rnd_old=1
ns=50   #number of transition shades between colours
r1=255  #define your 2 colours to fade between
g1=5
b1=102
r2=0
g2=153
b2=153
pixels.fill((r1,g1,b1))
pixels.show()
while True:
    sleep(1)        
    if r1<r2:
        rg=(r2-r1)/ns
    if r1>r2:
        rg=(r1-r2)/ns
        rg=-rg             #All this crap makes a table of all the transition colours so the lights can easily fade between one colour and another.
    if r1==r2:
        rg=0

    if g1<g2:
        gg=(g2-g1)/ns
    if g1>g2:
        gg=(g1-g2)/ns
        gg=-gg
    if g1==g2:
        gg=0
  
    if b1<b2:
        bg=(b2-b1)/ns
    if b1>b2:
        bg=(b1-b2)/ns
        bg=-bg
    if b1==b2:
        bg=0


    rows, cols = (3, ns)
    colour = [[0 for i in range(cols)] for j in range(rows)]

    r=0
    g=1
    b=2
    colour[r][0]=r1
    colour[g][0]=g1
    colour[b][0]=b1
    colour[r][ns-1]=r2
    colour[g][ns-1]=g2
    colour[b][ns-1]=b2

    for i in range(1,ns-1):
        if rg==0:
            colour[r][i]=r1
        if gg==0:
            colour[g][i]=g1
        if bg==0:
            colour[b][i]=b1

        colour[r][i] = colour[r][i-1] + rg
        colour[g][i] = colour[g][i-1]+ gg
        colour[b][i] = colour[b][i-1]+ bg

    colour=np.array(colour)
    rnd=random.randint(0,9)     #add in the number of tiles you have
    while rnd==rnd_old:
        rnd=random.randint(0,9)
    rnd_old=rnd                 #chooses a random tile and makes sure it fades a new tile each time

    rc=pixels[9+1+rnd*3][r]         #change this value to work on normal 3 light tiles. This calculation does something to take into account my hexagon tile 
    gc=pixels[9+1+rnd*3][g]         #with 12 light, I can't remember, I wrote this over a year ago. remove the 9+1 and I presume it'll work fine. 
    bc=pixels[9+1+rnd*3][b]
    position=np.where(colour == rc)
    listOfCoordinates= list(zip(position[0], position[1]))
    position=(listOfCoordinates[r][1])

    for x in range (1,ns):
        if position==0:
            #fade to orange

            rc=int(colour[r][x-1])
            gc=int(colour[g][x-1])
            bc=int(colour[b][x-1])
            for i in range(3):
                sum2=9+i+rnd*3      #change this bit aswell. 
                pixels[sum2]=((round(rc),round(gc),round(bc)))

        else:
            #fade to blue

            rc=int(colour[r][ns-x])
            gc=int(colour[g][ns-x])
            bc=int(colour[b][ns-x])
            for i in range(3):
                sum2=9+i+rnd*3
                pixels[sum2]=((round(rc),round(gc),round(bc)))
        pixels.show()
        sleep(sleep_time)
    for i in range(3):
        if position==0:
            sum2=9+i+rnd*3       
            pixels[sum2]=((round(r2),round(g2),round(b2)))
        else:
            sum2=9+i+rnd*3
            pixels[sum2]=((round(r1),round(g1),round(b1)))

