-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_model.js
230 lines (195 loc) · 6.09 KB
/
user_model.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// normal distribution
function gaussian(x, mu, sigma)
{
const TWO_PI_SQRT = Math.sqrt(2 * Math.PI);
var sigma2 = sigma*sigma;
var e = Math.exp(-.5 * Math.pow(x - mu, 2) / sigma2);
return e / (sigma * TWO_PI_SQRT);
}
var MAX_HARD_KEYS = 4;
var HIGH_IMPORTANCE = 1.0;
var SERIAL = 0;
var MAX_PREF_STRENGTH = gaussian(0, 0, MIN_BLOCK_SIZE*2);
/* ColorPreference: implements a color block that indicates user preference for a
* particular colors
*/
function ColorPreference(start, end, color)
{
this.update(start, end)
this.color = color;
this.serial = SERIAL++;
}
ColorPreference.prototype.update = function(start, end) {
this.start = start;
this.end = end;
this.mu = .5 * (this.start + this.end);
this.sigma = .5 * (this.end-this.start);
}
ColorPreference.prototype.copy = function() {
var copy = new ColorPreference(this.start, this.end, this.color);
copy.update(this.start, this.end);
return copy;
}
ColorPreference.prototype.evaluateStrength = function(keyI)
{
return gaussian(keyI, this.mu, this.sigma);
}
ColorPreference.prototype.getMaxStrength = function()
{
return gaussian(0, 0, this.sigma);
}
ColorPreference.prototype.evaluateRelStrength = function(keyI)
{
return this.evaluateStrength(keyI) / MAX_PREF_STRENGTH;
}
ColorPreference.prototype.testIntersection = function(colorBlock)
{
function intersect(b1, b2)
{
var s = b1.start, e = b1.end;
var intersectionBool = (b2.start >= s && b2.start <= e) || (b2.end >= s && b2.end <= e);
return intersectionBool;
}
return intersect(this, colorBlock) || intersect(colorBlock, this);
}
ColorPreference.prototype.renderGradient = function(width, height, totalWidth)
{
var GRAD_STEP = 1;
// luminance profile
var luminanceGenerator = getLuminanceProfile();
// create offscreen canvas
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
// get original color
var color = d3.lab(this.color);
// loop through pixels
for (var pixel=0; pixel<width; pixel+= GRAD_STEP)
{
var keyI = (pixel/width) * (this.end-this.start) + this.start;
color.l = luminanceGenerator.getLuminance( keyI );
// render checker background
/*
{
var CHECKER_SIZE = 8;
var col = Math.floor(keyI * totalWidth);
for (var row=0; row<height; row+= GRAD_STEP)
{
var indexR = (row / CHECKER_SIZE) % 2;
var index = ((indexR >= 1 ? col+CHECKER_SIZE : col) / CHECKER_SIZE) % 2;
if (index >= 1) {
context.fillStyle = "rgb(200, 200, 200)";
context.fillRect(pixel, row, GRAD_STEP, GRAD_STEP);
}
}
}
*/
color.opacity = Math.max(1.0, 1.5*this.evaluateRelStrength(keyI));
if (color.displayable())
{
context.fillStyle = color.toString();
context.fillRect(pixel, 0, GRAD_STEP, height);
}
else {
var trimmedColor = trimColorToGamut(color);
trimmedColor.opacity = color.opacity;
context.fillStyle = trimmedColor.toString();
context.fillRect(pixel, 0, GRAD_STEP, height);
}
}
return canvas;
}
/* UserModel: implements user preference */
function UserModel(prefColorBlocks)
{
this.hardKeys = [];
this.updateBiasVectors(prefColorBlocks)
}
UserModel.prototype.updateBiasVectors = function(prefColorBlocks)
{
if (!prefColorBlocks) {
// empty preference blocks
prefColorBlocks = [];
}
var bias = [];
// for all keys
var keyCount = getLuminanceProfile().getKeyMultiples();
for (var i=0; i<keyCount; i++)
{
// look through all colors
var keyI = i/(keyCount-1);
var biasForKey = [];
for (var j=0, len=prefColorBlocks.length; j<len; j++)
{
var prefBlock = prefColorBlocks[j];
var mu = (prefBlock.start + prefBlock.end)/2;
var sigma = (prefBlock.end-prefBlock.start)/2;
// evaluate a gaussian centered at the middle of the block
var h = gaussian(keyI, mu, sigma);
var maxH = gaussian(0, 0, sigma);
// create pref color
var prefColor = [prefBlock.color.l, prefBlock.color.a, prefBlock.color.b];
// accumilate the contribution of the color weighted by the gaussian
biasForKey.push({
/*
strength: h,
maxHeight: maxH,
*/
relativeStrength: prefBlock.evaluateRelStrength(keyI),
preference: prefColor
});
}
bias.push(biasForKey);
}
this.bias = bias;
}
UserModel.prototype.test = function()
{
console.log("test function")
}
UserModel.prototype.getBias = function()
{
var biasCopy = [];
for (var i=0, len=this.bias.length; i<len; i++)
{
biasCopy.push(this.bias[i].slice());
}
// add in hard keys
for (var i=0, len=this.hardKeys.length; i<len; i++)
{
var hardKey = this.hardKeys[i];
biasCopy[hardKey.index].push({
relativeStrength: MAX_PREF_STRENGTH * hardKey.importance,
preference: hardKey.color
});
}
return biasCopy;
}
UserModel.prototype.addHardKey = function(keyIndex, color)
{
// see if key index is already there
for (var i=0; i<this.hardKeys.length; i++)
{
var k = this.hardKeys[i];
if (k.index == keyIndex)
{
this.hardKeys.splice(i, 1);
}
}
// add key at the end
this.hardKeys.push({
importance: 1,
index: keyIndex,
color: color
});
// remove element if exceeding maximum allowed
if (this.hardKeys.length > MAX_HARD_KEYS) {
this.hardKeys.shift();
}
// re-evaluate importance
for (var i=0, lastIndex = this.hardKeys.length-1; i<=lastIndex; i++) {
var importance = Math.exp(-.6 * (lastIndex-i));
this.hardKeys[i].importance = importance;
}
}