@oxog/pigment 1.1.0 → 1.1.1

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @oxog/pigment - Zero-dependency terminal styling
2
+ * @oxog/pigment - Terminal styling
3
3
  * MIT License - https://pigment.oxog.dev
4
4
  */
5
5
  "use strict";
@@ -329,6 +329,12 @@ var PigmentError = class _PigmentError extends Error {
329
329
  Error.captureStackTrace(this, _PigmentError);
330
330
  }
331
331
  };
332
+ var PluginError = class extends PigmentError {
333
+ constructor(pluginName, message) {
334
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
335
+ this.name = "PluginError";
336
+ }
337
+ };
332
338
  var ColorError = class extends PigmentError {
333
339
  constructor(message) {
334
340
  super(message, "COLOR_ERROR");
@@ -608,7 +614,260 @@ function hslToRgb(h2, s, l) {
608
614
  };
609
615
  }
610
616
 
617
+ // src/utils/rgb-to-ansi256.ts
618
+ function rgbToAnsi256(r, g, b) {
619
+ const clampedR = Math.max(0, Math.min(255, r));
620
+ const clampedG = Math.max(0, Math.min(255, g));
621
+ const clampedB = Math.max(0, Math.min(255, b));
622
+ if (clampedR === clampedG && clampedG === clampedB) {
623
+ if (clampedR < 8) {
624
+ return 16;
625
+ }
626
+ if (clampedR > 248) {
627
+ return 231;
628
+ }
629
+ return Math.round((clampedR - 8) / 247 * 24) + 232;
630
+ }
631
+ return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
632
+ }
633
+
634
+ // src/core/ansi.ts
635
+ function generateModifierCode(name) {
636
+ const modifier = ANSI_MODIFIERS[name];
637
+ if (!modifier) {
638
+ throw new StyleError(`Unknown modifier: ${name}`);
639
+ }
640
+ return {
641
+ name,
642
+ code: -1,
643
+ open: modifier.open,
644
+ close: modifier.close,
645
+ type: "modifier"
646
+ };
647
+ }
648
+ function generateColorCode(name, background = false) {
649
+ let normalizedName = name;
650
+ const lowerName = name.toLowerCase();
651
+ if (lowerName === "gray" || lowerName === "grey") {
652
+ normalizedName = "blackBright";
653
+ } else if (lowerName === "bggray" || lowerName === "bggrey") {
654
+ normalizedName = "bgBlackBright";
655
+ }
656
+ const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
657
+ const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
658
+ let lookupName = normalizedName;
659
+ if (background && !normalizedName.startsWith("bg")) {
660
+ lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
661
+ }
662
+ const allColors = background ? backgroundColors : foregroundColors;
663
+ const code = allColors[lookupName];
664
+ if (code === void 0) {
665
+ throw new StyleError(`Unknown color: ${name}`);
666
+ }
667
+ const closeCode = background ? 49 : 39;
668
+ return {
669
+ name,
670
+ code,
671
+ open: `\x1B[${code}m`,
672
+ close: `\x1B[${closeCode}m`,
673
+ type: "color",
674
+ background
675
+ };
676
+ }
677
+ function generateAnsi256Code(code, background = false) {
678
+ if (code < 0 || code > 255) {
679
+ throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
680
+ }
681
+ const prefix = background ? 48 : 38;
682
+ const closeCode = background ? 49 : 39;
683
+ return {
684
+ name: `ansi256(${code})`,
685
+ code,
686
+ open: `\x1B[${prefix};5;${code}m`,
687
+ close: `\x1B[${closeCode}m`,
688
+ type: "color",
689
+ background
690
+ };
691
+ }
692
+ function generateRgbCode(r, g, b, background = false) {
693
+ if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
694
+ throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
695
+ }
696
+ const prefix = background ? 48 : 38;
697
+ const closeCode = background ? 49 : 39;
698
+ return {
699
+ name: `rgb(${r}, ${g}, ${b})`,
700
+ code: -1,
701
+ open: `\x1B[${prefix};2;${r};${g};${b}m`,
702
+ close: `\x1B[${closeCode}m`,
703
+ type: "color",
704
+ background
705
+ };
706
+ }
707
+ function ansi256ToAnsi16(code) {
708
+ if (code < 8) {
709
+ return 30 + code;
710
+ }
711
+ if (code < 16) {
712
+ return 90 + (code - 8);
713
+ }
714
+ let red;
715
+ let green;
716
+ let blue;
717
+ if (code >= 232) {
718
+ red = ((code - 232) * 10 + 8) / 255;
719
+ green = red;
720
+ blue = red;
721
+ } else {
722
+ const index = code - 16;
723
+ const remainder = index % 36;
724
+ red = Math.floor(index / 36) / 5;
725
+ green = Math.floor(remainder / 6) / 5;
726
+ blue = remainder % 6 / 5;
727
+ }
728
+ const value = Math.max(red, green, blue) * 2;
729
+ if (value === 0) {
730
+ return 30;
731
+ }
732
+ let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
733
+ if (value === 2) {
734
+ result += 60;
735
+ }
736
+ return result;
737
+ }
738
+ function effectiveLevel(level) {
739
+ return level ?? 3;
740
+ }
741
+ function ansi16Style(name, paletteCode, background) {
742
+ const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
743
+ return {
744
+ name,
745
+ code,
746
+ open: `\x1B[${code}m`,
747
+ close: `\x1B[${background ? 49 : 39}m`,
748
+ type: "color",
749
+ background
750
+ };
751
+ }
752
+ function generateAnsi256CodeForLevel(code, background, level) {
753
+ const style = generateAnsi256Code(code, background);
754
+ if (effectiveLevel(level) >= 2) {
755
+ return style;
756
+ }
757
+ return ansi16Style(style.name, code, background);
758
+ }
759
+ function generateRgbCodeForLevel(r, g, b, background, level) {
760
+ const style = generateRgbCode(r, g, b, background);
761
+ const effective = effectiveLevel(level);
762
+ if (effective >= 3) {
763
+ return style;
764
+ }
765
+ const paletteCode = rgbToAnsi256(r, g, b);
766
+ if (effective === 2) {
767
+ return { ...generateAnsi256Code(paletteCode, background), name: style.name };
768
+ }
769
+ return ansi16Style(style.name, paletteCode, background);
770
+ }
771
+ function paintRgb(text, rgb, background, level) {
772
+ if (level === 0 || text === "") {
773
+ return text;
774
+ }
775
+ const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
776
+ return `${style.open}${text}${style.close}`;
777
+ }
778
+
779
+ // src/core/plugin-api.ts
780
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
781
+ var globalRegistry = globalThis;
782
+ var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
783
+ var { apis, roots } = registry;
784
+ var PLUGIN_API_PROVIDERS = {
785
+ gradient: "gradientPlugin",
786
+ rainbow: "gradientPlugin",
787
+ theme: "themePlugin",
788
+ success: "semanticPlugin",
789
+ error: "semanticPlugin",
790
+ warning: "semanticPlugin",
791
+ info: "semanticPlugin",
792
+ debug: "semanticPlugin",
793
+ box: "boxPlugin",
794
+ template: "templatePlugin"
795
+ };
796
+ function registerPluginApi(kernel, name, value) {
797
+ let api = apis.get(kernel);
798
+ if (!api) {
799
+ api = /* @__PURE__ */ new Map();
800
+ apis.set(kernel, api);
801
+ }
802
+ api.set(name, value);
803
+ }
804
+ function getPluginApi(kernel, name) {
805
+ return apis.get(kernel)?.get(name);
806
+ }
807
+ function registerRoot(kernel, root) {
808
+ roots.set(kernel, root);
809
+ }
810
+ function getRoot(kernel) {
811
+ return roots.get(kernel);
812
+ }
813
+ function missingPluginApi(name) {
814
+ const plugin = PLUGIN_API_PROVIDERS[name];
815
+ const fail = () => {
816
+ throw new PluginError(
817
+ plugin,
818
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
819
+ );
820
+ };
821
+ if (name === "theme") {
822
+ return {
823
+ keyword: fail,
824
+ string: fail,
825
+ number: fail,
826
+ comment: fail,
827
+ error: fail,
828
+ success: fail,
829
+ warning: fail,
830
+ info: fail
831
+ };
832
+ }
833
+ return fail;
834
+ }
835
+ function installPlugin(kernel, plugin) {
836
+ kernel.use(plugin);
837
+ if (plugin.onInit) {
838
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
839
+ });
840
+ }
841
+ }
842
+
611
843
  // src/plugins/optional/gradient.ts
