Skip to content

Commit 16a7e35

Browse files
author
David Wu
committed
2 parents 3ec7dd4 + 7b93f5a commit 16a7e35

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con
15181518
`Array.slice()` is used to remove `#` from string start since it's added once.
15191519
```js
15201520
const extendHex = shortHex =>
1521-
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split().map(x => x+x).join()
1521+
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
15221522
// convertHex('#03f') -> '#0033ff'
15231523
// convertHex('05a') -> '#0055aa'
15241524
```
@@ -1543,14 +1543,18 @@ const getType = v =>
15431543

15441544
Converts a colorcode to a `rgb()` string.
15451545

1546-
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.
1546+
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)
15471547

15481548
```js
1549-
const hexToRGB = hex => {
1550-
const h = parseInt(hex.slice(1), 16);
1551-
return `rgb(${h >> 16}, ${(h & 0x00ff00) >> 8}, ${h & 0x0000ff})`;
1552-
}
1549+
const hexToRgb = hex => {
1550+
const extendHex = shortHex =>
1551+
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
1552+
return hex.slice(1).length==3 ?
1553+
`rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
1554+
`rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
1555+
}
15531556
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
1557+
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
15541558
```
15551559

15561560
[⬆ back to top](#table-of-contents)

docs/index.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ <h3>Utility
163163
// arrayMax([10, 1, 5]) -&gt; 10
164164
</code></pre>
165165
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraymin">arrayMin</h3></div><div class="section double-padded">
166-
<p>Returns the maximum value in an array.</p>
166+
<p>Returns the minimum value in an array.</p>
167167
<p>Use <code>Math.min()</code> combined with the spread operator (<code>...</code>) to get the minimum value in the array.</p>
168168
<pre><code class="language-js">const arrayMin = arr =&gt; Math.min(...arr);
169169
// arrayMin([10, 1, 5]) -&gt; 1
@@ -919,7 +919,7 @@ <h3>Utility
919919
<p>Use <code>Array.map()</code>, <code>split()</code> and <code>Array.join()</code> to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
920920
<code>Array.slice()</code> is used to remove <code>#</code> from string start since it's added once.</p>
921921
<pre><code class="language-js">const extendHex = shortHex =&gt;
922-
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split().map(x =&gt; x+x).join()
922+
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x =&gt; x+x).join('')
923923
// convertHex('#03f') -&gt; '#0033ff'
924924
// convertHex('05a') -&gt; '#0055aa'
925925
</code></pre>
@@ -932,12 +932,16 @@ <h3>Utility
932932
</code></pre>
933933
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="hextorgb">hexToRGB</h3></div><div class="section double-padded">
934934
<p>Converts a colorcode to a <code>rgb()</code> string.</p>
935-
<p>Use bitwise right-shift operator and mask bits with <code>&amp;</code> (and) operator to convert a hexadecimal color code (prefixed with <code>#</code>) to a string with the RGB values.</p>
936-
<pre><code class="language-js">const hexToRGB = hex =&gt; {
937-
const h = parseInt(hex.slice(1), 16);
938-
return `rgb(${h &gt;&gt; 16}, ${(h &amp; 0x00ff00) &gt;&gt; 8}, ${h &amp; 0x0000ff})`;
939-
}
935+
<p>Use bitwise right-shift operator and mask bits with <code>&amp;</code> (and) operator to convert a hexadecimal color code (prefixed with <code>#</code>) 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. <code>extendHex</code> snippet)</p>
936+
<pre><code class="language-js">const hexToRgb = hex =&gt; {
937+
const extendHex = shortHex =&gt;
938+
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x =&gt; x+x).join('');
939+
return hex.slice(1).length==3 ?
940+
`rgb(${parseInt(extendHex(hex).slice(1), 16) &gt;&gt; 16}, ${(parseInt(extendHex(hex).slice(1), 16) &amp; 0x00ff00) &gt;&gt; 8}, ${parseInt(extendHex(hex).slice(1), 16) &amp; 0x0000ff})`:
941+
`rgb(${parseInt(hex.slice(1), 16) &gt;&gt; 16}, ${(parseInt(hex.slice(1), 16) &amp; 0x00ff00) &gt;&gt; 8}, ${parseInt(hex.slice(1), 16) &amp; 0x0000ff})`;
942+
}
940943
// hexToRgb('#27ae60') -&gt; 'rgb(39, 174, 96)'
944+
// hexToRgb('#acd') -&gt; 'rgb(170, 204, 221)'
941945
</code></pre>
942946
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="isarray">isArray</h3></div><div class="section double-padded">
943947
<p>Checks if the given argument is an array.</p>

snippets/arrayMin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### arrayMin
22

3-
Returns the maximum value in an array.
3+
Returns the minimum value in an array.
44

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

snippets/hexToRGB.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
Converts a colorcode to a `rgb()` string.
44

5-
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.
5+
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)
66

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

0 commit comments

Comments
 (0)