Skip to content

Commit

Permalink
allow transparent values
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jul 18, 2018
1 parent 2b1727f commit a1bebe1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 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})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|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})(,(1|0??\.([0-9]{0,3})))??\)/.exec(strippedColor);
var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|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.

24 changes: 15 additions & 9 deletions test/rgb2hex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ const invalidErrorMessage = (input) => 'given color (' + input + ') isn\'t a val
describe('rgb2hex should', () => {
describe('throw an error if input is not typeof string', () => {
it('[Object] {color: \'something\'}', () => {
var input = {color: 'something'}
const input = {color: 'something'}
expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
})

it('[Function] function(){}', () => {
var input = function(){}
const input = function(){}
expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
})

it('[Number] 231', () => {
var input = 231
const input = 231
expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
})
})

describe('throw an error if input is invalid', () => {
it('notacolor', () => {
var input = 'notacolor'
const input = 'notacolor'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})

it('rgba(100, 100)', () => {
var input = 'rgb(100, 100)'
const input = 'rgb(100, 100)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})

it('rgba(100, 10a0, 200, 300)', () => {
var input = 'rgba(100, 10a0, 200, 300)'
const input = 'rgba(100, 10a0, 200, 300)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})

it('rgba(23, 54, 4, -.33)', () => {
var input = 'rgba(23, 54, 4, -.33)'
const input = 'rgba(23, 54, 4, -.33)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})
})
Expand Down Expand Up @@ -80,12 +80,12 @@ describe('rgb2hex should', () => {
})

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

it('by not accepting to big values', () => {
var input = 'rgba(1123, 54, 4, 0.33)'
let input = 'rgba(1123, 54, 4, 0.33)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
input = 'rgba(113, 1154, 4, 0.33)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
Expand All @@ -98,6 +98,12 @@ describe('rgb2hex should', () => {
input = 'rgba(12,173,22,1.67)'
expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
})

it('transparent color', () => {
const input = 'rgba(0, 0, 0, 0)'
expect(rgb2hex(input).alpha).toBe(0)
expect(rgb2hex(input).hex).toBe('#000000')
})
})

describe('not care about', () => {
Expand Down

0 comments on commit a1bebe1

Please sign in to comment.