Skip to content

Commit 3b2263a

Browse files
committed
Support 3-character hex, fixes #1
1 parent 8b60f4a commit 3b2263a

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

dist/coreui-utils.common.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/coreui-utils.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hex-to-rgb.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const hexToRgb = color => {
1919
g = parseInt(color.slice(3, 5), 16)
2020
b = parseInt(color.slice(5, 7), 16)
2121
} else {
22-
r = parseInt(color.slice(1, 2), 16)
23-
g = parseInt(color.slice(2, 3), 16)
24-
b = parseInt(color.slice(3, 5), 16)
22+
// Convert 3-digit hex (eg #abc) to 6-digit (#aabbcc) for valid rgb code
23+
r = parseInt(color.slice(1, 2).repeat(2), 16)
24+
g = parseInt(color.slice(2, 3).repeat(2), 16)
25+
b = parseInt(color.slice(3, 4).repeat(2), 16)
2526
}
2627

2728
return `rgba(${r}, ${g}, ${b})`

src/hex-to-rgba.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const hexToRgba = (color, opacity = 100) => {
1919
g = parseInt(color.slice(3, 5), 16)
2020
b = parseInt(color.slice(5, 7), 16)
2121
} else {
22-
r = parseInt(color.slice(1, 2), 16)
23-
g = parseInt(color.slice(2, 3), 16)
24-
b = parseInt(color.slice(3, 5), 16)
22+
// Convert 3-digit hex (eg #abc) to 6-digit (#aabbcc) for valid rgb code
23+
r = parseInt(color.slice(1, 2).repeat(2), 16)
24+
g = parseInt(color.slice(2, 3).repeat(2), 16)
25+
b = parseInt(color.slice(3, 4).repeat(2), 16)
2526
}
2627

2728
return `rgba(${r}, ${g}, ${b}, ${opacity / 100})`

0 commit comments

Comments
 (0)