@videsk/tailwind-random-color
Version:
Generate random tailwind colors with a custom palette range
2 lines (1 loc) • 950 B
JavaScript
!function(o,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(o="undefined"!=typeof globalThis?globalThis:o||self).TailwindColor=r()}(this,(function(){return class{constructor(o){const{colors:r=["gray","red","yellow","green","blue","indigo","purple","pink"],range:t=[1,9],prefix:s="bg"}=o||{};this.colors=r,this.range={min:t[0],max:t[1]},this.prefix=s,this.tempColors=[]}pick(){const o=100*this.constructor.random(this.range.min,this.range.max),r=this.constructor.random(0,this.colors.length-1);return`${this.prefix}-${this.colors[r]}-${o}`}color(o=""){return Array.isArray(o)?o.forEach((o=>this.tempColors.push(o))):this.tempColors.push(o),this}add(){this.tempColors.forEach((o=>this.colors.push(o)))}remove(){this.tempColors.forEach((o=>{const r=this.colors.indexOf(o);r>=0&&this.colors.splice(r,1)}))}static random(o=1,r=9){return Math.floor(Math.random()*(r-o+1))+o}}}));