'''PrimeNumbersImage.py Create a PixelMath image that shows where the primes are.''' W = None; H = None def build_prime_image(width, height): global W, H W = width; H = height wn = pmNewImage(0, "Where the Primes Are", W, H, 0,0,0) pmSetDestination(wn) nprimes_so_far = 0 for y in range(height): for x in range(width): number = y*width+x if number>1: (r,g,b)=pmGetPixel(wn,x,y) if r+g+b==0: pmSetPixel(wn,x,y,255,0,0) # prime colorMultiples(number, getColor(nprimes_so_far)) nprimes_so_far += 1 def colorMultiples(n, color): if DEBUG: report("colorMultiples", "n="+str(n)) f = "if (y*"+str(W)+"+x)>"+str(n)+\ " and (y*"+str(W)+"+x) mod "+str(n)+\ "=0 then rgb"+str(color)+" else d(x,y)" pmSetFormula(f) pmCompute() def getColor(i): if DEBUG: report("getColor", "i="+str(i)) return [(0,128,0),(0,0,128),(96,128,0),(96,0,128),(0,96,128)][i % 5] DEBUG = True CALL_COUNTS = {} MAX_REPORTS_PER_FUNCTION = 10 def report(functionName, message): try: c=CALL_COUNTS[functionName] except KeyError: c=0 CALL_COUNTS[functionName]=c+1 if c