Skip to content

Commit

Permalink
fix: node 6 compatibility, take 2 (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 23, 2021
1 parent edb2e01 commit c3bf3b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parseToRgba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const compressedColorMap = '1q29ehhb 1n09sgk7 1kl1ekf_ _yl4zsno 16z9eiv3 1p29lhp
const key = colorToInt(next.substring(0, 3));
const hex = colorToInt(next.substring(3)).toString(16);

// NOTE: pad start could be used here but it breaks Node 6 compat
// NOTE: padStart could be used here but it breaks Node 6 compat
// https://github.com/ricokahler/color2k/issues/351
let prefix = '';
for (let i = 0; i < 6 - hex.length; i++) {
Expand Down
13 changes: 12 additions & 1 deletion src/toHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import guard from './guard';
*/
function toHex(color: string): string {
const [r, g, b, a] = parseToRgba(color);
const hex = (x: number) => guard(0, 255, x).toString(16).padStart(2, '0');

let hex = (x: number) => {
const h = guard(0, 255, x).toString(16).padStart(2, '0');
// NOTE: padStart could be used here but it breaks Node 6 compat
// https://github.com/ricokahler/color2k/issues/351
let prefix = '';
for (let i = 0; i < 2 - h.length; i++) {
prefix += '0';
}
return `${prefix}${h}`;
};

return `#${hex(r)}${hex(g)}${hex(b)}${a < 1 ? hex(Math.round(a * 255)) : ''}`;
}

Expand Down

0 comments on commit c3bf3b0

Please sign in to comment.