#!/usr/bin/python from random import * # complimentary: h, h + 180 # split complimentary: h, h + 150, h + 210 # triad: h, h + 120, h + 240 # analagous: h, h + 30, h + 330 # mono: vary saturation, value def create_color_scheme(h, s, v): comp = (h + 180) % 360, s, v split_comp = ((h + 150) % 360, s, v), ((h + 210) % 360, s, v) triad = ((h + 120) % 360, s, v), ((h + 240) % 360, s, v) analagous = ((h + 30) % 360, s, v), ((h + 330) % 360, s, v) text = hsv_to_rgb(h, s, v) # whatever they gave us if (s < 15 and v > 90): # is the base color very light? bg = hsv_to_rgb(analagous[0][0], 100, v - 70) # dark color for bg else: bg = hsv_to_rgb(analagous[0][0], 10, 100) # light color for bg link = hsv_to_rgb(*split_comp[0]) linkhover = hsv_to_rgb(*split_comp[1]) return { 'text':text, 'background':bg, 'link':link, 'linkhover':linkhover } class RGB: vals = (0, 0, 0) # range 0-255 def __init__(self, r, g, b): self.vals = tuple([min(255, c) for c in (r, g, b)]) def r(self): vals[0] def g(self): vals[1] def b(self): vals[2] def __eq__(self, other): return self.vals == other.vals def __repr__(self): return "RGB(%d, %d, %d)" % self.vals def hexify(self): return "#%02X%02X%02X" % self.vals def __str__(self): return self.hexify() V = 0 P = 1 Q = 2 T = 3 selector = ((V, T, P), (Q, V, P), (P, V, T), (P, Q, V), (T, P, V), (V, P, Q)) def hsv_to_rgb(h, s100, v100): """Convert (hue, saturation, value) to (red, green, blue).""" assert (0 <= h < 360) assert (0 <= s100 <= 100) assert (0 <= v100 <= 100) hi = int(h / 60) % 6 f = h / 60.0 - hi s = s100 / 100.0 v = v100 / 100.0 p = v * (1.0 - s) q = v * (1.0 - f * s) t = v * (1.0 - (1.0 - f) * s) values = [v, p, q, t] return RGB(*tuple([int(256 * values[i]) for i in selector[hi]])) assert(hsv_to_rgb(200, 20, 60) == RGB(122, 143, 153)) assert(hsv_to_rgb(34, 45, 34) == RGB(87, 70, 47)) assert(hsv_to_rgb(34, 90, 34) == RGB(87, 53, 8)) assert(hsv_to_rgb(50, 90, 80) == RGB(204, 174, 20)) def make_style_sheet(color_scheme): return """ body { color:%(text)s; background-color:%(background)s; } a,a:link { color: %(link)s; outline: none; } a:hover { color: %(linkhover)s; } """ % color_scheme #print make_style_sheet(create_color_scheme(20, 75, 50)) print make_style_sheet(create_color_scheme(randint(0,359), randint(0,100), randint(0,100)))