UNPKG

@tsparticles/plugin-rgb-color

Version:

tsParticles RGB color plugin

47 lines (46 loc) 1.72 kB
import { getRangeValue, parseAlpha, } from "@tsparticles/engine"; var RgbIndexes; (function (RgbIndexes) { RgbIndexes[RgbIndexes["r"] = 1] = "r"; RgbIndexes[RgbIndexes["g"] = 2] = "g"; RgbIndexes[RgbIndexes["b"] = 3] = "b"; RgbIndexes[RgbIndexes["a"] = 5] = "a"; })(RgbIndexes || (RgbIndexes = {})); const rgbRegex = /rgba?\(\s*(\d{1,3})\s*[\s,]\s*(\d{1,3})\s*[\s,]\s*(\d{1,3})\s*([\s,]\s*(0|1|0?\.\d+|(\d{1,3})%)\s*)?\)/i; export class RgbColorManager { accepts(input) { return input.startsWith("rgb"); } handleColor(color) { const colorValue = color.value, rgbColor = colorValue.rgb ?? color.value; if (!("r" in rgbColor) || !("g" in rgbColor) || !("b" in rgbColor)) { return; } return rgbColor; } handleRangeColor(color) { const colorValue = color.value, rgbColor = colorValue.rgb ?? color.value; if (!("r" in rgbColor) || !("g" in rgbColor) || !("b" in rgbColor)) { return; } return { r: getRangeValue(rgbColor.r), g: getRangeValue(rgbColor.g), b: getRangeValue(rgbColor.b), }; } parseString(input) { if (!this.accepts(input)) { return; } const result = rgbRegex.exec(input), radix = 10, minLength = 4, defaultAlpha = 1; return result ? { a: result.length > minLength ? parseAlpha(result[RgbIndexes.a]) : defaultAlpha, b: parseInt(result[RgbIndexes.b] ?? "0", radix), g: parseInt(result[RgbIndexes.g] ?? "0", radix), r: parseInt(result[RgbIndexes.r] ?? "0", radix), } : undefined; } }