fantasy/painting/color.js

45 lines
1.0 KiB
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());
}
static randomGray() {
let intensiviry = Math.random();
return new Color(intensiviry, intensiviry, intensiviry, Math.random());
}
}
export default Color;