Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mun.py
blob: d352d3861e8b761fdeabb435d42ef6edab860e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
#Copyright (c) 2012 Walter Bender

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA

# Albert Munsell-inspired class library
# Chromas at values 2-9 by hue

MUN = [
        [ # Neutral
        [52, 52, 52], [73, 73, 73], [97, 97, 97], [122, 122, 122],
	 [147, 147, 147], [173, 173, 173], [200, 200, 200], [228, 228, 228]],
        [ # Chroma low
	# 5R
	[[66, 44, 49], [90, 69, 66], [115, 93, 90], [140, 117, 115],
	 [165, 142, 140], [189, 170, 165], [214, 199, 198], [247, 227, 222]],
	# 5YR
	[[66, 48, 41], [90, 69, 57], [115, 93, 82], [140, 117, 107],
	 [165, 142, 132], [189, 170, 156], [222, 199, 181], [247, 227, 214]],
	# 5Y
	[[57, 52, 33], [82, 73, 49], [107, 97, 74], [132, 121, 99],
	 [156, 146, 123], [181, 174, 148], [214, 203, 173], [239, 231, 198]],
	# 5GY
	[[49, 52, 41], [66, 77, 57], [90, 97, 74], [115, 125, 99],
	 [140, 150, 123], [173, 178, 148], [198, 207, 173], [231, 235, 198]],
	# 5G
	[[41, 52, 49], [57, 77, 66], [82, 101, 90], [107, 125, 115],
	 [132, 154, 140], [156, 182, 165], [181, 207, 189], [206, 239, 222]],
	# 5BG
	[[33, 52, 49], [57, 77, 74], [74, 101, 99], [99, 125, 123],
	 [123, 154, 148], [148, 178, 173], [181, 207, 206], [206, 235, 231]],
	# 5B
	[[41, 52, 57], [57, 77, 82], [82, 101, 107], [107, 125, 132],
	 [132, 150, 156], [156, 178, 181], [181, 207, 214], [214, 235, 239]],
	# 5PB
	[[41, 52, 66], [66, 73, 90], [90, 97, 107], [115, 121, 132],
	 [140, 150, 156], [165, 174, 189], [198, 203, 214], [222, 231, 239]],
	# 5P
	[[57, 48, 57], [82, 69, 82], [99, 93, 107], [123, 117, 132],
	 [156, 146, 156], [181, 170, 181], [206, 199, 214], [231, 227, 239]], 
	# 5RP
	[[66, 44, 57], [90, 69, 74], [107, 93, 99], [132, 117, 123],
	 [165, 142, 148], [189, 170, 173], [214, 199, 206], [239, 227, 231]]],
	[# Chroma moderate
	# 5R
	[[82, 32, 41], [115, 52, 57], [140, 81, 74], [173, 105, 99],
	 [198, 130, 123], [222, 158, 156], [239, 190, 189], [-1, -1, -1]],
	# 5YR
	[[82, 40, 8], [107, 60, 24], [132, 85, 49], [165, 109, 66],
	 [189, 137, 90], [222, 162, 115], [247, 190, 140], [-1, -1, -1]],
	# 5Y
	[[57, 48, 8], [82, 73, 33], [115, 93, 24], [140, 121, 41],
	 [165, 146, 66], [198, 174, 90], [222, 203, 115], [247, 231, 173]],
	# 5GY
	[[41, 56, 24], [57, 81, 16], [82, 105, 33], [107, 130, 57],
	 [132, 154, 82], [165, 182, 107], [189, 211, 123], [222, 239, 148]],
	# 5G
	[[24, 56, 41], [41, 81, 66], [33, 109, 82], [57, 134, 107],
	 [82, 162, 123], [115, 190, 156], [140, 219, 181], [156, 247, 206]],
	# 5BG
	[[8, 56, 57], [24, 81, 82], [49, 105, 99], [41, 134, 132],
	 [66, 162, 156], [99, 190, 181], [123, 219, 206], [140, 247, 231]], 
	# 5B
	[[16, 56, 74], [33, 77, 99], [16, 105, 132], [49, 130, 156],
	 [82, 158, 181], [107, 186, 206], [132, 215, 239], [-1, -1, -1]],
	# 5PB V2-V9
	[[24, 52, 90], [49, 73, 115], [74, 97, 140], [99, 121, 165],
	 [123, 150, 189], [148, 174, 214], [173, 203, 247], [-1, -1, -1]],
	# 5P V2-V9
	[[66, 40, 74], [90, 60, 107], [115, 85, 132], [140, 113, 156],
	 [165, 138, 181], [189, 166, 206], [214, 195, 239], [-1, -1,-1]],
	# 5RP V2-V9
	[[82, 36, 57], [107, 56, 82], [132, 81, 99], [165, 105, 123],
	 [189, 134, 148], [214, 158, 181], [247, 186, 206], [-1, -1, -1]]],
	[# max chroma
	# 5R
	[[99, 24, 41], [140, 32, 41], [189, 36, 49], [239, 32, 57],
	 [247, 101, 99], [239, 150, 148], [-1, -1, -1], [-1, -1, -1]],
	# 5YR
	[[-1, -1, -1], [-1, -1, -1], [140, 81, 33], [181, 101, 24],
	 [222, 125, 24], [247, 154, 82], [-1, -1, -1], [-1, -1, -1]],
	# 5Y
	[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1],
	 [173, 146, 41], [206, 174, 16], [231, 199, 57], [-1, -1, -1]],
	# 5GY
	[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1], [107, 130, 24],
	 [132, 158, 49], [156, 186, 41], [181, 215, 8], [206, 247, 41]],
	# 5G
	[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1], [8, 138, 99],
	 [57, 166, 123], [41, 199, 140], [74, 227, 165], [-1, -1, -1]],
	# 5BG
	[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1],
	 [-1,-1, -1], [57, 195, 181], [82, 223, 206], [-1, -1, -1]], 
	# 5B
	[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1], [-1, -1, -1],
	 [24,162, 198], [74, 190, 222], [-1, -1, -1], [-1, -1, -1]], 
	# 5PB
	[[-1, -1, -1], [33, 73, 123], [41, 97, 165], [49, 121, 206],
	 [82,150, 239], [140, 178, 231], [-1, -1, -1], [-1, -1, -1]], 
	# 5P
	[[82, 20, 99], [115, 20, 148], [165, 16, 198], [198, 48, 239],
	 [198, 109, 247], [206, 154, 247], [-1, -1, -1], [-1, -1, -1]],
	# 5RP
	[[99, 8, 66], [140, 16, 90], [189, 20, 115], [231, 40, 140],
	 [247, 93, 165], [247, 146, 181], [-1, -1, -1], [-1, -1, -1]]]]

