{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import { rgbToHex, rgbToHsl } from \"./converter\";\n\nexport * from \"./converter\";\n\n/**\n * @returns `true` if the color is to be kept.\n */\nexport interface Filter {\n\t(red: number, green: number, blue: number, alpha: number): boolean;\n}\n\n/**\n * 3d floating pointer vector\n */\nexport type Vec3 = [number, number, number];\n\n/**\n * The layout for a node-vibrant Palette. Allows you to keep track of\n */\nexport interface Palette {\n\tVibrant: Swatch | null;\n\tMuted: Swatch | null;\n\tDarkVibrant: Swatch | null;\n\tDarkMuted: Swatch | null;\n\tLightVibrant: Swatch | null;\n\tLightMuted: Swatch | null;\n\t// ?\n\t[name: string]: Swatch | null;\n}\n\n/**\n * Represents a color swatch generated from an image's palette.\n */\nexport class Swatch {\n\tstatic applyFilters(colors: Swatch[], filters: Filter[]): Swatch[] {\n\t\treturn filters.length > 0\n\t\t\t? colors.filter(({ r, g, b }) => {\n\t\t\t\t\tfor (let j = 0; j < filters.length; j++) {\n\t\t\t\t\t\tif (!filters[j]?.(r, g, b, 255)) return false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t})\n\t\t\t: colors;\n\t}\n\n\t/**\n\t * Make a value copy of a swatch based on a previous one. Returns a new Swatch instance\n\t * @param {Swatch} swatch\n\t */\n\tstatic clone(swatch: Swatch) {\n\t\treturn new Swatch(swatch._rgb, swatch._population);\n\t}\n\tprivate _rgb: Vec3;\n\tprivate _population: number;\n\tprivate _hsl: Vec3 | undefined;\n\tprivate _yiq: number | undefined;\n\tprivate _hex: string | undefined;\n\n\t/**\n\t * The red value in the RGB value\n\t */\n\tget r(): number {\n\t\treturn this._rgb[0];\n\t}\n\t/**\n\t * The green value in the RGB value\n\t */\n\tget g(): number {\n\t\treturn this._rgb[1];\n\t}\n\t/**\n\t * The blue value in the RGB value\n\t */\n\tget b(): number {\n\t\treturn this._rgb[2];\n\t}\n\t/**\n\t * The color value as a rgb value\n\t */\n\tget rgb(): Vec3 {\n\t\treturn this._rgb;\n\t}\n\t/**\n\t * The color value as a hsl value\n\t */\n\tget hsl(): Vec3 {\n\t\tif (!this._hsl) {\n\t\t\tconst [r, g, b] = this._rgb;\n\t\t\tthis._hsl = rgbToHsl(r, g, b);\n\t\t}\n\t\treturn this._hsl;\n\t}\n\n\t/**\n\t * The color value as a hex string\n\t */\n\tget hex(): string {\n\t\tif (!this._hex) {\n\t\t\tconst [r, g, b] = this._rgb;\n\t\t\tthis._hex = rgbToHex(r, g, b);\n\t\t}\n\t\treturn this._hex;\n\t}\n\n\tget population(): number {\n\t\treturn this._population;\n\t}\n\n\t/**\n\t * Get the JSON object for the swatch\n\t */\n\ttoJSON(): { rgb: Vec3; population: number } {\n\t\treturn {\n\t\t\trgb: this.rgb,\n\t\t\tpopulation: this.population,\n\t\t};\n\t}\n\n\tprivate getYiq(): number {\n\t\tif (!this._yiq) {\n\t\t\tconst rgb = this._rgb;\n\t\t\tthis._yiq = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;\n\t\t}\n\t\treturn this._yiq;\n\t}\n\n\tprivate _titleTextColor: string | undefined;\n\tprivate _bodyTextColor: string | undefined;\n\n\t/**\n\t * Returns an appropriate color to use for any 'title' text which is displayed over this Swatch's color.\n\t */\n\tget titleTextColor() {\n\t\tif (!this._titleTextColor) {\n\t\t\tthis._titleTextColor = this.getYiq() < 200 ? \"#fff\" : \"#000\";\n\t\t}\n\t\treturn this._titleTextColor;\n\t}\n\n\t/**\n\t * Returns an appropriate color to use for any 'body' text which is displayed over this Swatch's color.\n\t */\n\tget bodyTextColor() {\n\t\tif (!this._bodyTextColor) {\n\t\t\tthis._bodyTextColor = this.getYiq() < 150 ? \"#fff\" : \"#000\";\n\t\t}\n\t\treturn this._bodyTextColor;\n\t}\n\n\t/**\n\t * Internal use.\n\t * @param rgb `[r, g, b]`\n\t * @param population Population of the color in an image\n\t */\n\tconstructor(rgb: Vec3, population: number) {\n\t\tthis._rgb = rgb;\n\t\tthis._population = population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