Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for hex values, rgba strings, malformed values with fail… #419

Merged
merged 9 commits into from
Nov 8, 2018
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