diff --git a/src/parseToRgba.ts b/src/parseToRgba.ts index 70df320..860a75b 100644 --- a/src/parseToRgba.ts +++ b/src/parseToRgba.ts @@ -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++) { diff --git a/src/toHex.ts b/src/toHex.ts index e7eadd9..b35acd2 100644 --- a/src/toHex.ts +++ b/src/toHex.ts @@ -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)) : ''}`; }