844
+ function toRgb(color) {
845
+ if (typeof color !== "string") {
846
+ const [r, g, b] = color;
847
+ return { r, g, b };
848
+ }
849
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
850
+ }
851
+ function paintGradient(text, startRgb, endRgb, steps, level) {
852
+ if (text.length === 0) {
853
+ return text;
854
+ }
855
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
856
+ let result = "";
857
+ for (let i = 0; i < text.length; i++) {
858
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
859
+ result += paintRgb(text[i], colors[colorIndex], false, level);
860
+ }
861
+ return result;
862
+ }
863
+ function paintRainbow(text, steps, level) {
864
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
865
+ let result = "";
866
+ for (let i = 0; i < text.length; i++) {
867
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
868
+ }
869
+ return result;
870
+ }
612
871
  function gradientPlugin(options) {
613
872
  const steps = options?.steps ?? 10;
614
873
  return {
@@ -622,6 +881,15 @@ function gradientPlugin(options) {
622
881
  enabled: true
623
882
  }
624
883
  });
884
+ registerPluginApi(kernel, "gradient", (start, end) => {
885
+ const startRgb = toRgb(start);
886
+ const endRgb = toRgb(end);
887
+ if (!startRgb || !endRgb) {
888
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
889
+ }
890
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
891
+ });
892
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
625
893
  k.on("style:gradient", (data) => {
626
894
  const { start, end, text, steps: gradientSteps = steps } = data;
627
895
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -692,6 +960,7 @@ function generateRainbowColors(length, steps) {
692
960
  }
693
961
 
694
962
  // src/plugins/optional/theme.ts
963
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
695
964
  function themePlugin(options) {
696
965
  const preset = options?.preset ?? "monokai";
697
966
  const custom = options?.custom ?? {};
@@ -711,6 +980,17 @@ function themePlugin(options) {
711
980
  enabled: true
712
981
  }
713
982
  });
983
+ const api = {};
984
+ for (const type of THEME_COLOR_TYPES) {
985
+ const color = theme[type];
986
+ api[type] = (text) => {
987
+ if (!color) {
988
+ throw new Error(`Unknown theme color type: ${type}`);
989
+ }
990
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
991
+ };
992
+ }
993
+ registerPluginApi(kernel, "theme", api);
714
994
  k.on("style:theme", (data) => {
715
995
  const { type, text } = data;
716
996
  const color = theme[type];
@@ -780,6 +1060,10 @@ function semanticPlugin(options) {
780
1060
  if (!hexToRgb2) {
781
1061
  throw new Error("hexToRgb utility not available");
782
1062
  }
1063
+ for (const [name, color] of Object.entries(defaultColors)) {
1064
+ const rgb = hexToRgb2(color);
1065
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
1066
+ }
783
1067
  k.on("style:success", (data) => {
784
1068
  const { r, g, b } = hexToRgb2(defaultColors.success);
785
1069
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -821,9 +1105,12 @@ function boxPlugin(options) {
821
1105
  borders: BOX_BORDERS
822
1106
  }
823
1107
  });
824
- k.on("style:box", (data) => {
825
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
1108
+ const draw = (text, opts = {}) => {
1109
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
826
1110
  const chars = BOX_BORDERS[boxBorder];
1111
+ if (!chars) {
1112
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
1113
+ }
827
1114
  const lines = text.split("\n");
828
1115
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
829
1116
  const pad = " ".repeat(boxPadding);
@@ -835,21 +1122,152 @@ function boxPlugin(options) {
835
1122
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
836
1123
  const result = [top, ...middle, bottom].join("\n");
837
1124
  if (bgColor) {
838
- const ctx = k.getContext();
839
- const hexToRgb2 = ctx.utils?.hexToRgb;
840
- if (!hexToRgb2) {
841
- throw new Error("hexToRgb utility not available");
842
- }
843
- const { r, g, b } = hexToRgb2(bgColor);
844
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
1125
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
845
1126
  }
846
1127
  return result;
1128
+ };
1129
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
1130
+ k.on("style:box", (data) => {
1131
+ const { text, ...opts } = data;
1132
+ return draw(text, opts);
847
1133
  });
848
1134
  }
849
1135
  };
850
1136
  }
851
1137
 
1138
+ // src/core/proxy.ts
1139
+ var proxyCache = /* @__PURE__ */ new WeakMap();
1140
+ function createProxyPigment(kernel, styles = []) {
1141
+ const handler = {
1142
+ get(target, prop) {
1143
+ const ctx = kernel.getContext();
1144
+ if (prop === "level") {
1145
+ return ctx.level;
1146
+ }
1147
+ if (prop === "supportsColor") {
1148
+ return ctx.colorSupport;
1149
+ }
1150
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
1151
+ const modifierStyle = generateModifierCode(prop);
1152
+ return createProxyPigment(kernel, [...styles, modifierStyle]);
1153
+ }
1154
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
1155
+ const background = prop.startsWith("bg");
1156
+ const colorName = background ? prop.slice(2) : prop;
1157
+ const colorStyle = generateColorCode(colorName, background);
1158
+ return createProxyPigment(kernel, [...styles, colorStyle]);
1159
+ }
1160
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
1161
+ if (prop === "ansi256" || prop === "bgAnsi256") {
1162
+ return (code) => {
1163
+ const background = prop === "bgAnsi256";
1164
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
1165
+ return createProxyPigment(kernel, [...styles, ansi256Style]);
1166
+ };
1167
+ }
1168
+ if (prop === "rgb" || prop === "bgRgb") {
1169
+ return (r, g, b) => {
1170
+ const background = prop === "bgRgb";
1171
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1172
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1173
+ };
1174
+ }
1175
+ if (prop === "hex" || prop === "bgHex") {
1176
+ return (color) => {
1177
+ const background = prop === "bgHex";
1178
+ const hexToRgb2 = ctx.utils?.hexToRgb;
1179
+ if (!hexToRgb2) {
1180
+ throw new Error("hexToRgb utility not available");
1181
+ }
1182
+ const { r, g, b } = hexToRgb2(color);
1183
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1184
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1185
+ };
1186
+ }
1187
+ if (prop === "hsl" || prop === "bgHsl") {
1188
+ return (h2, s, l) => {
1189
+ const background = prop === "bgHsl";
1190
+ const hslToRgb2 = ctx.utils?.hslToRgb;
1191
+ if (!hslToRgb2) {
1192
+ throw new Error("hslToRgb utility not available");
1193
+ }
1194
+ const { r, g, b } = hslToRgb2(h2, s, l);
1195
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1196
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1197
+ };
1198
+ }
1199
+ }
1200
+ if (prop === "visible") {
1201
+ const colorSupport = ctx.colorSupport;
1202
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1203
+ }
1204
+ if (prop === "use") {
1205
+ return (plugin) => {
1206
+ installPlugin(kernel, plugin);
1207
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1208
+ };
1209
+ }
1210
+ if (Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, prop)) {
1211
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
1212
+ }
1213
+ return Reflect.get(target, prop);
1214
+ },
1215
+ apply(_target, _thisArg, argArray) {
1216
+ const text = String(argArray[0] ?? "");
1217
+ return applyStyles(text, styles, kernel.getContext().level);
1218
+ }
1219
+ };
1220
+ const pigment = new Proxy((() => ""), handler);
1221
+ proxyCache.set(pigment, pigment);
1222
+ return pigment;
1223
+ }
1224
+ function applyStyles(text, styles, level) {
1225
+ if (styles.length === 0 || text === "" || level === 0) {
1226
+ return text;
1227
+ }
1228
+ const open = styles.map((style) => style.open).join("");
1229
+ const close = [...styles].reverse().map((style) => style.close).join("");
1230
+ let result = text;
1231
+ for (const style of styles) {
1232
+ if (style.close) {
1233
+ result = result.split(style.close).join(style.close + style.open);
1234
+ }
1235
+ }
1236
+ return `${open}${result}${close}`;
1237
+ }
1238
+
852
1239
  // src/plugins/optional/template.ts
