hex-to-rgba
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -23,9 +23,20 @@ 'use strict'; | ||
var r = parseInt(fixHex(hex).substring(0, 2), 16); | ||
var g = parseInt(fixHex(hex).substring(2, 4), 16); | ||
var b = parseInt(fixHex(hex).substring(4, 6), 16); | ||
var newHex = fixHex(hex); | ||
var r = parseInt(newHex.substring(0, 2), 16); | ||
var g = parseInt(newHex.substring(2, 4), 16); | ||
var b = parseInt(newHex.substring(4, 6), 16); | ||
return typeof a !== 'undefined' ? 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')' : 'rgb(' + r + ', ' + g + ', ' + b + ')'; | ||
var o = void 0; | ||
if (newHex.length === 8) { | ||
o = +(parseInt(newHex.substring(6, 8), 16) / 255).toFixed(2); | ||
} | ||
o = isNumeric(a) ? a : o; | ||
return isNumeric(o) ? 'rgba(' + r + ', ' + g + ', ' + b + ', ' + o + ')' : 'rgb(' + r + ', ' + g + ', ' + b + ')'; | ||
}; | ||
var isNumeric = function isNumeric(n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
}; | ||
module.exports = hexToRgba; |
{ | ||
"name": "hex-to-rgba", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Converts hexadecimal color codes to rgb()/rgba() values.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -0,1 +1,2 @@ | ||
[![Build Status](https://travis-ci.org/misund/hex-to-rgba.svg?branch=master)](https://travis-ci.org/misund/hex-to-rgba) | ||
@@ -23,2 +24,7 @@ `hex-to-rgba` turns an old-fashioned css hex color value string into an rgb() string. | ||
hexToRgba('#123', 0.2) // "rgba(17, 34, 51, 0.2)" | ||
hexToRgba('11223344') // "rgba(17, 34, 51, 0.27)" | ||
hexToRgba('#11223344') // "rgba(17, 34, 51, 0.27)" | ||
hexToRgba('11223344', '0.5') // "rgba(17, 34, 51, 0.5)" | ||
hexToRgba('#11223344', 0.75) // "rgba(17, 34, 51, 0.75)" | ||
``` | ||
@@ -25,0 +31,0 @@ |
@@ -18,12 +18,22 @@ /** | ||
return newHex; | ||
}; | ||
const r = parseInt((fixHex(hex)).substring(0, 2), 16); | ||
const g = parseInt((fixHex(hex)).substring(2, 4), 16); | ||
const b = parseInt((fixHex(hex)).substring(4, 6), 16); | ||
const newHex = fixHex(hex); | ||
const r = parseInt(newHex.substring(0, 2), 16); | ||
const g = parseInt(newHex.substring(2, 4), 16); | ||
const b = parseInt(newHex.substring(4, 6), 16); | ||
return typeof a !== 'undefined' ? `rgba(${r}, ${g}, ${b}, ${a})` : `rgb(${r}, ${g}, ${b})`; | ||
let o; | ||
if (newHex.length === 8) { | ||
o = +((parseInt(newHex.substring(6, 8), 16)/255).toFixed(2)); | ||
} | ||
o = isNumeric(a) ? a : o; | ||
return isNumeric(o) ? `rgba(${r}, ${g}, ${b}, ${o})` : `rgb(${r}, ${g}, ${b})`; | ||
}; | ||
const isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n); | ||
module.exports = hexToRgba; |
@@ -85,5 +85,5 @@ import assert from 'assert'; | ||
describe.skip('8-digit hex values, no a', function() { | ||
describe('8-digit hex values, no a', function() { | ||
it('should calculate correct rgb values', function() { | ||
assert.equal('rgba(17, 34, 51, 0.27 )', hexToRgba('11223344')); | ||
assert.equal('rgba(17, 34, 51, 0.27)', hexToRgba('11223344')); | ||
}); | ||
@@ -96,3 +96,3 @@ | ||
it('should remove trailing zeros', function() { | ||
assert.equal('rgba(17, 34, 51, 0.5 )', hexToRgba('1122337f')); | ||
assert.equal('rgba(17, 34, 51, 0.5)', hexToRgba('1122337f')); | ||
}); | ||
@@ -110,3 +110,3 @@ | ||
describe.skip('8-digit hex values, a as parameter (separate parameter should override alpha in hex)', function() { | ||
describe('8-digit hex values, a as parameter (separate parameter should override alpha in hex)', function() { | ||
it('should calculate rgba values from hex and string alpha value', function() { | ||
@@ -113,0 +113,0 @@ assert.equal('rgba(17, 34, 51, 0.5)', hexToRgba('11223344', '0.5')); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46569
199
40