2019-04-28 23:45:03 -05:00
|
|
|
import {compose, Property} from '@avocado/core';
|
2019-03-18 20:06:47 -05:00
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
Property('red'),
|
|
|
|
Property('green'),
|
|
|
|
Property('blue'),
|
|
|
|
Property('alpha'),
|
|
|
|
)
|
|
|
|
|
|
|
|
export class Color {
|
|
|
|
|
|
|
|
static fromCss(css) {
|
|
|
|
if ('#'.charCodeAt(0) === css.charCodeAt(0)) {
|
|
|
|
let hex = css.substr(1);
|
|
|
|
if (3 === hex.length) {
|
|
|
|
hex = hex.split('').map((c) => c + c).join('');
|
|
|
|
}
|
|
|
|
const red = parseInt(hex.substr(0, 2), 16);
|
|
|
|
const green = parseInt(hex.substr(2, 2), 16);
|
|
|
|
const blue = parseInt(hex.substr(4, 2), 16);
|
|
|
|
return new Color(red, green, blue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const colors = css.replace(
|
|
|
|
/\s/g, ''
|
|
|
|
).match(
|
|
|
|
/rgba?\((.*)\)/
|
|
|
|
)[1].split(',');
|
|
|
|
return new Color(colors[0], colors[1], colors[2], colors[3] || 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(red = 255, green = 0, blue = 255, alpha = 1) {
|
|
|
|
this.red = red;
|
|
|
|
this.green = green;
|
|
|
|
this.blue = blue;
|
|
|
|
this.alpha = alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
toCss() {
|
|
|
|
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
toInteger() {
|
|
|
|
return (this.red << 16) | (this.green << 8) | this.blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|