from colrconv import *


def remap(i,mini,maxi,minj,maxj):
  return (i-mini)*(maxj-minj)/(maxi-mini)+minj

def mandelbrot(sx,sy,maxi):
  sx = remap(sx,0,width,centerx-pwidth/2,centerx+pwidth/2)
  sy = remap(sy,0,height,centery-pheight/2,centery+pheight/2)
  x,y,i = (0,0,0)
  while x**2+y**2 < 2**2 and i < maxi:
    newx = x**2-y**2+sx
    y = 2*x*y+sy
    x = newx
    i += 1
  if i == maxi:
    return -1
  return i


centerx = -0.75
centery = 0
pwidth = 2.5
pheight = 2.5
height = 100
width = 100
maxi = 100
it = []
ipp = [0 for a in range(maxi+1)]
for i in range(height):
  it.append([])
  for j in range(width):
    it[i].append(mandelbrot(j,i,maxi))
    if it[i][j] != -1:
      ipp[it[i][j]] += 1
'''
for i in range(height):
  for j in range(width):
    if it[i][j] == -1:
      color = (0,0,0)
    else:
      g = round(remap(it[i][j]**2,0,maxi,0,255))
      color = (0,g,255)
    set_pixel(j,i,color)
  show_screen()
'''
total = 0
for i in range(maxi+1):
  total += ipp[i]

for i in range(height):
  for j in range(width):
    if it[i][j] == -1:
      color = (0,0,0)
    else:
      h = 0
      for ite in range(it[i][j]):
        h += ipp[ite]/total
      l = round(remap(h,0,1,25,50))
      h = round(remap(h,0,1,0,60))
      color = HSLtoRGB(h,100,l)
    set_pixel(j,i,color)
  show_screen()
