Skip to content

Commit

Permalink
Make color conversion functions non-enumerable (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Mar 31, 2021
1 parent 2f9bea0 commit cdc520c
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,49 +101,58 @@ function assembleStyles() {
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);

// From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
styles.rgbToAnsi256 = (red, green, blue) => {
// We use the extended greyscale palette here, with the exception of
// black and white. normal palette only has 4 greyscale shades.
if (red === green && green === blue) {
if (red < 8) {
return 16;
}

if (red > 248) {
return 231;
}

return Math.round(((red - 8) / 247) * 24) + 232;
}

return 16 +
(36 * Math.round(red / 255 * 5)) +
(6 * Math.round(green / 255 * 5)) +
Math.round(blue / 255 * 5);
};

styles.hexToRgb = hex => {
const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
if (!matches) {
return [0, 0, 0];
}

let {colorString} = matches.groups;

if (colorString.length === 3) {
colorString = colorString.split('').map(character => character + character).join('');
Object.defineProperties(styles, {
rgbToAnsi256: {
value: (red, green, blue) => {
// We use the extended greyscale palette here, with the exception of
// black and white. normal palette only has 4 greyscale shades.
if (red === green && green === blue) {
if (red < 8) {
return 16;
}

if (red > 248) {
return 231;
}

return Math.round(((red - 8) / 247) * 24) + 232;
}

return 16 +
(36 * Math.round(red / 255 * 5)) +
(6 * Math.round(green / 255 * 5)) +
Math.round(blue / 255 * 5);
},
enumerable: false
},
hexToRgb: {
value: hex => {
const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
if (!matches) {
return [0, 0, 0];
}

let {colorString} = matches.groups;

if (colorString.length === 3) {
colorString = colorString.split('').map(character => character + character).join('');
}

const integer = Number.parseInt(colorString, 16);

return [
(integer >> 16) & 0xFF,
(integer >> 8) & 0xFF,
integer & 0xFF
];
},
enumerable: false
},
hexToAnsi256: {
value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
enumerable: false
}

const integer = Number.parseInt(colorString, 16);

return [
(integer >> 16) & 0xFF,
(integer >> 8) & 0xFF,
integer & 0xFF
];
};

styles.hexToAnsi256 = hex => styles.rgbToAnsi256(...styles.hexToRgb(hex));
});

return styles;
}
Expand Down

0 comments on commit cdc520c

Please sign in to comment.