1240
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
1241
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
1242
+ var TOKEN = /\uE000(\d+)\uE001/g;
1243
+ function renderTemplate(root, strings, values) {
1244
+ const parts = values.map((value) => String(value));
1245
+ const token = (index) => `\uE000${index}\uE001`;
1246
+ const restore = (text) => text.replace(TOKEN, (_match, index) => parts[Number(index)]);
1247
+ let source = "";
1248
+ for (let i = 0; i < strings.length; i++) {
1249
+ source += strings[i];
1250
+ if (i < values.length) {
1251
+ source += token(i);
1252
+ }
1253
+ }
1254
+ let previous;
1255
+ do {
1256
+ previous = source;
1257
+ source = source.replace(BLOCK, (_match, chain, text) => {
1258
+ let styler = root;
1259
+ for (const name of chain.split(".")) {
1260
+ if (!STYLE_NAMES.has(name)) {
1261
+ throw new StyleError(`Unknown style in template: ${name}`);
1262
+ }
1263
+ styler = styler[name];
1264
+ }
1265
+ parts.push(styler(restore(text)));
1266
+ return token(parts.length - 1);
1267
+ });
1268
+ } while (source !== previous);
1269
+ return restore(source);
1270
+ }
853
1271
  function templatePlugin() {
854
1272
  return {
855
1273
  name: "template",
@@ -861,6 +1279,11 @@ function templatePlugin() {
861
1279
  enabled: true
862
1280
  }
863
1281
  });
