#!/usr/bin/env python import sys, os, string, re import colorsys from collections import defaultdict colors = defaultdict(int) p = re.compile(r'[\#]([a-fA-F\d]{6}|[a-fA-F\d]{3})', re.IGNORECASE) explicitMap = { "157fcc":"dff4e9" } def rotateHue(rgb): hls = colorsys.rgb_to_hsv(rgb[0]/256.0,rgb[1]/256.0,rgb[2]/256.0) newHLS = (hls[0]-120.0/360,hls[1],hls[2])#blue to green newRGB = colorsys.hsv_to_rgb(*newHLS) #print hls,newHLS,newRGB newRGB256 = tuple([ int(v*256.0+0.5) for v in newRGB]) return newRGB256 def toGrayscale(rgb): yiq = colorsys.rgb_to_yiq(rgb[0]/256.0,rgb[1]/256.0,rgb[2]/256.0) newYIQ = (yiq[0],0,0) newRGB = colorsys.yiq_to_rgb(*newYIQ) #print hls,newHLS,newRGB newRGB256 = tuple([ int(v*256.0+0.5) for v in newRGB]) return newRGB256 def getNewColor(color): if(len(color) == 3): return color rgb = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4)) isGray = len(set(rgb)) == 1 if isGray: return color #if color in explicitMap: # return explicitMap[color] if(max(rgb) != rgb[0]): newRGB256 = toGrayscale(rgb) return '%02x%02x%02x' % (newRGB256[0],newRGB256[1],newRGB256[2]) else: return color mappedSkin = open("skin_styles.css","w") with open(sys.argv[1]) as f: for line in f: m = p.findall(line) for color in m: colors[color.lower()] += 1 line = line.replace(color,getNewColor(color)) mappedSkin.write(line) #print len(colors),"colors" print ".mycolors {\n" nonGray = set() for color,count in sorted(colors.items(), key=lambda x: -x[1]): if(len(color) == 3): #these are all shades of gray continue rgb = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4)) isGray = len(set(rgb)) == 1 if isGray: continue nonGray.add(color) r,g,b = color[0:2],color[2:4],color[4:6] newColor = getNewColor(color) comment = "" if(color == newColor): continue print " color: #"+color+";", print "color: #"+newColor+"; /* %s */"%comment print "}\n" #print "Non gray: ",len(nonGray)