Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: node 6 compatibility, take 2 (#355) #355

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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