from random import uniform

class MunColor():

    def __init__(self):
        self.hue = 0
	self.value = 5
        self.chroma = 1

    def set_hue(self, hue):
        self.hue = hue % 10

    def set_value(self, value):
        self.value = max(min(value, 9), 2)

    def set_chroma(self, chroma):
        self.chroma = max(min(chroma, 3), 0)

    def random(self):
        self.hue = int(uniform(0, 10))
        self.value = int(uniform(2, 10))
        self.chroma = int(uniform(0, 4))
        return self.get_color()

    def random_chroma(self, c1, c2):
        self.hue = int(uniform(0, 10))
        self.value = int(uniform(2, 10))
        self.chroma = int(uniform(c1, c2)) % 4
        return self.get_color()

    def random_value(self, v1, v2):
        self.hue = int(uniform(0, 10))
        self.value = max(min(int(uniform(v1, v2)), 9), 2)
        self.chroma = int(uniform(0, 4))
        return self.get_color()

    def random_hue(self, h1, h2):
        self.hue = int(uniform(h1, h2)) % 10
        self.value = int(uniform(2, 10))
        self.chroma = int(uniform(0, 4))
        return self.get_color()

    def random_hvc(self, h1, h2, v1, v2, c1, c2):
        self.hue = int(uniform(h1, h2)) % 10
        self.value = max(min(int(uniform(v1, v2)), 9), 2)
        self.chroma = int(uniform(c1, c2)) % 4
        return self.get_color()

    def hvc(self, hue, value, chroma):
        self.set_hue(hue)
        self.set_value(value)
        self.set_chroma(chroma)
        return self.get_color()

    def get_color(self):
        c = self.chroma
	rgb = [-1, -1, -1]
	while -1 in rgb:
           if c == 0:
               rgb = MUN[0][self.value - 2]
           else:
               rgb = MUN[c][self.hue][self.value - 2]
           if -1 in rgb:
               c -= 1
        return '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])