Skip to content

Commit

Permalink
better handle alpha values
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jul 5, 2018
1 parent 395b7e4 commit 0b35575
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var rgb2hex = module.exports = function rgb2hex(color) {
/**
* parse input
*/
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);

if(!digits) {
// or throw error if input isn't a valid rgb(a) color
Expand Down
2 changes: 1 addition & 1 deletion rgb2hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* parse input
*/
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,[01]??\.([0-9]{0,3}))??\)/.exec(strippedColor);
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);

if(!digits) {
// or throw error if input isn't a valid rgb(a) color
Expand Down
2 changes: 1 addition & 1 deletion rgb2hex.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion test/rgb2hex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('rgb2hex should', () => {
})

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

Expand All @@ -95,6 +95,8 @@ describe('rgb2hex should', () => {
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
input = 'rgbaaaaaa(113, 54, 4, .33)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
input = 'rgba(12,173,22,1.67)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})
})

Expand Down

0 comments on commit 0b35575

Please sign in to comment.