
-----------------------------------
munt4life
Wed Jan 18, 2012 3:52 pm

how do I make a gradient tool in python?
-----------------------------------
when i draw a line in a closed shape, the gradient goes from the first point to the 2nd point
im not sure whats wrong with my program



[code] from pygame import *
from collections import deque 
from sets import Set 

screen=display.set_mode((800,600))
def gradient(cmx,cmy,mx,my,firstclr,oldclr,r,g,b):
    x=(cmx+mx)/2
    y=(cmy+my)/2
    visit_queue = deque ( [(cmx,cmy)] );
        
    visited = Set() 
    
    # Represent your neighbourhood this way; it makes it easier to deal with. 
    # Later on, if you want to move to 8 points (or more), change this list. 
    neighbourhood = [ (-1,0), (+1,0), (0,-1), (0,+1) ] 
    
    # While there are still points left to visit 
    while visit_queue: 
            # Get the next point to visit 
        pt = visit_queue.popleft()
        dist=pt[0]-x
            
            # If that point is not the border color, and we haven't visited it 
        if screen.get_at(pt) != firstclr or screen.get_at(pt) != oldclr and pt not in visited: 
                    # Change the colour 
            screen.set_at(pt, (firstclr[0]/2-(dist*r/(mx-x)),firstclr[1]/2-(dist*g/(mx-x)),firstclr[2]/2-(dist*b/(mx-x)))) 
                    
                    # Mark this point as visited (add that point to the visited set) 
            visited.add(pt) 
            
                    # Identify neighbours and add them to the to-be-visited list. 
            for n in neighbourhood: 
                visit_queue.append ( ( pt[0] + n[0], pt[1] + n[1] ) ) 


running = True
a=screen.copy()
while running:
    for evnt in event.get():            
        if evnt.type == QUIT:
            running = False
        if evnt.type == MOUSEBUTTONDOWN:
            cmx,cmy = evnt.pos
            a=screen.copy()
        if evnt.type == MOUSEBUTTONUP:
            umx,umy=evnt.pos
           

            
            
    mx,my = mouse.get_pos()
    mb = mouse.get_pressed()

    if mb[0]==1:
        screen.blit(a,(0,0))
        draw.rect(screen,(255,255,255),(cmx,cmy,mx-cmx,my-cmy),1)

    if mb[2]==1:
        screen.blit(a,(0,0))
        draw.line(screen,(255,255,255),(cmx,cmy),(mx,my),1)
        r=128
        g=128
        b=128
        if mx-cmx>=50:
            gradient(cmx,cmy,mx,my,(255,255,255),(0,0,0),r,g,b)
                    

    display.flip()
         
quit() [/code]
