fantasy/painting/color.js
2018-11-18 21:15:26 +03:00

41 lines
885 B
JavaScript

class Color {
constructor(r, g, b, a) {
r = Math.max(0, r);
r = Math.min(1, r);
g = Math.max(0, g);
g = Math.min(1, g);
b = Math.max(0, b);
b = Math.min(1, b);
if (a !== 0) {
a = a || 1;
}
a = Math.max(0, a);
a = Math.min(1, a);
this._r = r;
this._g = g;
this._b = b;
this._a = a;
}
rgba() {
let out = "rgba(";
out += Math.floor(this._r * 255);
out += ", ";
out += Math.floor(this._g * 255);
out += ", ";
out += Math.floor(this._b * 255);
out += ", ";
out += this._a;
out += ")";
return out;
}
static random() {
return new Color(Math.random(), Math.random(), Math.random(), Math.random());
}
}
export default Color;