Not reuse function!

tileCountX=2

tileCountY=10

colorsLeft=[]

colorsRight=[]

interpolateShortest=True

def setup():

    size(800,800)

    colorMode(HSB)

    noStroke()

    shakeColors()

    

def draw():

    tileCountX=int(map(mouseX,0,width,2,100))

    tileCountY=int(map(mouseY,0,height,2,10))

    

    tileWidth=width/tileCountX

    tileHeight=height/tileCountY

    for gridY in range(tileCountY):

        col1=colorsLeft[gridY]

        col2=colorsRight[gridY]

        for gridX in range(tileCountX):

            amount=map(gridX,0,tileCountX-1,0,1)

            if interpolateShortest:

                colorMode(RGB)

                interCol=lerpColor(col1,col2,amount)

                colorMode(HSB)

            else:

                interCol=lerpColor(col1,col2,amount)

            fill(interCol)

            posX=tileWidth*gridX

            posY=tileHeight*gridY

            rect(posX,posY,tileWidth,tileHeight)

def shakeColors():

    for i in range(tileCountY):

        colorsLeft.append(color(random(0, 60), random(0, 100), 100)) 

        colorsRight.append(color(random(160, 190), 100, random(0, 100)))

        

def keyPressed():

    global interpolateShortest

    if key=="1": 

        interpolateShortest=True

    elif key=="2": 

        interpolateShortest=False

question :when i pressed mouse i couldn’t make shakeColors() function work. How can i make it work ?

1 Like

Welcome, @Mayarch

You’re listening for key, not mouse input. In other words, when the user presses the 1 or 2 key on the keyboard.

I assume that you want to detect left and right mouse clicks:

...

def mousePressed():
    global interpolateShortest
    
    if mouseButton == LEFT:
        interpolateShortest=True

    elif mouseButton == RIGHT: 
        interpolateShortest=False
1 Like

def mousePressed():

if mouseButton==LEFT:

    shakeColors()

sorry i forgot that definition what i want.but i couldn t rework shakeColor function again when i pressed mouse left click

tileCountX=2

tileCountY=10

colorsLeft=[]

colorsRight=[]

interpolateShortest=True

def setup():

    size(800,800)

    colorMode(HSB)

    noStroke()

    makeColors()

    shakeColors()

    

def draw():

    tileCountX=int(map(mouseX,0,width,2,100))

    tileCountY=int(map(mouseY,0,height,2,10))

    

    tileWidth=width/tileCountX

    tileHeight=height/tileCountY

    for gridY in range(tileCountY):

        col1=colorsLeft[gridY]

        col2=colorsRight[gridY]

        for gridX in range(tileCountX):

            amount=map(gridX,0,tileCountX-1,0,1)

            if interpolateShortest:

                colorMode(RGB)

                interCol=lerpColor(col1,col2,amount)

                colorMode(HSB)

            else:

                interCol=lerpColor(col1,col2,amount)

            fill(interCol)

            posX=tileWidth*gridX

            posY=tileHeight*gridY

            rect(posX,posY,tileWidth,tileHeight)

def makeColors():

    for i in range(tileCountY):

        colorsLeft.append(color(random(0, 60), random(0, 100), 100)) 

        colorsRight.append(color(random(160, 190), 100, random(0, 100)))

def shakeColors():

    for i in range(tileCountY):

        colorsLeft[i]=color(random(0, 60), random(0, 100), 100) 

        colorsRight[i]=color(random(160, 190), 100, random(0, 100))

        

def keyPressed():

    global interpolateShortest

    if key=="1": 

        interpolateShortest=True

    elif key=="2": 

        interpolateShortest=False

def mousePressed():

    if mouseButton==LEFT:

        shakeColors()

i changed the name shakeColors as makeColors and then i redefined colorsLeft, colorsRight lists indexes again with shakeColors function. then solved

2 Likes