Skip to content

Commit

Permalink
fix: node 6 compatibility, take 2 (#353) (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb authored Feb 23, 2021
1 parent edb2e01 commit 0a56624
Show file tree
Hide file tree
Showing 2 changed files with 9 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
9 changes: 8 additions & 1 deletion src/toHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ 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);
// NOTE: padStart could be used here but it breaks Node 6 compat
// https://github.com/ricokahler/color2k/issues/351
return h.length === 1 ? `0${h}` : h;
};

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

Expand Down

0 comments on commit 0a56624

Please sign in to comment.