Class syntax

This commit is contained in:
Mimi 2020-07-24 15:22:41 +08:00
parent 01da4d3e55
commit 3d10dd527c
2 changed files with 78 additions and 68 deletions

View File

@ -69,8 +69,8 @@ function update(frame) {
weights[dir] = 100; weights[dir] = 100;
weights[mod(dir + 2)] = -9999; weights[mod(dir + 2)] = -9999;
for (let nd = 0; nd < 4; nd++) { for (var nd = 0; nd < 4; nd++) {
for (let S = 1; S < 20; S++) { for (var S = 1; S < 20; S++) {
var nr = MOVES[nd][0] * S + row; var nr = MOVES[nd][0] * S + row;
var nc = MOVES[nd][1] * S + col; var nc = MOVES[nd][1] * S + col;
@ -82,7 +82,7 @@ function update(frame) {
if (grid.get(nr, nc) !== user) weights[nd]--; if (grid.get(nr, nc) !== user) weights[nd]--;
var tailed = undefined; var tailed = undefined;
for (let o of others) { for (var o of others) {
if (o.tail.hitsTail(new Loc(nr, nc))) { if (o.tail.hitsTail(new Loc(nr, nc))) {
tailed = o; tailed = o;
break; break;
@ -99,8 +99,8 @@ function update(frame) {
//View a selection of choices based on the weights we computed //View a selection of choices based on the weights we computed
var choices = []; var choices = [];
for (let d = 0; d < 4; d++) { for (var d = 0; d < 4; d++) {
for (let S = 1; S < weights[d]; S++) { for (var S = 1; S < weights[d]; S++) {
choices.push(d); choices.push(d);
} }
} }
@ -135,8 +135,8 @@ function update(frame) {
weights[dir] = 50; weights[dir] = 50;
weights[mod(dir + 2)] = -9999; weights[mod(dir + 2)] = -9999;
for (let nd = 0; nd < 4; nd++) { for (var nd = 0; nd < 4; nd++) {
for (let S = 1; S < 20; S++) { for (var S = 1; S < 20; S++) {
var nr = MOVES[nd][0] * S + row; var nr = MOVES[nd][0] * S + row;
var nc = MOVES[nd][1] * S + col; var nc = MOVES[nd][1] * S + col;
@ -153,7 +153,7 @@ function update(frame) {
if (grid.get(nr, nc) === user) weights[nd] += 10 + S; if (grid.get(nr, nc) === user) weights[nd] += 10 + S;
var tailed = undefined; var tailed = undefined;
for (let o of others) { for (var o of others) {
if (o.tail.hitsTail(new Loc(nr, nc))) { if (o.tail.hitsTail(new Loc(nr, nc))) {
tailed = o; tailed = o;
break; break;
@ -170,8 +170,8 @@ function update(frame) {
//View a selection of choices based on the weights we computed //View a selection of choices based on the weights we computed
var choices = []; var choices = [];
for (let d = 0; d < 4; d++) { for (var d = 0; d < 4; d++) {
for (let S = 1; S < weights[d]; S++) { for (var S = 1; S < weights[d]; S++) {
choices.push(d); choices.push(d);
} }
} }

View File

@ -24,68 +24,78 @@ function hslToRgb(h, s, l) {
} }
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]; return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
} }
function Color(h, s, l, a) {
verifyRange(h, s, l); class Color {
if (a === undefined) a = 1; constructor(h, s, l, a) {
else verifyRange(a); verifyRange(h, s, l);
Object.defineProperties(this, { if (a === undefined) a = 1;
"hue": { else verifyRange(a);
value: h, Object.defineProperties(this, {
enumerable: true "hue": {
}, value: h,
"sat": { enumerable: true
value: s, },
enumerable: true "sat": {
}, value: s,
"lum": { enumerable: true
value: l, },
enumerable: true "lum": {
}, value: l,
"alpha": { enumerable: true
value: a, },
enumerable: true "alpha": {
}, value: a,
}); enumerable: true
},
});
}
interpolateToString(color, amount) {
const rgbThis = hslToRgb(this.hue, this.sat, this.lum);
const rgbThat = hslToRgb(color.hue, color.sat, color.lum);
const rgb = [];
for (let i = 0; i < 3; i++) {
rgb[i] = Math.floor((rgbThat[i] - rgbThis[i]) * amount + rgbThis[i]);
}
return {
rgbString: function() {
return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`
}
};
}
deriveLumination(amount) {
let lum = this.lum + amount;
lum = Math.min(Math.max(lum, 0), 1);
return new Color(this.hue, this.sat, lum, this.alpha);
}
deriveHue(amount) {
const hue = this.hue - amount;
return new Color(hue - Math.floor(hue), this.sat, this.lum, this.alpha);
}
deriveSaturation(amount) {
let sat = this.sat + amount;
sat = Math.min(Math.max(sat, 0), 1);
return new Color(this.hue, sat, this.lum, this.alpha);
}
deriveAlpha(newAlpha) {
verifyRange(newAlpha);
return new Color(this.hue, this.sat, this.lum, newAlpha);
}
rgbString() {
const rgb = hslToRgb(this.hue, this.sat, this.lum);
rgb[3] = this.a;
return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${this.alpha})`;
}
} }
Color.fromData = data => { Color.fromData = data => {
return new Color(data.hue, data.sat, data.lum, data.alpha); return new Color(data.hue, data.sat, data.lum, data.alpha);
}; };
Color.prototype.interpolateToString = function(color, amount) {
const rgbThis = hslToRgb(this.hue, this.sat, this.lum);
const rgbThat = hslToRgb(color.hue, color.sat, color.lum);
const rgb = [];
for (let i = 0; i < 3; i++) {
rgb[i] = Math.floor((rgbThat[i] - rgbThis[i]) * amount + rgbThis[i]);
}
return {
rgbString: function() {
return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`
}
};
};
Color.prototype.deriveLumination = function(amount) {
let lum = this.lum + amount;
lum = Math.min(Math.max(lum, 0), 1);
return new Color(this.hue, this.sat, lum, this.alpha);
};
Color.prototype.deriveHue = function(amount) {
const hue = this.hue - amount;
return new Color(hue - Math.floor(hue), this.sat, this.lum, this.alpha);
};
Color.prototype.deriveSaturation = function(amount) {
let sat = this.sat + amount;
sat = Math.min(Math.max(sat, 0), 1);
return new Color(this.hue, sat, this.lum, this.alpha);
};
Color.prototype.deriveAlpha = function(newAlpha) {
verifyRange(newAlpha);
return new Color(this.hue, this.sat, this.lum, newAlpha);
};
Color.prototype.rgbString = function() {
const rgb = hslToRgb(this.hue, this.sat, this.lum);
rgb[3] = this.a;
return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${this.alpha})`;
};
Color.possColors = () => { Color.possColors = () => {
const SATS = [192, 150, 100].map(val => val / 240); const SATS = [192, 150, 100].map(val => val / 240);
const HUES = [0, 10, 20, 25, 30, 35, 40, 45, 50, 60, 70, 100, 110, 120, 125, 130, 135, 140, 145, 150, 160, 170, 180, 190, 200, 210, 220].map(val => val / 240); const HUES = [0, 10, 20, 25, 30, 35, 40, 45, 50, 60, 70, 100, 110, 120, 125, 130, 135, 140, 145, 150, 160, 170, 180, 190, 200, 210, 220].map(val => val / 240);