1282
+ registerPluginApi(
1283
+ kernel,
1284
+ "template",
1285
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
1286
+ );
864
1287
  k.on("style:template", (data) => {
865
1288
  const { strings, values } = data;
866
1289
  let result = "";
@@ -1362,176 +1785,16 @@ function W(d) {
1362
1785
  return new c(d);
1363
1786
  }
1364
1787
 
1365
- // src/core/ansi.ts
1366
- function generateModifierCode(name) {
1367
- const modifier = ANSI_MODIFIERS[name];
1368
- if (!modifier) {
1369
- throw new StyleError(`Unknown modifier: ${name}`);
1370
- }
1371
- return {
1372
- name,
1373
- code: -1,
1374
- open: modifier.open,
1375
- close: modifier.close,
1376
- type: "modifier"
1377
- };
1378
- }
1379
- function generateColorCode(name, background = false) {
1380
- let normalizedName = name;
1381
- const lowerName = name.toLowerCase();
1382
- if (lowerName === "gray" || lowerName === "grey") {
1383
- normalizedName = "blackBright";
1384
- } else if (lowerName === "bggray" || lowerName === "bggrey") {
1385
- normalizedName = "bgBlackBright";
1386
- }
1387
- const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
1388
- const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
1389
- let lookupName = normalizedName;
1390
- if (background && !normalizedName.startsWith("bg")) {
1391
- lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
1392
- }
1393
- const allColors = background ? backgroundColors : foregroundColors;
1394
- const code = allColors[lookupName];
1395
- if (code === void 0) {
1396
- throw new StyleError(`Unknown color: ${name}`);
1397
- }
1398
- const closeCode = background ? 49 : 39;
1399
- return {
1400
- name,
1401
- code,
1402
- open: `\x1B[${code}m`,
1403
- close: `\x1B[${closeCode}m`,
1404
- type: "color",
1405
- background
1406
- };
1407
- }
1408
- function generateAnsi256Code(code, background = false) {
1409
- if (code < 0 || code > 255) {
1410
- throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
1411
- }
1412
- const prefix = background ? 48 : 38;
1413
- const closeCode = background ? 49 : 39;
1414
- return {
1415
- name: `ansi256(${code})`,
1416
- code,
1417
- open: `\x1B[${prefix};5;${code}m`,
1418
- close: `\x1B[${closeCode}m`,
1419
- type: "color",
1420
- background
1421
- };
1422
- }
1423
- function generateRgbCode(r, g, b, background = false) {
1424
- if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
1425
- throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
1426
- }
1427
- const prefix = background ? 48 : 38;
1428
- const closeCode = background ? 49 : 39;
1429
- return {
1430
- name: `rgb(${r}, ${g}, ${b})`,
1431
- code: -1,
1432
- open: `\x1B[${prefix};2;${r};${g};${b}m`,
1433
- close: `\x1B[${closeCode}m`,
1434
- type: "color",
1435
- background
1436
- };
1437
- }
1438
-
1439
- // src/core/proxy.ts
1440
- var proxyCache = /* @__PURE__ */ new WeakMap();
1441
- function createProxyPigment(kernel, styles = []) {
1442
- const handler = {
1443
- get(target, prop) {
1444
- const ctx = kernel.getContext();
1445
- if (prop === "level") {
1446
- return ctx.level;
1447
- }
1448
- if (prop === "supportsColor") {
1449
- return ctx.colorSupport;
1450
- }
1451
- if (MODIFIER_NAMES.some((name) => name === prop)) {
1452
- const modifierStyle = generateModifierCode(prop);
1453
- return createProxyPigment(kernel, [...styles, modifierStyle]);
1454
- }
1455
- if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
1456
- const background = prop.startsWith("bg");
1457
- const colorName = background ? prop.slice(2) : prop;
1458
- const colorStyle = generateColorCode(colorName, background);
1459
- return createProxyPigment(kernel, [...styles, colorStyle]);
1460
- }
1461
- if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
1462
- if (prop === "ansi256" || prop === "bgAnsi256") {
1463
- return (code) => {
1464
- const background = prop === "bgAnsi256";
1465
- const ansi256Style = generateAnsi256Code(code, background);
1466
- return createProxyPigment(kernel, [...styles, ansi256Style]);
1467
- };
1468
- }
1469
- if (prop === "rgb" || prop === "bgRgb") {
1470
- return (r, g, b) => {
1471
- const background = prop === "bgRgb";
1472
- const rgbStyle = generateRgbCode(r, g, b, background);
1473
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1474
- };
1475
- }
1476
- if (prop === "hex" || prop === "bgHex") {
1477
- return (color) => {
1478
- const background = prop === "bgHex";
1479
- const hexToRgb2 = ctx.utils?.hexToRgb;
1480
- if (!hexToRgb2) {
1481
- throw new Error("hexToRgb utility not available");
1482
- }
1483
- const { r, g, b } = hexToRgb2(color);
1484
- const rgbStyle = generateRgbCode(r, g, b, background);
1485
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1486
- };
1487
- }
1488
- if (prop === "hsl" || prop === "bgHsl") {
1489
- return (h2, s, l) => {
1490
- const background = prop === "bgHsl";
1491
- const hslToRgb2 = ctx.utils?.hslToRgb;
1492
- if (!hslToRgb2) {
1493
- throw new Error("hslToRgb utility not available");
1494
- }
1495
- const { r, g, b } = hslToRgb2(h2, s, l);
1496
- const rgbStyle = generateRgbCode(r, g, b, background);
1497
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1498
- };
1499
- }
1500
- }
1501
- if (prop === "visible") {
1502
- const colorSupport = ctx.colorSupport;
1503
- return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
1504
- }
1505
- return Reflect.get(target, prop);
1506
- },
1507
- apply(_target, _thisArg, argArray) {
1508
- const text = String(argArray[0] ?? "");
1509
- return applyStyles(text, styles);
1510
- }
1511
- };
1512
- const pigment = new Proxy((() => ""), handler);
1513
- proxyCache.set(pigment, pigment);
1514
- return pigment;
1515
- }
1516
- function applyStyles(text, styles) {
1517
- if (styles.length === 0 || text === "") {
1518
- return text;
1519
- }
1520
- const open = styles.map((style) => style.open).join("");
1521
- const close = [...styles].reverse().map((style) => style.close).join("");
1522
- let result = text;
1523
- for (const style of styles) {
1524
- if (style.close) {
1525
- result = result.split(style.close).join(style.close + style.open);
1526
- }
1527
- }
1528
- return `${open}${result}${close}`;
1529
- }
1530
-
1531
1788
  // src/core/builder.ts
