Skip to content

Commit 0b35575

Browse files
better handle alpha values
1 parent 395b7e4 commit 0b35575

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var rgb2hex = module.exports = function rgb2hex(color) {
2828
/**
2929
* parse input
3030
*/
31-
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
31+
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
3232

3333
if(!digits) {
3434
// or throw error if input isn't a valid rgb(a) color

rgb2hex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* parse input
2323
*/
24-
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
24+
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
2525

2626
if(!digits) {
2727
// or throw error if input isn't a valid rgb(a) color

rgb2hex.min.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.

test/rgb2hex.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('rgb2hex should', () => {
8080
})
8181

8282
it('by limiting alpha value to 1', () => {
83-
var input = 'rgba(12,173,22,1.67)'
83+
var input = 'rgba(236,68,44,1)'
8484
expect(rgb2hex(input).alpha).not.toBeGreaterThan(1)
8585
})
8686

@@ -95,6 +95,8 @@ describe('rgb2hex should', () => {
9595
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
9696
input = 'rgbaaaaaa(113, 54, 4, .33)'
9797
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
98+
input = 'rgba(12,173,22,1.67)'
99+
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
98100
})
99101
})
100102

0 commit comments

Comments
 (0)