Last active
July 27, 2023 19:33
-
-
Save danmatthews/f6ea70c3a1ef6348b38a5bd79ac1f6dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add this to your 'plugins' array in your tailwind config file. | |
// This creates classes suffixed with an opacity level for each bg colour | |
// For example, .bg-red would have .bg-red-10 through .bg-red-100 for 0.10% opacity background and 100% opacity respectively. | |
({addUtilities, config}) => { | |
let colors = config('colors', []); | |
const newColors = {}; | |
let hexToRgb = (hex) => { | |
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | |
return result ? { | |
r: parseInt(result[1], 16), | |
g: parseInt(result[2], 16), | |
b: parseInt(result[3], 16) | |
} : null; | |
} | |
for ( i in colors) { | |
let c = hexToRgb(colors[i]); | |
if (c) { | |
let nms = { | |
'.1':'10', | |
'.2':'20', | |
'.3':'30', | |
'.4':'40', | |
'.5':'50', | |
'.6':'60', | |
'.7':'70', | |
'.8':'80', | |
'.9':'90', | |
'1.0':'100' | |
}; | |
for (o in nms) { | |
newColors[`.bg-${i}-${nms[o]}`] = { | |
'background-color': `rgba(${c.r},${c.g},${c.b}, ${o})` | |
}; | |
} | |
} | |
} | |
addUtilities(newColors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improves, add text color, background color, custom properties, and accept params.