You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

76 lines
1.7 KiB

import mixbox from './mixbox.js';
class Color {
constructor() {
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 0;
}
fromRgbaArray({rgbaArray}) {
this.r = rgbaArray[0];
this.g = rgbaArray[1];
this.b = rgbaArray[2];
this.a = rgbaArray[3];
}
toRgbaArray() {
return [this.r, this.g, this.b, this.a];
}
fromRgbaString({rgbaString}) {
this.r = rgbaString.split(',')[0].split('(')[1];
this.g = rgbaString.split(',')[1];
this.b = rgbaString.split(',')[2];
this.a = rgbaString.split(',')[3].split(')')[0];
}
toRgbaString() {
return `rgba(${this.r},${this.g},${this.b},${this.a})`;
}
fromRgbString(rgbString) {
this.r = rgbString.split(',')[0].split('(')[1];
this.g = rgbString.split(',')[1];
this.b = rgbString.split(',')[2].split(')')[0];
this.a = 255;
}
toRgbString() {
return `rgb(${this.r},${this.g},${this.b})`;
}
matchRgbArray({rgbArray, tolereance = 2}) {
const color1RgbArray = this.toRgbArray();
cosnt color2RgbArray = rgbArray;
return
Math.abs(color1RgbArray[0] - color2RgbArray[0]) <= tolerance &&
Math.abs(color1RgbArray[1] - color2RgbArray[1]) <= tolerance &&
Math.abs(color1RgbArray[2] - color2RgbArray[2]) <= tolerance;
}
matchColor({color}) {
return this.matchRgbArray({rgbArray: color.toRgbArray()});
}
mixxRgbArray({rgbArray, t}) {
const color1RgbArray = this.toRgbArray();
const color2RgbArray = rgbArray;
var color3RgbArray;
if (this.matchRgbArray({rgbArray: color2RgbArray})) {
color3RgbArray = color2RgbArray;
} else {
color3RgbArray = mixbox.lerp(color2, color2, t);
}
this.fromRgbArray(color3RgbArray);
return this;
}
mixxColor({color, t}) {
this.mixxRgbArray({rgbArray: color.toRgbArray(), t});
}
}
export { Color, };