1 |
# Choosing colors and 3-dotproduct factors for papers. |
# Choosing colors and 3-dotproduct factors for papers. |
2 |
|
|
3 |
from gfx.libcolor.spaces import getRandomColor,getRandomColor2,RGBtoLAB,LABtoRGB,LABclamp,getangle |
from gfx.libcolor.spaces import getRandomColor,getRandomColor2,YSTtoRGB,clampSat |
4 |
|
from gfx.libcolor.spaces import RGBtoLAB,LABtoRGB,LABclamp |
5 |
|
|
6 |
from math import sin,cos,atan2,pi,log |
from math import sin,cos,atan2,pi,log |
7 |
from random import Random,shuffle |
from random import Random,shuffle |
19 |
|
|
20 |
colors = 8 |
colors = 8 |
21 |
minlum = 80 |
minlum = 80 |
|
#huerange = (45 + rnd.nextGaussian() * 45) |
|
22 |
huerange = rnd.nextGaussian() * 90 |
huerange = rnd.nextGaussian() * 90 |
23 |
|
|
24 |
# Note: This color sampling scheme only produces |
# Note: This color sampling scheme only produces |
25 |
# palettes with similar colors. |
# palettes with similar colors. |
26 |
# It could be nice to have other schemes |
# It could be nice to have other schemes |
27 |
# with, e.g., complementary colors. |
# with, e.g., complementary colors. |
28 |
# (Note: color complementing should be done in RGB space) |
|
29 |
|
# Add orange color to the color circle |
30 |
|
def getangle(f): |
31 |
|
# 0 = red, 120 = green, 240 = blue |
32 |
|
angles = [ 0, 30, 60, 120, 180, 240, 300, 360 ] |
33 |
|
n = len(angles) - 1 |
34 |
|
f *= n / 360.0 |
35 |
|
index = int(f) % n |
36 |
|
fract = f - int(f) |
37 |
|
return (1 - fract) * angles[index] + fract * angles[index + 1] |
38 |
|
|
39 |
# Sample hues uniformly from the range shifted to a random angle |
# Sample hues uniformly from the range shifted to a random angle |
40 |
if 0: |
hue0 = rnd.nextDouble() * 360 |
41 |
huerange *= pi / 180 |
hues = ([hue0, hue0 + huerange] + |
42 |
hue0 = rnd.nextDouble() * 2*pi |
[hue0 + rnd.nextDouble() * huerange for i in range(2,colors)]) |
43 |
hues = ([hue0, hue0 + huerange] + |
hues = map(getangle, hues) |
44 |
[hue0 + rnd.nextDouble() * huerange for i in range(2,colors)]) |
shuffle(hues, rnd.nextDouble) |
|
shuffle(hues, rnd.nextDouble) |
|
|
else: |
|
|
huerange /= 360 |
|
|
hue0 = rnd.nextDouble() |
|
|
hues = ([hue0, hue0 + huerange] + |
|
|
[hue0 + rnd.nextDouble() * huerange for i in range(2,colors)]) |
|
|
hues = map(getangle, hues) |
|
|
shuffle(hues, rnd.nextDouble) |
|
|
|
|
45 |
|
|
46 |
# Take one half dark colors and one half light colors |
# Take one half dark colors and one half light colors |
47 |
lumrange = 100 - minlum |
lumrange = 100 - minlum |
54 |
# Sample saturation: |
# Sample saturation: |
55 |
# - take the most saturated color 2/3 of the time |
# - take the most saturated color 2/3 of the time |
56 |
# and a dull color 1/3 of the time |
# and a dull color 1/3 of the time |
57 |
sats = [100 * (1 - (1 - (1 - rnd.nextDouble())**2) * (rnd.nextDouble() < .333)) |
sats = [(1 - (1 - (1 - rnd.nextDouble())**2) * (rnd.nextDouble() < .333)) |
58 |
for i in range(0, colors)] |
for i in range(0, colors)] |
59 |
|
|
60 |
# Construct colors and clamp towards the CIELAB L-axis |
# Construct colors and clamp to RGB cube keeping hue and luminance constant |
61 |
# (keeping hue and luminance) to fit into the RGB cube |
yst = [(pow((lums[i] + 16.0) / 116, 3), |
62 |
lab = [(lums[i], sats[i] * cos(hues[i]), sats[i] * sin(hues[i])) |
sats[i] * cos(hues[i]*pi/180), |
63 |
for i in range(0,colors)] |
sats[i] * sin(hues[i]*pi/180)) |
64 |
col = [LABclamp(LABtoRGB(c)) for c in lab] |
for i in range(0,colors)] |
65 |
|
|
66 |
|
col = [clampSat(YSTtoRGB(c)) for c in yst] |
67 |
shuffle(col, rnd.nextDouble) |
shuffle(col, rnd.nextDouble) |
68 |
|
|
69 |
if dbg: |
if dbg: |