1532
1789
  var BuilderPigment = class {
1533
1790
  styles = [];
1534
- constructor(_kernel) {
1791
+ kernel;
1792
+ constructor(kernel) {
1793
+ this.kernel = kernel;
1794
+ }
1795
+ /** The instance's color level (true color when there is no kernel). */
1796
+ get level() {
1797
+ return this.kernel?.getContext().level;
1535
1798
  }
1536
1799
  bold() {
1537
1800
  this.styles.push(generateModifierCode("bold"));
@@ -1710,26 +1973,26 @@ var BuilderPigment = class {
1710
1973
  return this;
1711
1974
  }
1712
1975
  ansi256(code) {
1713
- this.styles.push(generateAnsi256Code(code, false));
1976
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1714
1977
  return this;
1715
1978
  }
1716
1979
  bgAnsi256(code) {
1717
- this.styles.push(generateAnsi256Code(code, true));
1980
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1718
1981
  return this;
1719
1982
  }
1720
1983
  rgb(r, g, b) {
1721
- this.styles.push(generateRgbCode(r, g, b, false));
1984
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1722
1985
  return this;
1723
1986
  }
1724
1987
  bgRgb(r, g, b) {
1725
- this.styles.push(generateRgbCode(r, g, b, true));
1988
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1726
1989
  return this;
1727
1990
  }
1728
1991
  hex(color) {
1729
1992
  const rgb = hexToRgb(color);
1730
1993
  if (rgb) {
1731
1994
  const { r, g, b } = rgb;
1732
- this.styles.push(generateRgbCode(r, g, b, false));
1995
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1733
1996
  }
1734
1997
  return this;
1735
1998
  }
@@ -1737,22 +2000,22 @@ var BuilderPigment = class {
1737
2000
  const rgb = hexToRgb(color);
1738
2001
  if (rgb) {
1739
2002
  const { r, g, b } = rgb;
1740
- this.styles.push(generateRgbCode(r, g, b, true));
2003
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1741
2004
  }
1742
2005
  return this;
1743
2006
  }
1744
2007
  hsl(h2, s, l) {
1745
2008
  const { r, g, b } = hslToRgb(h2, s, l);
1746
- this.styles.push(generateRgbCode(r, g, b, false));
2009
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1747
2010
  return this;
1748
2011
  }
1749
2012
  bgHsl(h2, s, l) {
1750
2013
  const { r, g, b } = hslToRgb(h2, s, l);
1751
- this.styles.push(generateRgbCode(r, g, b, true));
2014
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1752
2015
  return this;
1753
2016
  }
1754
2017
  paint(text) {
1755
- if (this.styles.length === 0 || text === "") {
2018
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1756
2019
  return text;
1757
2020
  }
1758
2021
  const open = this.styles.map((style) => style.open).join("");
@@ -1767,23 +2030,6 @@ var BuilderPigment = class {
1767
2030
  }
1768
2031
  };
1769
2032
 
1770
- // src/utils/rgb-to-ansi256.ts
1771
- function rgbToAnsi256(r, g, b) {
1772
- const clampedR = Math.max(0, Math.min(255, r));
1773
- const clampedG = Math.max(0, Math.min(255, g));
1774
- const clampedB = Math.max(0, Math.min(255, b));
1775
- if (clampedR === clampedG && clampedG === clampedB) {
1776
- if (clampedR < 8) {
1777
- return 16;
1778
- }
1779
- if (clampedR > 248) {
1780
- return 231;
1781
- }
1782
- return Math.round((clampedR - 8) / 247 * 24) + 232;
1783
- }
1784
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1785
- }
1786
-
1787
2033
  // src/core/factory.ts
1788
2034
  function createPigment(options) {
1789
2035
  const colorSupport = detectColorSupport(options?.level);
@@ -1813,13 +2059,12 @@ function createPigment(options) {
1813
2059
  kernel.use(environmentPlugin());
1814
2060
  if (options?.plugins) {
1815
2061
  for (const plugin of options.plugins) {
1816
- kernel.use(plugin);
2062
+ installPlugin(kernel, plugin);
1817
2063
  }
1818
2064
  }
1819
- void kernel.init();
1820
2065
  const pigment = createProxyPigment(kernel);
1821
- const builder = new BuilderPigment(kernel);
1822
- Object.defineProperty(pigment, "builder", { value: builder, enumerable: false });
2066
+ registerRoot(kernel, pigment);
2067
+ Object.defineProperty(pigment, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1823
2068
  Object.defineProperty(pigment, "level", { value: level, enumerable: true });
1824
2069
  Object.defineProperty(pigment, "supportsColor", { value: colorSupport, enumerable: true });
1825
2070
  return pigment;