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,7 +24,9 @@ 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) {
class Color {
constructor(h, s, l, a) {
verifyRange(h, s, l); verifyRange(h, s, l);
if (a === undefined) a = 1; if (a === undefined) a = 1;
else verifyRange(a); else verifyRange(a);
@ -46,11 +48,9 @@ function Color(h, s, l, a) {
enumerable: true enumerable: true
}, },
}); });
} }
Color.fromData = data => {
return new Color(data.hue, data.sat, data.lum, data.alpha); interpolateToString(color, amount) {
};
Color.prototype.interpolateToString = function(color, amount) {
const rgbThis = hslToRgb(this.hue, this.sat, this.lum); const rgbThis = hslToRgb(this.hue, this.sat, this.lum);
const rgbThat = hslToRgb(color.hue, color.sat, color.lum); const rgbThat = hslToRgb(color.hue, color.sat, color.lum);
const rgb = []; const rgb = [];
@ -62,29 +62,39 @@ Color.prototype.interpolateToString = function(color, amount) {
return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})` return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`
} }
}; };
}; }
Color.prototype.deriveLumination = function(amount) {
deriveLumination(amount) {
let lum = this.lum + amount; let lum = this.lum + amount;
lum = Math.min(Math.max(lum, 0), 1); lum = Math.min(Math.max(lum, 0), 1);
return new Color(this.hue, this.sat, lum, this.alpha); return new Color(this.hue, this.sat, lum, this.alpha);
}; }
Color.prototype.deriveHue = function(amount) {
deriveHue(amount) {
const hue = this.hue - amount; const hue = this.hue - amount;
return new Color(hue - Math.floor(hue), this.sat, this.lum, this.alpha); return new Color(hue - Math.floor(hue), this.sat, this.lum, this.alpha);
}; }
Color.prototype.deriveSaturation = function(amount) {
deriveSaturation(amount) {
let sat = this.sat + amount; let sat = this.sat + amount;
sat = Math.min(Math.max(sat, 0), 1); sat = Math.min(Math.max(sat, 0), 1);
return new Color(this.hue, sat, this.lum, this.alpha); return new Color(this.hue, sat, this.lum, this.alpha);
}; }
Color.prototype.deriveAlpha = function(newAlpha) {
deriveAlpha(newAlpha) {
verifyRange(newAlpha); verifyRange(newAlpha);
return new Color(this.hue, this.sat, this.lum, newAlpha); return new Color(this.hue, this.sat, this.lum, newAlpha);
}; }
Color.prototype.rgbString = function() {
rgbString() {
const rgb = hslToRgb(this.hue, this.sat, this.lum); const rgb = hslToRgb(this.hue, this.sat, this.lum);
rgb[3] = this.a; rgb[3] = this.a;
return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${this.alpha})`; return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${this.alpha})`;
}
}
Color.fromData = data => {
return new Color(data.hue, data.sat, data.lum, data.alpha);
}; };
Color.possColors = () => { Color.possColors = () => {
const SATS = [192, 150, 100].map(val => val / 240); const SATS = [192, 150, 100].map(val => val / 240);