def findCommon(keys): cM = 5.1 order = [] tmp = [] for x in range(len(keys)): try: tmp.append([x,len(lumlib[keys[x]].keys())]) except KeyError as k: # TODO: Fix this so it doesn't keep analyzing colors with no higher pairings. print "Analyzing BG color %s..." % k (rloop,gloop,bloop) = color.getLoops(0,0,0,255,255,255,51) # all websafe colors only for nr in rloop: #Red loop: Start at minr, stop at maxr, increment by inc every time around. for ng in gloop: #Green loop: Start at ming, stop at maxg, increment by inc every time around. for nb in bloop: #Blue loop: Start at minb, stop at maxb, increment by inc every time around. nc = "%02X%02X%02X" % (nr,ng,nb) nratio = gridLums(keys[x],nc) try: tmp.append([x,len(lumlib[keys[x]].keys())]) except KeyError: tmp.append([x,0]) tmp = sorted(tmp,key = lambda y:y[1]) for z in tmp: order.append(z[0]) # print order commonfore = [] for y in order: comp = [] x = keys[y] try: comp = lumlib[x].keys() if commonfore == []: for i in comp: commonfore.append(str(i)) except KeyError: print "Missing data for %s. Please run daos to populate grid for this color. Grid uses 6-digit codes." % x return commonfore for lc in comp: cA = 0.0 try: cA = float(lumlib[x][lc]) except KeyError: print "Missing data for %s." % x cA = gridLums(x,lc) return commonfore if (cA < cM): comp.remove(lc) commonfore = color.commonList(commonfore,comp) return commonfore
def compileColors(c,minr,ming,minb,maxr,maxg,maxb,inc,sizeType,**kwargs): """Given all inputs, compiles a list of tuples containing colors that work as backgrounds to the chosen color. Returns this list. Inputs: A color code (string) Six ints (ming R/G/B, max R/G/B) An increment (int) a size tyoe for setting the minimum contrasts (char) Keyword args: method: 0 - Legacy method C/B, 1 - WCAG, 2 - WCAG/sortbylum, 3 - Unfiltered hue requirement (int): 0: all hues 1: reddish hues 2: greenish hues 3: yellowish hues 4: bluish hues 5: magentine hues 6: cyanate hues 7: grays """ global colors hue = '0' method = '1' goOn = False cM = 5.0 # Minimum contrast ratio cG = 7.0 # Good contrast ratio cD = 0 # How many colors will we display? colorMatches = 0 # How many accessible colors will we find? rr = 0 # We'll break it down into red, gg = 0 # green, bb = 0 # and blue. colors = [] for key in kwargs: if key == "method": method = str(kwargs[key]) if key == "hue": hue = str(kwargs[key]) c = color.valColor(c) if c is None: say(0,"Invalid color!\n") goOn = False else: goOn = True (minr,ming,minb,maxr,maxg,maxb) = color.valMinMax(minr,ming,minb,maxr,maxg,maxb) # Based on the character 'sizeType'... (cM,cG) = color.contLevels(sizeType) say(0,"...") if goOn: (rr,gg,bb) = color.unStringColor(c) if method == '2': inc = 51 # Method 2 always displays every color, so we'll only do it with the WebSafe colorspace. if inc == 1: inc = color.sanityCheck(rr,gg,bb) # If we're testing all colors, decide if this is something a sane person would do. say(1,"Using increments of {0}...".format(inc)) if hue != '0': # Looking at hue... say(1,"limited to ") hues = "" if hue == '1': hues = "reddish hues." elif hue == '2': hues = "greenish hues." elif hue == '3': hues = "yellowish hues." elif hue == '4': hues = "bluish hues." elif hue == '5': hues = "magentine hues." elif hue == '6': hues = "cyanate hues." elif hue == '7': hues = "grays." else: hues = "all hues." say(0,hues + "\n") say(0," Color range used is r:{0}-{1}/g:{2}-{3}/b:{4}-{5}.\n".format(minr,maxr - 1,ming,maxg - 1,minb,maxb - 1)) # Tell user about requested (or corrected) color ranges. goOn = False; # Don't continue... unless the next test passes. # say(0,"<p class=\"yourcolor\" style=\"border-color: #{0};\">Color entered: R: {1} G: {2} B: {3} (#{0})</p>".format(c, rr,gg,bb) # Remind user of the given color. It's been so long, I almost forgot. # Replace below with GTK fanciness to show colored stuff. say(0,"Color entered: R: {1} G: {2} B: {3} (#{0})\n".format(c, rr,gg,bb)) # Remind user of the given color. It's been so long, I almost forgot. (colorbrightness,cont0,contf,givenLum) = color.colorVitals(rr,gg,bb) say(1,"Color has a brightness of %s" % colorbrightness) say(1,"and a luminance of {0:.2%}.".format(givenLum)) say(0,"Its contrast with black is %s, and its contrast with white is %s.\n" % (cont0,contf)) # The user might be interested in this information. Tell the user what we've learned. (rloop,gloop,bloop) = color.getLoops(minr,ming,minb,maxr,maxg,maxb,inc) for nr in rloop: #Red loop: Start at minr, stop at maxr, increment by inc every time around. for ng in gloop: #Green loop: Start at ming, stop at maxg, increment by inc every time around. for nb in bloop: #Blue loop: Start at minb, stop at maxb, increment by inc every time around. if color.chkHue(nr,ng,nb,int(hue)): # Is the color we're testing of the proper hue type? nratio = 0.0 try: nc = "%02X%02X%02X" % (nr,ng,nb) (c1,c2) = sorted((str(nc),str(c))) nratio = float(lumlib[str(c1)][str(c2)]) print "!", except KeyError: print "?", nLum = color.lum(nr,ng,nb) # Luminance of the tested color is what? nratio = color.lumrat(givenLum,nLum) # Ratio of tested luminance vs. given color's luminance is what? if method == '0': # Use the old contrast/Brightness method (deprecated) nBright = color.getBrightness(nr,ng,nb) # Check out the brightness of this color. brightDiff = color.fabs(nBright - colorBrightness) # How different is it? if brightDiff > 125: # diff in bright should be greater than 125 contrast = color.getContrast(rr,gg,bb,nr,ng,nb) # Oh, check the contrast between these two colors if contrast > 500: # contrast should be greater than 500 colors.append(color.storeGoodColors(nr,ng,nb,nratio)) cD += 1; colorMatches += 1 elif nratio >= cM or method == '2': # Is the color match good enough to display? colors.append(color.storeGoodColors(nr,ng,nb,nratio)) color.storeLumRatios(rr,gg,bb,nr,ng,nb,nratio) cD += 1 if nratio >= cG: # Is the color match a preferable match? colorMatches += 1 if cD >= MAXMATCHES: # If we have a lot of colors to display... bloop = [] say(0,"Maximum matches (" + str(MAXMATCHES) + ") reached. Aborting process to save CPU resources!\n") # Tell the user that there were too many. break if colorMatches >= MAXSORTED and method == '3': # If we have a lot of colors to sort... bloop = [] say(0,"Maximum sorted matches (" + str(MAXSORTED) + ") reached. Aborting process to prevent browser spinning!\n") break print ".", return colors