@al8b/palette 0.1.13 → 0.1.14

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.
@@ -24,7 +24,6 @@ __export(palette_exports, {
24
24
  Palette: () => Palette
25
25
  });
26
26
  module.exports = __toCommonJS(palette_exports);
27
- var import_diagnostics = require("@al8b/diagnostics");
28
27
  var HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;
29
28
  var Palette = class _Palette {
30
29
  static {
@@ -38,8 +37,12 @@ var Palette = class _Palette {
38
37
  this.runtime = runtime;
39
38
  if ("colors" in options && Array.isArray(options.colors)) {
40
39
  if (!this.validatePaletteFormat(options.colors)) {
41
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
42
- format: "invalid color array"
40
+ this.runtime?.listener?.reportError?.({
41
+ code: "E7072",
42
+ message: "Invalid palette format",
43
+ data: {
44
+ format: "invalid color array"
45
+ }
43
46
  });
44
47
  this.colors = [];
45
48
  this.name = "Invalid";
@@ -67,9 +70,13 @@ var Palette = class _Palette {
67
70
  */
68
71
  get(index) {
69
72
  if (!isFinite(index) || index < 0) {
70
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7073, {
71
- index,
72
- maxIndex: this.colors.length - 1
73
+ this.runtime?.listener?.reportError?.({
74
+ code: "E7073",
75
+ message: "Invalid color index",
76
+ data: {
77
+ index,
78
+ maxIndex: this.colors.length - 1
79
+ }
73
80
  });
74
81
  return "#000000";
75
82
  }
@@ -115,15 +122,23 @@ var Palette = class _Palette {
115
122
  */
116
123
  set(index, color) {
117
124
  if (!isFinite(index) || index < 0) {
118
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7073, {
119
- index,
120
- maxIndex: this.colors.length - 1
125
+ this.runtime?.listener?.reportError?.({
126
+ code: "E7073",
127
+ message: "Invalid color index",
128
+ data: {
129
+ index,
130
+ maxIndex: this.colors.length - 1
131
+ }
121
132
  });
122
133
  return;
123
134
  }
124
135
  if (!HEX_COLOR_REGEX.test(color)) {
125
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
126
- format: color
136
+ this.runtime?.listener?.reportError?.({
137
+ code: "E7072",
138
+ message: "Invalid color format",
139
+ data: {
140
+ format: color
141
+ }
127
142
  });
128
143
  return;
129
144
  }
@@ -154,8 +169,12 @@ var Palette = class _Palette {
154
169
  */
155
170
  setPalette(colors) {
156
171
  if (!this.validatePaletteFormat(colors)) {
157
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
158
- format: "invalid color array"
172
+ this.runtime?.listener?.reportError?.({
173
+ code: "E7072",
174
+ message: "Invalid palette format",
175
+ data: {
176
+ format: "invalid color array"
177
+ }
159
178
  });
160
179
  return;
161
180
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: color });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;AAIA,yBAAiD;AAIjD,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAfb,OAeaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChDQ,mDAAmB,KAAKL,SAASM,UAAUC,gCAAaC,OAAO;UAAEC,QAAQ;QAAsB,CAAA;AAC/F,aAAKZ,SAAS,CAAA;AACd,aA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,MAAMU,OAAOnC,KAAKoC,OAAOpC,IAAImC,OAAOnC,KAAKkC,KAAAA;AACxD,UAAM/B,IAAIqB,KAAKC,MAAMU,OAAOhC,KAAKiC,OAAOjC,IAAIgC,OAAOhC,KAAK+B,KAAAA;AACxD,UAAM9B,IAAIoB,KAAKC,MAAMU,OAAO/B,KAAKgC,OAAOhC,IAAI+B,OAAO/B,KAAK8B,KAAAA;AACxD,WAAO9E,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","reportRuntimeError","listener","APIErrorCode","E7072","format","Map","every","color","test","get","index","isFinite","E7073","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
1
+ {"version":3,"sources":["../../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid color format\", data: { format: color } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;AAOA,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAdb,OAcaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAE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AMpD,MAAM8B,KAAK,IAAIwB,OAAK,CAAA;AACrD,UAAMvB,IAAIoB,KAAKM,IAAI,GAAGN,KAAKC,MAAMpD,MAAM+B,KAAK,IAAIuB,OAAK,CAAA;AACrD,WAAOvE,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKA2B,IAAIC,QAAgBC,QAAgBC,QAAgB,KAAe;AAClE,UAAMC,SAAS,KAAKvD,OAAOoD,MAAAA;AAC3B,UAAMI,SAAS,KAAKxD,OAAOqD,MAAAA;AAC3B,UAAMjC,IAAIwB,KAAKC,MAAMU,OAAOnC,KAAKoC,OAAOpC,IAAImC,OAAOnC,KAAKkC,KAAAA;AACxD,UAAM/B,IAAIqB,KAAKC,MAAMU,OAAOhC,KAAKiC,OAAOjC,IAAIgC,OAAOhC,KAAK+B,KAAAA;AACxD,UAAM9B,IAAIoB,KAAKC,MAAMU,OAAO/B,KAAKgC,OAAOhC,IAAI+B,OAAO/B,KAAK8B,KAAAA;AACxD,WAAO9E,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","listener","reportError","code","message","data","format","Map","every","color","test","get","index","isFinite","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
@@ -2,7 +2,6 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/core/palette.ts
5
- import { APIErrorCode, reportRuntimeError } from "@al8b/diagnostics";
6
5
  var HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;
7
6
  var Palette = class _Palette {
8
7
  static {
@@ -16,8 +15,12 @@ var Palette = class _Palette {
16
15
  this.runtime = runtime;
17
16
  if ("colors" in options && Array.isArray(options.colors)) {
18
17
  if (!this.validatePaletteFormat(options.colors)) {
19
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
20
- format: "invalid color array"
18
+ this.runtime?.listener?.reportError?.({
19
+ code: "E7072",
20
+ message: "Invalid palette format",
21
+ data: {
22
+ format: "invalid color array"
23
+ }
21
24
  });
22
25
  this.colors = [];
23
26
  this.name = "Invalid";
@@ -45,9 +48,13 @@ var Palette = class _Palette {
45
48
  */
46
49
  get(index) {
47
50
  if (!isFinite(index) || index < 0) {
48
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, {
49
- index,
50
- maxIndex: this.colors.length - 1
51
+ this.runtime?.listener?.reportError?.({
52
+ code: "E7073",
53
+ message: "Invalid color index",
54
+ data: {
55
+ index,
56
+ maxIndex: this.colors.length - 1
57
+ }
51
58
  });
52
59
  return "#000000";
53
60
  }
@@ -93,15 +100,23 @@ var Palette = class _Palette {
93
100
  */
94
101
  set(index, color) {
95
102
  if (!isFinite(index) || index < 0) {
96
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, {
97
- index,
98
- maxIndex: this.colors.length - 1
103
+ this.runtime?.listener?.reportError?.({
104
+ code: "E7073",
105
+ message: "Invalid color index",
106
+ data: {
107
+ index,
108
+ maxIndex: this.colors.length - 1
109
+ }
99
110
  });
100
111
  return;
101
112
  }
102
113
  if (!HEX_COLOR_REGEX.test(color)) {
103
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
104
- format: color
114
+ this.runtime?.listener?.reportError?.({
115
+ code: "E7072",
116
+ message: "Invalid color format",
117
+ data: {
118
+ format: color
119
+ }
105
120
  });
106
121
  return;
107
122
  }
@@ -132,8 +147,12 @@ var Palette = class _Palette {
132
147
  */
133
148
  setPalette(colors) {
134
149
  if (!this.validatePaletteFormat(colors)) {
135
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
136
- format: "invalid color array"
150
+ this.runtime?.listener?.reportError?.({
151
+ code: "E7072",
152
+ message: "Invalid palette format",
153
+ data: {
154
+ format: "invalid color array"
155
+ }
137
156
  });
138
157
  return;
139
158
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: color });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAIA,SAASA,cAAcC,0BAA0B;AAIjD,IAAMC,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAfb,OAeaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChDQ,2BAAmB,KAAKL,SAASM,UAAUC,aAAaC,OAAO;UAAEC,QAAQ;QAAsB,CAAA;AAC/F,aAAKZ,SAAS,CAAA;AACd,aAAKC,OAAO;MACb,OAAO;AACN,aAAKD,SAAS;aAAII,QAAQJ;;AAC1B,aAAKC,OAAOG,QAAQH,QAAQ;MAC7B;IACD,OAAO;AAEN,WAAKD,SAAS,CAAA;AACd,WAAKC,OAAO;IACb;AAEA,SAAKC,WAAW,oBAAIW,IAAAA;EACrB;;;;EAKQN,sBAAsBP,QAAwB;AACrD,QAAI,CAACK,MAAMC,QAAQN,MAAAA,EAAS,QAAO;AACnC,WAAOA,OAAOc,MAAM,CAACC,UA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AKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["APIErrorCode","reportRuntimeError","HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","reportRuntimeError","listener","APIErrorCode","E7072","format","Map","every","color","test","get","index","isFinite","E7073","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
1
+ {"version":3,"sources":["../../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid color format\", data: { format: color } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAOA,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAdb,OAcaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChD,aAAKG,SAASK,UAAUC,cAAc;UAAEC,MAAM;UAASC,SAAS;UAA0BC,MAAM;YAAEC,QAAQ;UAAsB;QAAE,CAAA;AAClI,aAAKb,SAAS,CAAA;AACd,aAAKC,OAAO;MACb,OAAO;AACN,aAAKD,SAAS;aAAII,QAAQJ;;AAC1B,aAAKC,OAAOG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;AAC3B,UAAMjC,IAAIwB,KAAKC,MAAMU,OAAOnC,KAAKoC,OAAOpC,IAAImC,OAAOnC,KAAKkC,KAAAA;AACxD,UAAM/B,IAAIqB,KAAKC,MAAMU,OAAOhC,KAAKiC,OAAOjC,IAAIgC,OAAOhC,KAAK+B,KAAAA;AACxD,UAAM9B,IAAIoB,KAAKC,MAAMU,OAAO/B,KAAKgC,OAAOhC,IAAI+B,OAAO/B,KAAK8B,KAAAA;AACxD,WAAO9E,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","listener","reportError","code","message","data","format","Map","every","color","test","get","index","isFinite","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
package/dist/index.js CHANGED
@@ -26,7 +26,6 @@ __export(index_exports, {
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/core/palette.ts
29
- var import_diagnostics = require("@al8b/diagnostics");
30
29
  var HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;
31
30
  var Palette = class _Palette {
32
31
  static {
@@ -40,8 +39,12 @@ var Palette = class _Palette {
40
39
  this.runtime = runtime;
41
40
  if ("colors" in options && Array.isArray(options.colors)) {
42
41
  if (!this.validatePaletteFormat(options.colors)) {
43
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
44
- format: "invalid color array"
42
+ this.runtime?.listener?.reportError?.({
43
+ code: "E7072",
44
+ message: "Invalid palette format",
45
+ data: {
46
+ format: "invalid color array"
47
+ }
45
48
  });
46
49
  this.colors = [];
47
50
  this.name = "Invalid";
@@ -69,9 +72,13 @@ var Palette = class _Palette {
69
72
  */
70
73
  get(index) {
71
74
  if (!isFinite(index) || index < 0) {
72
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7073, {
73
- index,
74
- maxIndex: this.colors.length - 1
75
+ this.runtime?.listener?.reportError?.({
76
+ code: "E7073",
77
+ message: "Invalid color index",
78
+ data: {
79
+ index,
80
+ maxIndex: this.colors.length - 1
81
+ }
75
82
  });
76
83
  return "#000000";
77
84
  }
@@ -117,15 +124,23 @@ var Palette = class _Palette {
117
124
  */
118
125
  set(index, color) {
119
126
  if (!isFinite(index) || index < 0) {
120
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7073, {
121
- index,
122
- maxIndex: this.colors.length - 1
127
+ this.runtime?.listener?.reportError?.({
128
+ code: "E7073",
129
+ message: "Invalid color index",
130
+ data: {
131
+ index,
132
+ maxIndex: this.colors.length - 1
133
+ }
123
134
  });
124
135
  return;
125
136
  }
126
137
  if (!HEX_COLOR_REGEX.test(color)) {
127
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
128
- format: color
138
+ this.runtime?.listener?.reportError?.({
139
+ code: "E7072",
140
+ message: "Invalid color format",
141
+ data: {
142
+ format: color
143
+ }
129
144
  });
130
145
  return;
131
146
  }
@@ -156,8 +171,12 @@ var Palette = class _Palette {
156
171
  */
157
172
  setPalette(colors) {
158
173
  if (!this.validatePaletteFormat(colors)) {
159
- (0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7072, {
160
- format: "invalid color array"
174
+ this.runtime?.listener?.reportError?.({
175
+ code: "E7072",
176
+ message: "Invalid palette format",
177
+ data: {
178
+ format: "invalid color array"
179
+ }
161
180
  });
162
181
  return;
163
182
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/palette.ts"],"sourcesContent":["/**\n * @al8b/palette - Color palette management system\n *\n * Architecture:\n * - core/: Palette class + helpers\n * - types/: Shared color types\n */\n\nexport {\n\tPalette,\n\ttype PaletteOptions,\n} from \"./core/palette\";\nexport type {\n\tColorHex,\n\tColorRGB,\n\tPaletteData,\n} from \"./types\";\n","/**\n * Palette - Color palette management\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: color });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACIA,yBAAiD;AAIjD,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAfb,OAeaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChDQ,mDAAmB,KAAKL,SAASM,UAAUC,gCAAaC,OAAO;UAAEC,QAAQ;QAAsB,CAAA;AAC/F,aAAKZ,SAAS,CAAA;AACd,aAAKC,OAAO;MACb,OAAO;AACN,aAAKD,SAAS;aAAII,QAAQJ;;AAC1B,aAAKC,OAAOG,QAAQH,QAAQ;MAC7B;IACD,OAAO;AAEN,WAAKD,SAAS,CAAA;AACd,WAAKC,OAAO;IACb;AAEA,SAAKC,WAAW,oBAAIW,IAAAA;EACrB;;;;EAKQN,sBAAsBP,QAAwB;AACrD,QAAI,CAACK,MAAMC,QAAQN,MAAAA,EAAS,QAAO;AACnC,WAAOA,OAAOc,MAAM,CA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AC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","reportRuntimeError","listener","APIErrorCode","E7072","format","Map","every","color","test","get","index","isFinite","E7073","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/palette.ts"],"sourcesContent":["/**\n * @al8b/palette - Color palette management system\n *\n * Architecture:\n * - core/: Palette class + helpers\n * - types/: Shared color types\n */\n\nexport {\n\tPalette,\n\ttype PaletteOptions,\n} from \"./core/palette\";\nexport type {\n\tColorHex,\n\tColorRGB,\n\tPaletteData,\n} from \"./types\";\n","/**\n * Palette - Color palette management\n */\n\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid color format\", data: { format: color } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACOA,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAdb,OAcaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;A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AAIuB,OAAK,CAAA;AACrD,WAAOvE,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKA2B,IAAIC,QAAgBC,QAAgBC,QAAgB,KAAe;AAClE,UAAMC,SAAS,KAAKvD,OAAOoD,MAAAA;AAC3B,UAAMI,SAAS,KAAKxD,OAAOqD,MAAAA;AAC3B,UAAMjC,IAAIwB,KAAKC,MAAMU,OAAOnC,KAAKoC,OAAOpC,IAAImC,OAAOnC,KAAKkC,KAAAA;AACxD,UAAM/B,IAAIqB,KAAKC,MAAMU,OAAOhC,KAAKiC,OAAOjC,IAAIgC,OAAOhC,KAAK+B,KAAAA;AACxD,UAAM9B,IAAIoB,KAAKC,MAAMU,OAAO/B,KAAKgC,OAAOhC,IAAI+B,OAAO/B,KAAK8B,KAAAA;AACxD,WAAO9E,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","listener","reportError","code","message","data","format","Map","every","color","test","get","index","isFinite","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
package/dist/index.mjs CHANGED
@@ -2,7 +2,6 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/core/palette.ts
5
- import { APIErrorCode, reportRuntimeError } from "@al8b/diagnostics";
6
5
  var HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;
7
6
  var Palette = class _Palette {
8
7
  static {
@@ -16,8 +15,12 @@ var Palette = class _Palette {
16
15
  this.runtime = runtime;
17
16
  if ("colors" in options && Array.isArray(options.colors)) {
18
17
  if (!this.validatePaletteFormat(options.colors)) {
19
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
20
- format: "invalid color array"
18
+ this.runtime?.listener?.reportError?.({
19
+ code: "E7072",
20
+ message: "Invalid palette format",
21
+ data: {
22
+ format: "invalid color array"
23
+ }
21
24
  });
22
25
  this.colors = [];
23
26
  this.name = "Invalid";
@@ -45,9 +48,13 @@ var Palette = class _Palette {
45
48
  */
46
49
  get(index) {
47
50
  if (!isFinite(index) || index < 0) {
48
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, {
49
- index,
50
- maxIndex: this.colors.length - 1
51
+ this.runtime?.listener?.reportError?.({
52
+ code: "E7073",
53
+ message: "Invalid color index",
54
+ data: {
55
+ index,
56
+ maxIndex: this.colors.length - 1
57
+ }
51
58
  });
52
59
  return "#000000";
53
60
  }
@@ -93,15 +100,23 @@ var Palette = class _Palette {
93
100
  */
94
101
  set(index, color) {
95
102
  if (!isFinite(index) || index < 0) {
96
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, {
97
- index,
98
- maxIndex: this.colors.length - 1
103
+ this.runtime?.listener?.reportError?.({
104
+ code: "E7073",
105
+ message: "Invalid color index",
106
+ data: {
107
+ index,
108
+ maxIndex: this.colors.length - 1
109
+ }
99
110
  });
100
111
  return;
101
112
  }
102
113
  if (!HEX_COLOR_REGEX.test(color)) {
103
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
104
- format: color
114
+ this.runtime?.listener?.reportError?.({
115
+ code: "E7072",
116
+ message: "Invalid color format",
117
+ data: {
118
+ format: color
119
+ }
105
120
  });
106
121
  return;
107
122
  }
@@ -132,8 +147,12 @@ var Palette = class _Palette {
132
147
  */
133
148
  setPalette(colors) {
134
149
  if (!this.validatePaletteFormat(colors)) {
135
- reportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, {
136
- format: "invalid color array"
150
+ this.runtime?.listener?.reportError?.({
151
+ code: "E7072",
152
+ message: "Invalid palette format",
153
+ data: {
154
+ format: "invalid color array"
155
+ }
137
156
  });
138
157
  return;
139
158
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7073, { index, maxIndex: this.colors.length - 1 });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: color });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\treportRuntimeError(this.runtime?.listener, APIErrorCode.E7072, { format: \"invalid color array\" });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAIA,SAASA,cAAcC,0BAA0B;AAIjD,IAAMC,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAfb,OAeaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChDQ,2BAAmB,KAAKL,SAASM,UAAUC,aAAaC,OAAO;UAAEC,QAAQ;QAAsB,CAAA;AAC/F,aAAKZ,SAAS,CAAA;AACd,aAAKC,OAAO;MACb,OAAO;AACN,aAAKD,SAAS;aAAII,QAAQJ;;AAC1B,aAAKC,OAAOG,QAAQH,QAAQ;MAC7B;IACD,OAAO;AAEN,WAAKD,SAAS,CAAA;AACd,WAAKC,OAAO;IACb;AAEA,SAAKC,WAAW,oBAAIW,IAAAA;EACrB;;;;EAKQN,sBAAsBP,QAAwB;AACrD,QAAI,CAACK,MAAMC,QAAQN,MAAAA,EAAS,QAAO;AACnC,WAAOA,OAAOc,MAAM,CAACC,UAAU,OAAOA,UAAU,YAAYjB,gBAAgBkB,KAAKD,KAAAA,CAAAA;EAClF;;;;EAKAE,IAAIC,OAAyB;AAE5B,Q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ErrorCode","reportRuntimeError","HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","reportRuntimeError","listener","APIErrorCode","E7072","format","Map","every","color","test","get","index","isFinite","E7073","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
1
+ {"version":3,"sources":["../src/core/palette.ts"],"sourcesContent":["/**\n * Palette - Color palette management\n */\n\nimport type { ColorHex, ColorRGB, PaletteData } from \"../types\";\n\n/** Matches a valid 6-digit hex color string, e.g. \"#1A2B3C\" */\nconst HEX_COLOR_REGEX = /^#[0-9A-Fa-f]{6}$/;\n\nexport interface PaletteOptions {\n\tcolors?: ColorHex[];\n\tname?: string;\n}\n\nexport class Palette {\n\tprivate colors: ColorHex[];\n\tprivate name: string;\n\tprivate rgbCache: Map<number, ColorRGB>;\n\tprivate runtime?: any;\n\n\tconstructor(options: PaletteOptions | PaletteData = {}, runtime?: any) {\n\t\tthis.runtime = runtime;\n\n\t\tif (\"colors\" in options && Array.isArray(options.colors)) {\n\t\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\t\tif (!this.validatePaletteFormat(options.colors)) {\n\t\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\t\tthis.colors = [];\n\t\t\t\tthis.name = \"Invalid\";\n\t\t\t} else {\n\t\t\t\tthis.colors = [...options.colors];\n\t\t\t\tthis.name = options.name || \"Custom\";\n\t\t\t}\n\t\t} else {\n\t\t\t// Initialize with empty palette when no valid colors provided\n\t\t\tthis.colors = [];\n\t\t\tthis.name = \"Empty\";\n\t\t}\n\n\t\tthis.rgbCache = new Map();\n\t}\n\n\t/**\n\t * Validate palette format\n\t */\n\tprivate validatePaletteFormat(colors: any[]): boolean {\n\t\tif (!Array.isArray(colors)) return false;\n\t\treturn colors.every((color) => typeof color === \"string\" && HEX_COLOR_REGEX.test(color));\n\t}\n\n\t/**\n\t * Get color by index\n\t */\n\tget(index: number): ColorHex {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\tif (this.colors.length === 0) {\n\t\t\treturn \"#000000\";\n\t\t}\n\n\t\treturn this.colors[index % this.colors.length] || \"#000000\";\n\t}\n\n\t/**\n\t * Get color as RGB object\n\t */\n\tgetRGB(index: number): ColorRGB {\n\t\tif (this.rgbCache.has(index)) {\n\t\t\treturn this.rgbCache.get(index)!;\n\t\t}\n\n\t\tconst hex = this.get(index);\n\t\tconst rgb = this.hexToRGB(hex);\n\t\tthis.rgbCache.set(index, rgb);\n\t\treturn rgb;\n\t}\n\n\t/**\n\t * Get all colors\n\t */\n\tgetAll(): ColorHex[] {\n\t\treturn [...this.colors];\n\t}\n\n\t/**\n\t * Get palette size\n\t */\n\tget size(): number {\n\t\treturn this.colors.length;\n\t}\n\n\t/**\n\t * Get palette name\n\t */\n\tget paletteName(): string {\n\t\treturn this.name;\n\t}\n\n\t/**\n\t * Set color at index (expands palette if needed)\n\t */\n\tset(index: number, color: ColorHex): void {\n\t\t// Validate color index is a finite, non-negative number\n\t\tif (!isFinite(index) || index < 0) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7073\", message: \"Invalid color index\", data: { index, maxIndex: this.colors.length - 1 } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate color format matches hex pattern (#RRGGBB)\n\t\tif (!HEX_COLOR_REGEX.test(color)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid color format\", data: { format: color } });\n\t\t\treturn;\n\t\t}\n\n\t\t// Expand palette with black (#000000) if index exceeds current size\n\t\twhile (this.colors.length <= index) {\n\t\t\tthis.colors.push(\"#000000\");\n\t\t}\n\t\tthis.colors[index] = color;\n\t\tthis.rgbCache.delete(index);\n\t}\n\n\t/**\n\t * Add color to palette\n\t */\n\tadd(color: ColorHex): number {\n\t\tthis.colors.push(color);\n\t\treturn this.colors.length - 1;\n\t}\n\n\t/**\n\t * Remove color at index\n\t */\n\tremove(index: number): void {\n\t\tif (index >= 0 && index < this.colors.length) {\n\t\t\tthis.colors.splice(index, 1);\n\t\t\tthis.rgbCache.clear();\n\t\t}\n\t}\n\n\t/**\n\t * Replace entire palette\n\t */\n\tsetPalette(colors: ColorHex[]): void {\n\t\t// Validate palette format to ensure all colors are valid hex strings\n\t\tif (!this.validatePaletteFormat(colors)) {\n\t\t\tthis.runtime?.listener?.reportError?.({ code: \"E7072\", message: \"Invalid palette format\", data: { format: \"invalid color array\" } });\n\t\t\treturn;\n\t\t}\n\n\t\tthis.colors = [...colors];\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Reset to original colors\n\t */\n\treset(paletteData?: PaletteData): void {\n\t\tif (paletteData) {\n\t\t\tthis.colors = [...paletteData.colors];\n\t\t\tthis.name = paletteData.name;\n\t\t}\n\t\tthis.rgbCache.clear();\n\t}\n\n\t/**\n\t * Convert hex to RGB\n\t */\n\tprivate hexToRGB(hex: ColorHex): ColorRGB {\n\t\tconst result = /^#?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i.exec(hex);\n\t\treturn result\n\t\t\t? {\n\t\t\t\t\tr: Number.parseInt(result[1], 16),\n\t\t\t\t\tg: Number.parseInt(result[2], 16),\n\t\t\t\t\tb: Number.parseInt(result[3], 16),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tr: 0,\n\t\t\t\t\tg: 0,\n\t\t\t\t\tb: 0,\n\t\t\t\t};\n\t}\n\n\t/**\n\t * Convert RGB to hex\n\t */\n\tstatic rgbToHex(r: number, g: number, b: number): ColorHex {\n\t\treturn \"#\" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();\n\t}\n\n\t/**\n\t * Find closest color in palette\n\t */\n\tfindClosest(targetHex: ColorHex): number {\n\t\tconst target = this.hexToRGB(targetHex);\n\t\tlet closestIndex = 0;\n\t\tlet closestDistance = Number.POSITIVE_INFINITY;\n\n\t\tfor (let i = 0; i < this.colors.length; i++) {\n\t\t\tconst color = this.getRGB(i);\n\t\t\tconst distance = (target.r - color.r) ** 2 + (target.g - color.g) ** 2 + (target.b - color.b) ** 2;\n\n\t\t\tif (distance < closestDistance) {\n\t\t\t\tclosestDistance = distance;\n\t\t\t\tclosestIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn closestIndex;\n\t}\n\n\t/**\n\t * Create a gradient between two palette colors\n\t */\n\tgradient(startIndex: number, endIndex: number, steps: number): ColorHex[] {\n\t\tconst start = this.getRGB(startIndex);\n\t\tconst end = this.getRGB(endIndex);\n\t\tconst gradient: ColorHex[] = [];\n\n\t\tfor (let i = 0; i < steps; i++) {\n\t\t\tconst t = i / (steps - 1);\n\t\t\tconst r = Math.round(start.r + (end.r - start.r) * t);\n\t\t\tconst g = Math.round(start.g + (end.g - start.g) * t);\n\t\t\tconst b = Math.round(start.b + (end.b - start.b) * t);\n\t\t\tgradient.push(Palette.rgbToHex(r, g, b));\n\t\t}\n\n\t\treturn gradient;\n\t}\n\n\t/**\n\t * Lighten a color\n\t */\n\tlighten(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.min(255, Math.round(color.r + (255 - color.r) * amount));\n\t\tconst g = Math.min(255, Math.round(color.g + (255 - color.g) * amount));\n\t\tconst b = Math.min(255, Math.round(color.b + (255 - color.b) * amount));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Darken a color\n\t */\n\tdarken(index: number, amount: number = 0.2): ColorHex {\n\t\tconst color = this.getRGB(index);\n\t\tconst r = Math.max(0, Math.round(color.r * (1 - amount)));\n\t\tconst g = Math.max(0, Math.round(color.g * (1 - amount)));\n\t\tconst b = Math.max(0, Math.round(color.b * (1 - amount)));\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Mix two colors\n\t */\n\tmix(index1: number, index2: number, ratio: number = 0.5): ColorHex {\n\t\tconst color1 = this.getRGB(index1);\n\t\tconst color2 = this.getRGB(index2);\n\t\tconst r = Math.round(color1.r + (color2.r - color1.r) * ratio);\n\t\tconst g = Math.round(color1.g + (color2.g - color1.g) * ratio);\n\t\tconst b = Math.round(color1.b + (color2.b - color1.b) * ratio);\n\t\treturn Palette.rgbToHex(r, g, b);\n\t}\n\n\t/**\n\t * Export palette data\n\t */\n\ttoData(): PaletteData {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tcolors: this.getAll(),\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAOA,IAAMA,kBAAkB;AAOjB,IAAMC,UAAN,MAAMA,SAAAA;EAdb,OAcaA;;;EACJC;EACAC;EACAC;EACAC;EAER,YAAYC,UAAwC,CAAC,GAAGD,SAAe;AACtE,SAAKA,UAAUA;AAEf,QAAI,YAAYC,WAAWC,MAAMC,QAAQF,QAAQJ,MAAM,GAAG;AAEzD,UAAI,CAAC,KAAKO,sBAAsBH,QAAQJ,MAAM,GAAG;AAChD,aAAKG,SAASK,UAAUC,cAAc;UAAEC,MAAM;UAASC,SAAS;UAA0BC,MAAM;YAAEC,QAAQ;UAAsB;QAAE,CAAA;AAClI,aAAKb,SAAS,CAAA;AACd,aAAKC,OAAO;MACb,OAAO;AACN,aAAKD,SAAS;aAAII,QAAQJ;;AAC1B,aAAKC,OAAOG,QAAQH,QAAQ;MAC7B;IACD,OAAO;AAEN,WAAKD,SAAS,CAAA;AACd,WAAKC,OAAO;IACb;AAEA,SAAKC,WAA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xD,UAAM/B,IAAIqB,KAAKC,MAAMU,OAAOhC,KAAKiC,OAAOjC,IAAIgC,OAAOhC,KAAK+B,KAAAA;AACxD,UAAM9B,IAAIoB,KAAKC,MAAMU,OAAO/B,KAAKgC,OAAOhC,IAAI+B,OAAO/B,KAAK8B,KAAAA;AACxD,WAAO9E,SAAQiD,SAASL,GAAGG,GAAGC,CAAAA;EAC/B;;;;EAKAiC,SAAsB;AACrB,WAAO;MACN/E,MAAM,KAAKA;MACXD,QAAQ,KAAK6B,OAAM;IACpB;EACD;AACD;","names":["HEX_COLOR_REGEX","Palette","colors","name","rgbCache","runtime","options","Array","isArray","validatePaletteFormat","listener","reportError","code","message","data","format","Map","every","color","test","get","index","isFinite","maxIndex","length","getRGB","has","hex","rgb","hexToRGB","set","getAll","size","paletteName","push","delete","add","remove","splice","clear","setPalette","reset","paletteData","result","exec","r","Number","parseInt","g","b","rgbToHex","toString","slice","toUpperCase","findClosest","targetHex","target","closestIndex","closestDistance","POSITIVE_INFINITY","i","distance","gradient","startIndex","endIndex","steps","start","end","t","Math","round","lighten","amount","min","darken","max","mix","index1","index2","ratio","color1","color2","toData"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@al8b/palette",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "sideEffects": false,
5
5
  "files": [
6
6
  "dist/**/*",
@@ -22,9 +22,7 @@
22
22
  "require": "./dist/index.js"
23
23
  }
24
24
  },
25
- "dependencies": {
26
- "@al8b/diagnostics": "^0.1.13"
27
- },
25
+ "dependencies": {},
28
26
  "keywords": [
29
27
  "palette"
30
28
  ],