-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from Lucassifoni/master
Adds support for hex values, rgba strings, malformed values with fail…
- Loading branch information
Showing
4 changed files
with
620 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.