Skip to content

Commit

Permalink
Merge pull request #419 from Lucassifoni/master
Browse files Browse the repository at this point in the history
Adds support for hex values, rgba strings, malformed values with fail…
  • Loading branch information
peterramsing authored Nov 8, 2018
2 parents 731a934 + 424b9b4 commit ec67f1d
Show file tree
Hide file tree
Showing 4 changed files with 620 additions and 450 deletions.
101 changes: 101 additions & 0 deletions lib/_lu-utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const testRgb = new RegExp(/rgb/);
//eslint-disable-next-line
const matchRgb = new RegExp(/rgba?\(([^\)]+)\)/);
const testHex = new RegExp(/#\w+/);

/**
* Returns a three-member number array from a hex string
* @param hex
* @returns {number[]}
*/
const safeHexToRgb = function safeHexToRgb(hex) {
const value = hex.trim().split('#');
var c = ['00', '00', '00'];
if (value.length === 1) {
c = value[0].split('');
}
if (value.length === 2) {
c = value[1].split('');
}
const l = c.length;
if (l === 3) return [hToD(c[0]), hToD(c[1]), hToD(c[2])];
if (l === 4) return [hToD(c[0]), hToD(c[1]), hToD(c[2])];
if (l === 6) return [hToD(c[0], c[1]), hToD(c[2], c[3]), hToD(c[4], c[5])];
if (l === 8) return [hToD(c[0], c[1]), hToD(c[2], c[3]), hToD(c[4], c[5])];
return [0, 0, 255];
};

/**
* Returns a sanitized three-number array representing RGB color values, with a safe default.
* @param string
* @returns {number[]}
*/
const getColorValue = function getColorValue(string) {
if (testRgb.test(string)) {
return safeRgbToRgb(string);
}
if (testHex.test(string)) {
return safeHexToRgb(string);
}
return [0, 0, 255];
};

/**
* Extracts the comma-separated numbers from a rgb(a?) string.
* @param string
* @returns {string}
*/
const extractRgbSubstring = function extractRgbSubstring(string) {
var candidate = string.match(matchRgb);
if (
candidate &&
candidate.length > 1 &&
candidate[1].length > 0 &&
typeof candidate[1] === 'string'
) {
return candidate[1];
}
return '0,0,255';
};

/**
* Returns a base10 number from one or two hex digits
* @param h
* @returns {number}
*/
const hToD = function hToD(...h) {
var hh = '00';
if (h.length === 1) {
hh = '' + h[0] + h[0];
} else if (h.length === 2) {
hh = '' + h[0] + h[1];
}
var d = parseInt(hh, 16);
return !isNaN(d) ? d : 0;
};

/**
* Returns a three-member number array from a rgb string
* @param rgb
* @returns {number[]}
*/
const safeRgbToRgb = function safeRgbToRgb(rgb) {
var value = extractRgbSubstring(rgb)
.split(',')
.map(function(a) {
var b = parseInt(a, 10);
return !isNaN(b) ? b : 0;
});
if (value.length >= 3) {
return [value[0], value[1], value[2]];
}
return [0, 0, 255];
};

module.exports = {
getColorValue,
extractRgbSubstring,
hToD,
safeRgbToRgb,
safeHexToRgb
};
9 changes: 2 additions & 7 deletions lib/lost-utility.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
var newBlock = require('./new-block.js');

function getColorValue(string) {
var color = string.split('rgb(')[1];
color = color.split(')')[0];
return color;
}
var getColorValue = require('./_lu-utilities').getColorValue;

function unitsMatch() {
var args = Array.prototype.slice.call(arguments, 0);
Expand Down Expand Up @@ -34,7 +29,7 @@ module.exports = function lostUtilityDecl(css) {
decl,
' *:not(input):not(textarea):not(select)',
['background-color'],
['rgba(' + color + ', 0.1)']
['rgba(' + color[0] + ',' + color[1] + ',' + color[2] + ', 0.1)']
);
} else {
newBlock(
Expand Down
Loading

0 comments on commit ec67f1d

Please sign in to comment.