from PIL import Image

button = "pause"
template = "clearCircles"
background = "back"
stroke = "#000000"
fade = "#000080"


def iconAssembler(button, template, stroke, fade, background):
    back = Image.open("themes\\backgrounds\\" + background + ".png")
    back = back.convert('RGBA')
    icon = Image.open("themes\\" + template + "\\" + button + ".png")
    icon = icon.convert('RGBA')
    icon = replaceColors(icon, stroke, fade)
    icon.save("themes\\backgrounds\icon.png")
    #    icon.show()
    mask = Image.open("themes\\" + template + "\\" + button + "Mask.png")
    mask = mask.convert('L')
    #    mask.show()
    back.putalpha(mask)
    #    back.show()
    final = Image.alpha_composite(back, icon)
    status = "Successfully generated icons!"
    final.save("themes\\backgrounds\\test.png")
    return status


def cvHex(hexstr):
    return (int(hexstr, 16))


def replaceColors(icon, stroke, fade):
    srgb = list((cvHex(stroke[1:3]), cvHex(stroke[3:5]), cvHex(stroke[5:7])))
    frgb = list((cvHex(fade[1:3]), cvHex(fade[3:5]), cvHex(fade[5:7])))
    print srgb
    pix = icon.load()

    def replaceFade(frgb, pix):
        rgbt = list(frgb)
        if pix[x, y][3] < 200:
            pass
            # rgbt[0] = rgbt[0] - (255 - pix[x, y][0])
            # rgbt[1] = rgbt[1] - (255 - pix[x, y][1])
            # rgbt[2] = rgbt[2] - (255 - pix[x, y][2])
        rgbt.append(pix[x, y][3])
        rgbt = tuple(rgbt)
        pix[x, y] = rgbt
        return pix

    def replaceStroke(srgb, pix):
        rgbt = list(srgb)
        # rgbt[0] = rgbt[0] - (255 - pix[x, y][0])
        rgbt[1] -= 255 - pix[x, y][1]
        # rgbt[2] = rgbt[2] - (255 - pix[x, y][2])
        rgbt.append(pix[x, y][3])
        rgbt = tuple(rgbt)
        pix[x, y] = rgbt
        return pix

    for y in xrange(icon.size[1]):
        for x in xrange(icon.size[0]):
            if pix[x, y][0:3] in ((0,0,0),(255,255,255)):
                continue
            if (pix[x, y][0] - pix[x,y][2]) < 150:
                if pix[x, y][1] < 255:
                    pix = replaceFade(frgb,pix)
            if pix[x, y][0] == pix[x, y][2]: #and pix[x, y][1] is not 255:
                pix = replaceStroke(srgb,pix)
                continue
            # if pix[x, y][1] == 255:
            #     pix = replaceStroke(srgb,pix)



    return icon


if __name__ == '__main__':
    iconAssembler(button, template, stroke, fade, background)

@@ -90,18 +90,18 @@
 oke = %22#
-00
+aa
 0000%22%0Afa
@@ -1111,30 +1111,30 @@
  def replace
-Fade(f
+Color(
 rgb, pix):%0A 
@@ -1156,227 +1156,13 @@
 ist(
-f
 rgb)%0A
-        if pix%5Bx, y%5D%5B3%5D %3C 200:%0A            pass%0A            # rgbt%5B0%5D = rgbt%5B0%5D - (255 - pix%5Bx, y%5D%5B0%5D)%0A            # rgbt%5B1%5D = rgbt%5B1%5D - (255 - pix%5Bx, y%5D%5B1%5D)%0A            # rgbt%5B2%5D = rgbt%5B2%5D - (255 - pix%5Bx, y%5D%5B2%5D)%0A
     
@@ -1271,100 +1271,104 @@
     
-def replaceStroke(srgb, pix):%0A        rgbt = list(srgb)%0A        # rgbt%5B0%5D = rgbt%5B0%5D - (255 -
+for y in xrange(icon.size%5B1%5D):%0A        for x in xrange(icon.size%5B0%5D):%0A            currentPixel =
  pix
@@ -1373,20 +1373,16 @@
 ix%5Bx, y%5D
-%5B0%5D)
 %0A       
@@ -1386,268 +1386,151 @@
     
-rgbt%5B1%5D -= 255 - pix%5Bx, y%5D%5B1%5D%0A        # rgbt%5B2%5D = rgbt%5B2%5D - (255 - pix%5Bx, y%5D%5B2%5D)%0A        rgbt.append(pix%5Bx, y%5D%5B3%5D)%0A        rgbt = tuple(rgbt)%0A        pix%5Bx, y%5D = rgbt%0A        return pix%0A%0A    for y in xrange(icon.size%5B1%5D):%0A        for x in xrange(icon.size%5B0%5D):
+    if currentPixel%5B0:3%5D in ((0,0,0),(255,255,255)):%0A                continue%0A            elif currentPixel%5B3%5D == 999:%0A                continue
 %0A   
@@ -1542,52 +1542,41 @@
     
+el
 if 
-pix%5Bx, y%5D%5B0:3%5D in ((0,0,0),(255,255,255))
+min(currentPixel%5B0:3%5D) %3E 250
 :%0A  
@@ -1614,45 +1614,54 @@
     
+el
 if 
-(pix%5Bx, y%5D%5B0%5D - pix%5Bx,y%5D%5B2%5D) %3C 150
+max(currentPixel%5B0:3%5D) == currentPixel%5B1%5D
 :%0A  
@@ -1681,26 +1681,28 @@
  if 
-pix%5Bx, y%5D%5B1%5D %3C 255
+currentPixel%5B1%5D %3E 64
 :%0A  
@@ -1736,18 +1736,20 @@
 lace
-Fade(f
+Color(s
 rgb,
+ 
 pix)
@@ -1765,69 +1765,208 @@
     
-if pix%5Bx, y%5D%5B0%5D == pix%5Bx, y%5D%5B2%5D: #and pix%5Bx, y%5D%5B1%5D is not 255
+        continue%0A            elif max(currentPixel%5B0:3%5D) == currentPixel%5B0%5D:%0A                pix = replaceColor(frgb,pix)%0A                continue%0A            # elif currentPixel%5B0%5D == currentPixel%5B2%5D
 :%0A  
@@ -1967,36 +1967,38 @@
 2%5D:%0A            
+#
     
+ 
 pix = replaceStr
@@ -1986,38 +1986,37 @@
    pix = replace
-Stroke
+Color
 (srgb,pix)%0A     
@@ -2013,32 +2013,34 @@
 pix)%0A           
+ #
      continue%0A  
@@ -2123,19 +2123,16 @@
 gb,pix)%0A
-%0A%0A%0A
     retu

remove code reuse, restructure replacement tree