Skip to content

Commit

Permalink
Merge pull request #45 from crswll/allow-alpha
Browse files Browse the repository at this point in the history
* allow colors with alpha channels but ignore alpha channels
* never use provided alpha channel
* clear up tests
* Release 0.8.0
  • Loading branch information
crswll authored Nov 11, 2023
2 parents 833f0fc + 64de27b commit 4433836
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailwindcss-theme-swapper",
"version": "0.7.3",
"version": "0.8.0",
"main": "src/index.js",
"license": "MIT",
"devDependencies": {
Expand Down
17 changes: 4 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,10 @@ function flatten (
const getTailwindKeyName = keys =>
keys.filter(key => key.toLowerCase() !== 'default').map(kebabCase).join('-')

function hasAlpha (color) {
return (
color.startsWith('rgba(') ||
color.startsWith('hsla(') ||
(color.startsWith('#') && color.length === 9) ||
(color.startsWith('#') && color.length === 5)
)
}

function toRgba (color) {
try {
const [ r, g, b, a ] = Color(color).rgb().array()
return [ r, g, b, a === undefined && hasAlpha(color) ? 1 : a ]
const [ r, g, b ] = Color(color).rgb().array()
return [ r, g, b ]
} catch {
return null
}
Expand All @@ -78,7 +69,7 @@ function toRgba (color) {
function defaultCustomPropValueTransformer (keys, value) {
if (colorConfigKeys.includes(keys[0])) {
const color = toRgba(value)
if (!hasAlpha(value) && color) {
if (color) {
const [ r, g, b ] = color
return `${r} ${g} ${b}`
}
Expand All @@ -93,7 +84,7 @@ function defaultCustomPropValueTransformer (keys, value) {

function defaultConfigValueTransformer (keys, value) {
if (colorConfigKeys.includes(keys[0])) {
if (!hasAlpha(value) && toRgba(value)) {
if (toRgba(value)) {
return tailwindVariableHelper(getTailwindKeyName(keys))
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ expect.extend({
const defaultTheme = {
colors: {
hotpink: 'hotpink',
opacity: 'rgba(255, 0, 0, 0.5)',
'with-opacity-ignored': 'rgba(255, 0, 0, 0.5)',
primary: {
default: '#f00',
darker: '#400',
Expand Down Expand Up @@ -70,7 +70,7 @@ postcss(tailwindcss({
expect(resolvedConfig).toMatchObject({
"theme": {
"colors": {
"opacity": "var(--colors-opacity, rgba(255, 0, 0, 0.5))",
"with-opacity-ignored": expect.any(Function),
"hotpink": expect.any(Function),
},
"spacing": {
Expand All @@ -87,7 +87,7 @@ postcss(tailwindcss({
const sampleConfigOutput = `
:root, .light {
--colors-hotpink: 255 105 180;
--colors-opacity: rgba(255, 0, 0, 0.5);
--colors-with-opacity-ignored: 255 0 0;
--colors-primary: 255 0 0;
--colors-primary-darker: 68 0 0;
--spacing-fart: 69px;
Expand Down
16 changes: 8 additions & 8 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ describe('getThemeAsCustomProps', () => {

describe('toRgba', () => {
test('should return an array of rgba', () => {
expect(toRgba('#fff')).toEqual([255, 255, 255, undefined])
expect(toRgba('#ffff')).toEqual([255, 255, 255, 1])
expect(toRgba('#fff0')).toEqual([255, 255, 255, 0])
expect(toRgba('hotpink')).toEqual([255, 105, 180, undefined])
expect(toRgba('rgb(255, 0, 0)')).toEqual([255, 0, 0, undefined])
expect(toRgba('rgba(255, 0, 0, 0.5)')).toEqual([255, 0, 0, 0.5])
expect(toRgba('hsl(0, 100%, 50%)')).toEqual([255, 0, 0, undefined])
expect(toRgba('hsl(0, 100%, 50%, 0.5)')).toEqual([255, 0, 0, 0.5])
expect(toRgba('#fff')).toEqual([255, 255, 255])
expect(toRgba('#ffff')).toEqual([255, 255, 255])
expect(toRgba('#fff0')).toEqual([255, 255, 255])
expect(toRgba('hotpink')).toEqual([255, 105, 180])
expect(toRgba('rgb(255, 0, 0)')).toEqual([255, 0, 0])
expect(toRgba('rgba(255, 0, 0, 0.5)')).toEqual([255, 0, 0])
expect(toRgba('hsl(0, 100%, 50%)')).toEqual([255, 0, 0])
expect(toRgba('hsl(0, 100%, 50%, 0.5)')).toEqual([255, 0, 0])
expect(toRgba('__DEFINITELY_NOT_A_COLOR_NO_WAY_NO_HOW__')).toEqual(null)
})
})
Expand Down

0 comments on commit 4433836

Please sign in to comment.