diff --git a/README.md b/README.md index 176a24c0b..4cd0838cd 100644 --- a/README.md +++ b/README.md @@ -1518,7 +1518,7 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con `Array.slice()` is used to remove `#` from string start since it's added once. ```js const extendHex = shortHex => - '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split().map(x => x+x).join() + '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('') // convertHex('#03f') -> '#0033ff' // convertHex('05a') -> '#0055aa' ``` @@ -1543,14 +1543,18 @@ const getType = v => Converts a colorcode to a `rgb()` string. -Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (prefixed with `#`) to a string with the RGB values. +Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (prefixed with `#`) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. `extendHex` snippet) ```js -const hexToRGB = hex => { - const h = parseInt(hex.slice(1), 16); - return `rgb(${h >> 16}, ${(h & 0x00ff00) >> 8}, ${h & 0x0000ff})`; -} +const hexToRgb = hex => { + const extendHex = shortHex => + '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join(''); + return hex.slice(1).length==3 ? + `rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`: + `rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`; +} // hexToRgb('#27ae60') -> 'rgb(39, 174, 96)' +// hexToRgb('#acd') -> 'rgb(170, 204, 221)' ``` [⬆ back to top](#table-of-contents) diff --git a/docs/index.html b/docs/index.html index 6c7e49ba5..c8bf8ab88 100644 --- a/docs/index.html +++ b/docs/index.html @@ -163,7 +163,7 @@ // arrayMax([10, 1, 5]) -> 10

arrayMin

-

Returns the maximum value in an array.

+

Returns the minimum value in an array.

Use Math.min() combined with the spread operator (...) to get the minimum value in the array.

const arrayMin = arr => Math.min(...arr);
 // arrayMin([10, 1, 5]) -> 1
@@ -919,7 +919,7 @@ Return the string truncated to the desired length, with ... appende
 

Use Array.map(), split() and Array.join() to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form. Array.slice() is used to remove # from string start since it's added once.

const extendHex = shortHex =>
-  '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split().map(x => x+x).join()
+  '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
 // convertHex('#03f') -> '#0033ff'
 // convertHex('05a') -> '#0055aa'
 
@@ -932,12 +932,16 @@ Return the string truncated to the desired length, with ... appende

hexToRGB

Converts a colorcode to a rgb() string.

-

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (prefixed with #) to a string with the RGB values.

-
const hexToRGB = hex => {
-  const h = parseInt(hex.slice(1), 16);
-  return `rgb(${h >> 16}, ${(h & 0x00ff00) >> 8}, ${h & 0x0000ff})`;
-}
+

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (prefixed with #) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. extendHex snippet)

+
const hexToRgb = hex => {
+  const extendHex = shortHex => 
+    '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
+  return hex.slice(1).length==3 ? 
+  `rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
+  `rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
+}   
 // hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
+// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
 

isArray

Checks if the given argument is an array.

diff --git a/snippets/arrayMin.md b/snippets/arrayMin.md index d2a1f3009..5bdd95559 100644 --- a/snippets/arrayMin.md +++ b/snippets/arrayMin.md @@ -1,6 +1,6 @@ ### arrayMin -Returns the maximum value in an array. +Returns the minimum value in an array. Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array.