|
| 1 | +/** |
| 2 | + * tailwindcss-palette-generator v0.3.0 |
| 3 | + * Copyright 2023 @ibodev1 <github.com/ibodev1> |
| 4 | + */ |
| 5 | + |
| 6 | +import chroma from 'chroma-js'; |
| 7 | + |
| 8 | +const darkenValue = (shade, mainShade) => { |
| 9 | + return (shade - mainShade) / 100 / 2; |
| 10 | +}; |
| 11 | +const shadeColor = (primaryColor, mainShade, shade) => { |
| 12 | + return chroma(primaryColor) |
| 13 | + .darken(darkenValue(shade, mainShade)) |
| 14 | + .hex(); |
| 15 | +}; |
| 16 | +const shadeColorResult = (fn, options) => { |
| 17 | + return options.shades.reduce((acc, shade) => { |
| 18 | + acc[shade] = fn(options.primaryColor, options.mainShade, shade); |
| 19 | + return acc; |
| 20 | + }, {}); |
| 21 | +}; |
| 22 | +const colorResult = (options) => { |
| 23 | + const palette = shadeColorResult(shadeColor, options); |
| 24 | + const colorPalette = { |
| 25 | + ...palette, |
| 26 | + DEFAULT: options.primaryColor |
| 27 | + }; |
| 28 | + return Object.freeze(colorPalette) ?? {}; |
| 29 | +}; |
| 30 | + |
| 31 | +const initalOptions = { |
| 32 | + mainShade: 500, |
| 33 | + primaryColor: "#FFBD00", |
| 34 | + shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900] |
| 35 | +}; |
| 36 | +const isColor = (color) => { |
| 37 | + const reg = /^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/gim; |
| 38 | + if (typeof color === "string" && reg.test(color)) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + else { |
| 42 | + return false; |
| 43 | + } |
| 44 | +}; |
| 45 | +const checkParam = (palette) => { |
| 46 | + if (palette.color && |
| 47 | + typeof palette.color === "string" && |
| 48 | + palette.name && |
| 49 | + typeof palette.name == "string") { |
| 50 | + if (!isColor(palette.color)) { |
| 51 | + throw new Error(`'${palette.color}' The value you entered is not a color. e.g #ffbd00 or #ffb or rgba(255, 189, 0, 1) or rgb(255, 189, 0) or hsl(44, 100%, 50%)`); |
| 52 | + } |
| 53 | + else if (!palette.shade && palette.shades) { |
| 54 | + throw new Error(`If you want to specify the shades, you have to specify the main shade.`); |
| 55 | + } |
| 56 | + else if (palette.shade && typeof palette.shade !== "number") { |
| 57 | + throw new Error(`'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.`); |
| 58 | + } |
| 59 | + else if (palette.shades && |
| 60 | + !Array.isArray(palette.shades)) { |
| 61 | + throw new Error(`Shades are not array.`); |
| 62 | + } |
| 63 | + else if (palette.shades && palette.shades.length <= 2) { |
| 64 | + throw new Error(`Shades can consist of at least 3 elements.`); |
| 65 | + } |
| 66 | + else if (palette.shade && |
| 67 | + palette.shades && |
| 68 | + !palette.shades.includes(palette.shade)) { |
| 69 | + throw new Error(`'${palette.shade}' mainShade are not included in the your shades.`); |
| 70 | + } |
| 71 | + else if (!palette.shades && |
| 72 | + palette.shade && |
| 73 | + !initalOptions.shades.includes(palette.shade)) { |
| 74 | + throw new Error(`'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.`); |
| 75 | + } |
| 76 | + else { |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + else { |
| 81 | + throw new Error("Make sure the required data is included."); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +const getPalette = (params) => { |
| 86 | + let palette = {}; |
| 87 | + if (Array.isArray(params)) { |
| 88 | + for (let i = 0; i < params.length; i++) { |
| 89 | + const colorPalette = params[i]; |
| 90 | + if (checkParam(colorPalette)) { |
| 91 | + const options = { |
| 92 | + mainShade: colorPalette.shade ?? initalOptions.mainShade, |
| 93 | + primaryColor: chroma(colorPalette.color).hex() ?? initalOptions.primaryColor, |
| 94 | + shades: colorPalette.shades ?? initalOptions.shades |
| 95 | + }; |
| 96 | + palette[colorPalette.name] = colorResult(options); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + else if (typeof params !== "string" && !Array.isArray(params)) { |
| 101 | + if (checkParam(params)) { |
| 102 | + const options = { |
| 103 | + mainShade: params.shade ?? initalOptions.mainShade, |
| 104 | + primaryColor: chroma(params.color).hex() ?? initalOptions.primaryColor, |
| 105 | + shades: params.shades ?? initalOptions.shades |
| 106 | + }; |
| 107 | + palette[params.name] = colorResult(options); |
| 108 | + } |
| 109 | + } |
| 110 | + else if (typeof params === "string") { |
| 111 | + const options = Object.assign(initalOptions, { |
| 112 | + primaryColor: chroma(params).hex() |
| 113 | + }); |
| 114 | + palette["primary"] = colorResult(options); |
| 115 | + } |
| 116 | + return palette; |
| 117 | +}; |
| 118 | + |
| 119 | +export { getPalette as default }; |
0 commit comments