Skip to content

Instantly share code, notes, and snippets.

@jberkel
Created June 18, 2009 13:15
Show Gist options
  • Save jberkel/131898 to your computer and use it in GitHub Desktop.
Save jberkel/131898 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#http://hse-at-work.blogspot.com/2006/11/color-scheme-generator-for-charts.html
from random import randint
class Color:
__c = [0, 0, 0]
__cB = None
def __init__(self, r=-1, g=-1, b=-1):
if r == -1:
r = randint(1,255)
if g == -1:
g = randint(1,255)
if b == -1:
b = randint(1,255)
self.__c = [r, g, b]
def __getitem__(self, key):
return self.__c[key]
def __setitem__(self, key, value):
self.__c[key] = value
self.__cB = None
def getBritness(self):
if self.__cB:
return self.__cB
else:
self.__cB = ((self.__c[0] * 299) +
(self.__c[1] * 587) +
(self.__c[2] * 114)) / 1000
return self.__cB
def __sub__(self, other):
return ((max(self.__c[0], other[0]) - min(self.__c[0], other[0])) +
(max(self[1], other[1]) - min(self[1], other[1])) +
(max(self[2], other[2]) - min(self[2], other[2])))
def __str__(self):
return "%02x%02x%02x" % (self.__c[0], self.__c[1], self.__c[2])
def generate(num=25):
colors = []
comparedNum = 5
c = Color()
colors.append(c)
stopper = 100000
minBrit = 35
minColDiff = 100
for n in range(num)[1:]:
while stopper:
newC = Color()
passed = 0
for back in range(comparedNum):
testedNum = n-(back+1)
if testedNum < 0:
passed += 1
continue
c = colors[testedNum]
bDiff = abs(c.getBritness() - newC.getBritness())
cDiff = c - newC
if cDiff >= minColDiff and bDiff > minBrit:
passed += 1
else:
break
if passed == comparedNum:
colors.append(newC)
break
stopper -= 1
if not stopper > 0:
raise InvalidState
return colors
if __name__ == '__main__':
for c in generate(5): print c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment