ansi-styles 5.1.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +51 -42
  2. package/package.json +1 -1
  3. package/readme.md +3 -3
package/index.js CHANGED
@@ -101,49 +101,58 @@ function assembleStyles() {
101
101
  styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
102
102
 
103
103
  // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
104
- styles.rgbToAnsi256 = (red, green, blue) => {
105
- // We use the extended greyscale palette here, with the exception of
106
- // black and white. normal palette only has 4 greyscale shades.
107
- if (red === green && green === blue) {
108
- if (red < 8) {
109
- return 16;
110
- }
111
-
112
- if (red > 248) {
113
- return 231;
114
- }
115
-
116
- return Math.round(((red - 8) / 247) * 24) + 232;
117
- }
118
-
119
- return 16 +
120
- (36 * Math.round(red / 255 * 5)) +
121
- (6 * Math.round(green / 255 * 5)) +
122
- Math.round(blue / 255 * 5);
123
- };
124
-
125
- styles.hexToRgb = hex => {
126
- const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
127
- if (!matches) {
128
- return [0, 0, 0];
129
- }
130
-
131
- let {colorString} = matches.groups;
132
-
133
- if (colorString.length === 3) {
134
- colorString = colorString.split('').map(character => character + character).join('');
104
+ Object.defineProperties(styles, {
105
+ rgbToAnsi256: {
106
+ value: (red, green, blue) => {
107
+ // We use the extended greyscale palette here, with the exception of
108
+ // black and white. normal palette only has 4 greyscale shades.
109
+ if (red === green && green === blue) {
110
+ if (red < 8) {
111
+ return 16;
112
+ }
113
+
114
+ if (red > 248) {
115
+ return 231;
116
+ }
117
+
118
+ return Math.round(((red - 8) / 247) * 24) + 232;
119
+ }
120
+
121
+ return 16 +
122
+ (36 * Math.round(red / 255 * 5)) +
123
+ (6 * Math.round(green / 255 * 5)) +
124
+ Math.round(blue / 255 * 5);
125
+ },
126
+ enumerable: false
127
+ },
128
+ hexToRgb: {
129
+ value: hex => {
130
+ const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
131
+ if (!matches) {
132
+ return [0, 0, 0];
133
+ }
134
+
135
+ let {colorString} = matches.groups;
136
+
137
+ if (colorString.length === 3) {
138
+ colorString = colorString.split('').map(character => character + character).join('');
139
+ }
140
+
141
+ const integer = Number.parseInt(colorString, 16);
142
+
143
+ return [
144
+ (integer >> 16) & 0xFF,
145
+ (integer >> 8) & 0xFF,
146
+ integer & 0xFF
147
+ ];
148
+ },
149
+ enumerable: false
150
+ },
151
+ hexToAnsi256: {
152
+ value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
153
+ enumerable: false
135
154
  }
136
-
137
- const integer = Number.parseInt(colorString, 16);
138
-
139
- return [
140
- (integer >> 16) & 0xFF,
141
- (integer >> 8) & 0xFF,
142
- integer & 0xFF
143
- ];
144
- };
145
-
146
- styles.hexToAnsi256 = hex => styles.rgbToAnsi256(...styles.hexToRgb(hex));
155
+ });
147
156
 
148
157
  return styles;
149
158
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/ansi-styles",
package/readme.md CHANGED
@@ -20,9 +20,9 @@ const style = require('ansi-styles');
20
20
  console.log(`${style.green.open}Hello world!${style.green.close}`);
21
21
 
22
22
 
23
- // Color conversion between 16/256/truecolor
24
- // NOTE: If conversion goes to 16 colors or 256 colors, the original color
25
- // may be degraded to fit that color palette. This means terminals
23
+ // Color conversion between 256/truecolor
24
+ // NOTE: When converting from truecolor to 256 colors, the original color
25
+ // may be degraded to fit the new color palette. This means terminals
26
26
  // that do not support 16 million colors will best-match the
27
27
  // original color.
28
28
  console.log(`${style.color.ansi256(style.rgbToAnsi256(199, 20, 250))}Hello World${style.color.close}`)