fantasy/painting/color.js

45 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-11-18 18:04:50 +00:00
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;
2018-11-18 18:09:30 +00:00
}
2018-11-18 18:04:50 +00:00
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;
2018-11-18 18:09:30 +00:00
}
2018-11-18 18:04:50 +00:00
static random() {
return new Color(Math.random(), Math.random(), Math.random(), Math.random());
}
2018-11-20 14:30:01 +00:00
static randomGray() {
let intensiviry = Math.random();
return new Color(intensiviry, intensiviry, intensiviry, Math.random());
}
2018-11-18 18:04:50 +00:00
}
2018-11-18 18:15:26 +00:00
export default Color;