@mlightcad/common 1.2.5 → 1.2.7

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.
package/lib/AcCmColor.js CHANGED
@@ -1,24 +1,3 @@
1
- /**
2
- * @fileoverview This module provides color handling functionality for AutoCAD files,
3
- * including color representation, color name mapping, and color index conversion.
4
- *
5
- * The module supports various color formats including RGB values, named colors,
6
- * AutoCAD color indices, and special values like "ByLayer" and "ByBlock".
7
- *
8
- * @module AcCmColor
9
- * @version 1.0.0
10
- */
11
- var __values = (this && this.__values) || function(o) {
12
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
13
- if (m) return m.call(o);
14
- if (o && typeof o.length === "number") return {
15
- next: function () {
16
- if (o && i >= o.length) o = void 0;
17
- return { value: o && o[i++], done: !o };
18
- }
19
- };
20
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
21
- };
22
1
  var __read = (this && this.__read) || function (o, n) {
23
2
  var m = typeof Symbol === "function" && o[Symbol.iterator];
24
3
  if (!m) return o;
@@ -35,606 +14,490 @@ var __read = (this && this.__read) || function (o, n) {
35
14
  }
36
15
  return ar;
37
16
  };
17
+ import { AcCmColorMethod } from './AcCmColorMethod';
18
+ import { AcCmColorUtil } from './AcCmColorUtil';
38
19
  /**
39
- * Mapping of CSS color names to their corresponding RGB values.
40
- * These are standard web color names that can be used for color representation.
41
- *
42
- * @internal
43
- */
44
- var _colorKeywords = {
45
- aliceblue: 0xf0f8ff,
46
- antiquewhite: 0xfaebd7,
47
- aqua: 0x00ffff,
48
- aquamarine: 0x7fffd4,
49
- azure: 0xf0ffff,
50
- beige: 0xf5f5dc,
51
- bisque: 0xffe4c4,
52
- black: 0x000000,
53
- blanchedalmond: 0xffebcd,
54
- blue: 0x0000ff,
55
- blueviolet: 0x8a2be2,
56
- brown: 0xa52a2a,
57
- burlywood: 0xdeb887,
58
- cadetblue: 0x5f9ea0,
59
- chartreuse: 0x7fff00,
60
- chocolate: 0xd2691e,
61
- coral: 0xff7f50,
62
- cornflowerblue: 0x6495ed,
63
- cornsilk: 0xfff8dc,
64
- crimson: 0xdc143c,
65
- cyan: 0x00ffff,
66
- darkblue: 0x00008b,
67
- darkcyan: 0x008b8b,
68
- darkgoldenrod: 0xb8860b,
69
- darkgray: 0xa9a9a9,
70
- darkgreen: 0x006400,
71
- darkkhaki: 0xbdb76b,
72
- darkmagenta: 0x8b008b,
73
- darkolivegreen: 0x556b2f,
74
- darkorange: 0xff8c00,
75
- darkorchid: 0x9932cc,
76
- darkred: 0x8b0000,
77
- darksalmon: 0xe9967a,
78
- darkseagreen: 0x8fbc8f,
79
- darkslateblue: 0x483d8b,
80
- darkslategray: 0x2f4f4f,
81
- darkturquoise: 0x00ced1,
82
- darkviolet: 0x9400d3,
83
- deeppink: 0xff1493,
84
- deepskyblue: 0x00bfff,
85
- dimgrey: 0x696969,
86
- dodgerblue: 0x1e90ff,
87
- firebrick: 0xb22222,
88
- floralwhite: 0xfffaf0,
89
- forestgreen: 0x228b22,
90
- fuchsia: 0xff00ff,
91
- gainsboro: 0xdcdcdc,
92
- ghostwhite: 0xf8f8ff,
93
- gold: 0xffd700,
94
- goldenrod: 0xdaa520,
95
- gray: 0x808080,
96
- green: 0x008000,
97
- greenyellow: 0xadff2f,
98
- grey: 0x808080,
99
- honeydew: 0xf0fff0,
100
- hotpink: 0xff69b4,
101
- indianred: 0xcd5c5c,
102
- indigo: 0x4b0082,
103
- ivory: 0xfffff0,
104
- khaki: 0xf0e68c,
105
- lavender: 0xe6e6fa,
106
- lavenderblush: 0xfff0f5,
107
- lawngreen: 0x7cfc00,
108
- lemonchiffon: 0xfffacd,
109
- lightblue: 0xadd8e6,
110
- lightcoral: 0xf08080,
111
- lightcyan: 0xe0ffff,
112
- lightgoldenrodyellow: 0xfafad2,
113
- lightgray: 0xd3d3d3,
114
- lightgreen: 0x90ee90,
115
- lightgrey: 0xd3d3d3,
116
- lightpink: 0xffb6c1,
117
- lightsalmon: 0xffa07a,
118
- lightseagreen: 0x20b2aa,
119
- lightskyblue: 0x87cefa,
120
- lightslategray: 0x778899,
121
- lightslategrey: 0x778899,
122
- lightsteelblue: 0xb0c4de,
123
- lightyellow: 0xffffe0,
124
- lime: 0x00ff00,
125
- limegreen: 0x32cd32,
126
- linen: 0xfaf0e6,
127
- magenta: 0xff00ff,
128
- maroon: 0x800000,
129
- mediumaquamarine: 0x66cdaa,
130
- mediumblue: 0x0000cd,
131
- mediumorchid: 0xba55d3,
132
- mediumpurple: 0x9370db,
133
- mediumseagreen: 0x3cb371,
134
- mediumslateblue: 0x7b68ee,
135
- mediumspringgreen: 0x00fa9a,
136
- mediumturquoise: 0x48d1cc,
137
- mediumvioletred: 0xc71585,
138
- midnightblue: 0x191970,
139
- mintcream: 0xf5fffa,
140
- mistyrose: 0xffe4e1,
141
- moccasin: 0xffe4b5,
142
- navajowhite: 0xffdead,
143
- navy: 0x000080,
144
- oldlace: 0xfdf5e6,
145
- olive: 0x808000,
146
- olivedrab: 0x6b8e23,
147
- orange: 0xffa500,
148
- orangered: 0xff4500,
149
- orchid: 0xda70d6,
150
- palegoldenrod: 0xeee8aa,
151
- palegreen: 0x98fb98,
152
- paleturquoise: 0xafeeee,
153
- palevioletred: 0xdb7093,
154
- papayawhip: 0xffefd5,
155
- peachpuff: 0xffdab9,
156
- peru: 0xcd853f,
157
- pink: 0xffc0cb,
158
- plum: 0xdda0dd,
159
- powderblue: 0xb0e0e6,
160
- purple: 0x800080,
161
- rebeccapurple: 0x663399,
162
- red: 0xff0000,
163
- rosybrown: 0xbc8f8f,
164
- royalblue: 0x4169e1,
165
- saddlebrown: 0x8b4513,
166
- salmon: 0xfa8072,
167
- sandybrown: 0xf4a460,
168
- seagreen: 0x2e8b57,
169
- seashell: 0xfff5ee,
170
- sienna: 0xa0522d,
171
- silver: 0xc0c0c0,
172
- skyblue: 0x87ceeb,
173
- slateblue: 0x6a5acd,
174
- slategrey: 0x708090,
175
- snow: 0xfffafa,
176
- springgreen: 0x00ff7f,
177
- steelblue: 0x4682b4,
178
- tan: 0xd2b48c,
179
- teal: 0x008080,
180
- thistle: 0xd8bfd8,
181
- tomato: 0xff6347,
182
- turquoise: 0x40e0d0,
183
- violet: 0xee82ee,
184
- wheat: 0xf5deb3,
185
- white: 0xffffff,
186
- whitesmoke: 0xf5f5f5,
187
- yellow: 0xffff00,
188
- yellowgreen: 0x9acd32
189
- };
190
- /**
191
- * AutoCAD color index array mapping index values (1-255) to RGB color values.
192
- * Each value corresponds to a color. Index 1 is red, that is 16711680 or 0xFF0000.
193
- * Index 0 and 256, while included in this array, are actually reserved for inheritance
194
- * values in AutoCAD so they should not be used for index color lookups:
195
- * - Index 0: "ByBlock" - entity uses color of the block reference
196
- * - Index 256: "ByLayer" - entity uses color specified in the layer
197
- *
198
- * @internal
199
- */
200
- var AUTO_CAD_COLOR_INDEX = [
201
- 0, 16711680, 16776960, 65280, 65535, 255, 16711935, 16777215, 8421504,
202
- 12632256, 16711680, 16744319, 13369344, 13395558, 10027008, 10046540, 8323072,
203
- 8339263, 4980736, 4990502, 16727808, 16752511, 13382400, 13401958, 10036736,
204
- 10051404, 8331008, 8343359, 4985600, 4992806, 16744192, 16760703, 13395456,
205
- 13408614, 10046464, 10056268, 8339200, 8347455, 4990464, 4995366, 16760576,
206
- 16768895, 13408512, 13415014, 10056192, 10061132, 8347392, 8351551, 4995328,
207
- 4997670, 16776960, 16777087, 13421568, 13421670, 10000384, 10000460, 8355584,
208
- 8355647, 5000192, 5000230, 12582656, 14679935, 10079232, 11717734, 7510016,
209
- 8755276, 6258432, 7307071, 3755008, 4344870, 8388352, 12582783, 6736896,
210
- 10079334, 5019648, 7510092, 4161280, 6258495, 2509824, 3755046, 4194048,
211
- 10485631, 3394560, 8375398, 2529280, 6264908, 2064128, 5209919, 1264640,
212
- 3099686, 65280, 8388479, 52224, 6736998, 38912, 5019724, 32512, 4161343,
213
- 19456, 2509862, 65343, 8388511, 52275, 6737023, 38950, 5019743, 32543,
214
- 4161359, 19475, 2509871, 65407, 8388543, 52326, 6737049, 38988, 5019762,
215
- 32575, 4161375, 19494, 2509881, 65471, 8388575, 52377, 6737074, 39026,
216
- 5019781, 32607, 4161391, 19513, 2509890, 65535, 8388607, 52428, 6737100,
217
- 39064, 5019800, 32639, 4161407, 19532, 2509900, 49151, 8380415, 39372,
218
- 6730444, 29336, 5014936, 24447, 4157311, 14668, 2507340, 32767, 8372223,
219
- 26316, 6724044, 19608, 5010072, 16255, 4153215, 9804, 2505036, 16383, 8364031,
220
- 13260, 6717388, 9880, 5005208, 8063, 4149119, 4940, 2502476, 255, 8355839,
221
- 204, 6710988, 152, 5000344, 127, 4145023, 76, 2500172, 4129023, 10452991,
222
- 3342540, 8349388, 2490520, 6245528, 2031743, 5193599, 1245260, 3089996,
223
- 8323327, 12550143, 6684876, 10053324, 4980888, 7490712, 4128895, 6242175,
224
- 2490444, 3745356, 12517631, 14647295, 10027212, 11691724, 7471256, 8735896,
225
- 6226047, 7290751, 3735628, 4335180, 16711935, 16744447, 13369548, 13395660,
226
- 9961624, 9981080, 8323199, 8339327, 4980812, 4990540, 16711871, 16744415,
227
- 13369497, 13395634, 9961586, 9981061, 8323167, 8339311, 4980793, 4990530,
228
- 16711807, 16744383, 13369446, 13395609, 9961548, 9981042, 8323135, 8339295,
229
- 4980774, 4990521, 16711743, 16744351, 13369395, 13395583, 9961510, 9981023,
230
- 8323103, 8339279, 4980755, 4990511, 3355443, 5987163, 8684676, 11382189,
231
- 14079702, 16777215, 0
232
- ];
233
- /**
234
- * Clamps a value between a minimum and maximum value.
235
- *
236
- * @param {number} value - The value to clamp.
237
- * @param {number} min - The minimum value.
238
- * @param {number} max - The maximum value.
239
- * @returns {number} The clamped value.
240
- * @internal
241
- */
242
- function clamp(value, min, max) {
243
- return Math.max(min, Math.min(max, value));
244
- }
245
- /**
246
- * Represents a color in AutoCAD, supporting various color formats.
247
- *
248
- * @class AcCmColor
249
- * @version 1.0.0
20
+ * Represents an AutoCAD color. This class supports color methods:
21
+ * - ByColor: explicit RGB
22
+ * - ByACI: AutoCAD Color Index (0-256)
23
+ * - ByLayer: color inherited from layer
24
+ * - ByBlock: color inherited from block
250
25
  */
251
26
  var AcCmColor = /** @class */ (function () {
252
27
  /**
253
- * Constructs a new AcCmColor instance.
254
- * Initializes the color to "ByLayer" (index 256) and null RGB values.
28
+ * Constructs a new AcCmColor.
29
+ * @param method Initial color method (defaults to `ByColor`)
30
+ * @param value Internal packed value
255
31
  */
256
- function AcCmColor() {
257
- this._colorIndex = 256;
258
- this._color = null;
259
- this._colorName = null;
32
+ function AcCmColor(method, value) {
33
+ if (method === void 0) { method = AcCmColorMethod.ByLayer; }
34
+ this._colorMethod = method;
35
+ if (this._colorMethod == AcCmColorMethod.ByColor && value == null) {
36
+ this._value = 0xffffff;
37
+ }
38
+ else if (this._colorMethod == AcCmColorMethod.ByACI) {
39
+ if (value == null) {
40
+ this._value = 8;
41
+ }
42
+ else if (value === 0) {
43
+ this._colorMethod = AcCmColorMethod.ByBlock;
44
+ }
45
+ else if (value === 256) {
46
+ this._colorMethod = AcCmColorMethod.ByLayer;
47
+ }
48
+ else {
49
+ this._value = Math.max(0, Math.min(value, 256));
50
+ }
51
+ }
52
+ else {
53
+ this._value = value;
54
+ }
260
55
  }
261
- Object.defineProperty(AcCmColor.prototype, "color", {
262
- /**
263
- * Gets the current color value.
264
- *
265
- * @returns {number | null} The color value (RGB or index).
266
- */
56
+ Object.defineProperty(AcCmColor.prototype, "colorMethod", {
57
+ // ---------------------------------------------------------------------
58
+ // Color method
59
+ // ---------------------------------------------------------------------
60
+ /** Gets the current color method. */
267
61
  get: function () {
268
- return this._color;
62
+ return this._colorMethod;
269
63
  },
270
64
  /**
271
- * Sets the color value.
65
+ * Sets the color method.
272
66
  *
273
- * @param {number | null} value - The color value to set (RGB or index).
67
+ * Note: Changing the method does not modify `_value`.
274
68
  */
275
- set: function (value) {
276
- if (value == null) {
277
- this._color = null;
278
- }
279
- else {
280
- this._color = Math.round(clamp(value, 0, 256 * 256 * 256 - 1));
281
- this._colorIndex = this.getColorIndexByValue(this._color);
282
- this._colorName = this.getColorNameByValue(this._color);
283
- }
69
+ set: function (method) {
70
+ this._colorMethod = method;
284
71
  },
285
72
  enumerable: false,
286
73
  configurable: true
287
74
  });
288
- Object.defineProperty(AcCmColor.prototype, "hexColor", {
289
- /**
290
- * Gets the hexadecimal representation of the current color.
291
- *
292
- * @returns {string} The hexadecimal color string (e.g., "0xFFFFFF").
293
- */
75
+ Object.defineProperty(AcCmColor.prototype, "red", {
76
+ // ---------------------------------------------------------------------
77
+ // RGB accessors (ByColor)
78
+ // ---------------------------------------------------------------------
79
+ /** Gets the red component (0–255). */
294
80
  get: function () {
295
- // Ensure the number is a valid RGB value
296
- if (this._color && this._color > 0 && this._color <= 0xffffff) {
297
- var hexString = this._color.toString(16).toUpperCase();
298
- // Pad the string with leading zeros if necessary
299
- while (hexString.length < 6) {
300
- hexString = '0' + hexString;
301
- }
302
- return "0x".concat(hexString);
303
- }
304
- return '';
81
+ var rgb = this.RGB;
82
+ return rgb != null ? (rgb >> 16) & 0xff : undefined;
305
83
  },
306
84
  enumerable: false,
307
85
  configurable: true
308
86
  });
309
- Object.defineProperty(AcCmColor.prototype, "cssColor", {
310
- /**
311
- * Gets the CSS RGB color string representation.
312
- *
313
- * @returns {string} The CSS RGB color string (e.g., "rgb(255,255,255)").
314
- */
87
+ Object.defineProperty(AcCmColor.prototype, "green", {
88
+ /** Gets the green component (0–255). */
315
89
  get: function () {
316
- return "rgb(".concat(this.red, ",").concat(this.green, ",").concat(this.blue, ")");
90
+ var rgb = this.RGB;
91
+ return rgb != null ? (rgb >> 8) & 0xff : undefined;
317
92
  },
318
93
  enumerable: false,
319
94
  configurable: true
320
95
  });
321
- Object.defineProperty(AcCmColor.prototype, "red", {
322
- /**
323
- * Gets the red component of the current color.
324
- *
325
- * @returns {number | null} The red component (0-255).
326
- */
96
+ Object.defineProperty(AcCmColor.prototype, "blue", {
97
+ /** Gets the blue component (0–255). */
327
98
  get: function () {
328
- return this.color ? (this.color >> 16) & 255 : null;
99
+ var rgb = this.RGB;
100
+ return rgb != null ? rgb & 0xff : undefined;
329
101
  },
330
102
  enumerable: false,
331
103
  configurable: true
332
104
  });
333
- Object.defineProperty(AcCmColor.prototype, "green", {
105
+ Object.defineProperty(AcCmColor.prototype, "RGB", {
334
106
  /**
335
- * Gets the green component of the current color.
107
+ * Gets the packed RGB value (0xRRGGBB).
336
108
  *
337
- * @returns {number | null} The green component (0-255).
109
+ * - For `ByColor`, returns `_value` directly
110
+ * - For `ByACI`, converts index to RGB via `AcCmColorUtil`
111
+ * - For `ByLayer` or `ByBlock`, returns `_value` directly
338
112
  */
339
113
  get: function () {
340
- return this.color ? (this.color >> 8) & 255 : null;
114
+ switch (this._colorMethod) {
115
+ case AcCmColorMethod.ByColor:
116
+ case AcCmColorMethod.ByBlock:
117
+ case AcCmColorMethod.ByLayer:
118
+ return this._value;
119
+ case AcCmColorMethod.ByACI:
120
+ return this._value
121
+ ? AcCmColorUtil.getColorByIndex(this._value)
122
+ : this._value;
123
+ default:
124
+ return undefined;
125
+ }
341
126
  },
342
127
  enumerable: false,
343
128
  configurable: true
344
129
  });
345
- Object.defineProperty(AcCmColor.prototype, "blue", {
130
+ /**
131
+ * Sets the RGB color.
132
+ *
133
+ * @param r Red component (0–255)
134
+ * @param g Green component (0–255)
135
+ * @param b Blue component (0–255)
136
+ * @returns The current instance for chaining
137
+ */
138
+ AcCmColor.prototype.setRGB = function (r, g, b) {
139
+ var red = Math.max(0, Math.min(255, Math.round(r)));
140
+ var green = Math.max(0, Math.min(255, Math.round(g)));
141
+ var blue = Math.max(0, Math.min(255, Math.round(b)));
142
+ this._value = (red << 16) | (green << 8) | blue;
143
+ this._colorMethod = AcCmColorMethod.ByColor;
144
+ return this;
145
+ };
146
+ /**
147
+ * Sets the RGB color by a single packed number (0xRRGGBB).
148
+ *
149
+ * @param value Packed RGB number
150
+ */
151
+ AcCmColor.prototype.setRGBValue = function (value) {
152
+ if (value == null || !Number.isFinite(value)) {
153
+ console.warn('Invalid RGB value:', value);
154
+ return this;
155
+ }
156
+ this._value = value & 0xffffff;
157
+ this._colorMethod = AcCmColorMethod.ByColor;
158
+ return this;
159
+ };
160
+ /**
161
+ * Sets the RGB color from a CSS color string.
162
+ *
163
+ * Examples:
164
+ * - "#FF00FF"
165
+ * - "#F0F"
166
+ * - "rgb(255,0,255)"
167
+ * - "rgba(255,0,255,0.5)"
168
+ * - "red" (named colors)
169
+ *
170
+ * @param cssString CSS color string
171
+ * @returns The current instance for chaining
172
+ */
173
+ AcCmColor.prototype.setRGBFromCss = function (cssString) {
174
+ if (!cssString)
175
+ return this;
176
+ var s = cssString.trim().toLowerCase();
177
+ // Hex: #RRGGBB or #RGB
178
+ if (s.startsWith('#')) {
179
+ var r = 0, g = 0, b = 0;
180
+ if (s.length === 7) {
181
+ r = parseInt(s.substr(1, 2), 16);
182
+ g = parseInt(s.substr(3, 2), 16);
183
+ b = parseInt(s.substr(5, 2), 16);
184
+ }
185
+ else if (s.length === 4) {
186
+ r = parseInt(s[1] + s[1], 16);
187
+ g = parseInt(s[2] + s[2], 16);
188
+ b = parseInt(s[3] + s[3], 16);
189
+ }
190
+ else {
191
+ console.warn('Invalid hex color:', cssString);
192
+ return this;
193
+ }
194
+ return this.setRGB(r, g, b);
195
+ }
196
+ // rgb() or rgba()
197
+ var rgbMatch = s.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/);
198
+ if (rgbMatch) {
199
+ var r = parseInt(rgbMatch[1], 10);
200
+ var g = parseInt(rgbMatch[2], 10);
201
+ var b = parseInt(rgbMatch[3], 10);
202
+ return this.setRGB(r, g, b);
203
+ }
204
+ // Named color
205
+ var namedColor = AcCmColorUtil.getColorByName(cssString);
206
+ if (namedColor !== undefined) {
207
+ return this.setRGBValue(namedColor);
208
+ }
209
+ console.warn('Unknown CSS color string:', cssString);
210
+ return this;
211
+ };
212
+ /**
213
+ * Sets the color as a scalar grayscale value.
214
+ *
215
+ * @param scalar Scalar value (0–255)
216
+ * @returns The current instance for chaining
217
+ */
218
+ AcCmColor.prototype.setScalar = function (scalar) {
219
+ return this.setRGB(scalar, scalar, scalar);
220
+ };
221
+ Object.defineProperty(AcCmColor.prototype, "hexColor", {
346
222
  /**
347
- * Gets the blue component of the current color.
348
- *
349
- * @returns {number | null} The blue component (0-255).
223
+ * Gets the hexadecimal representation of the color (e.g., "0xFF00FF").
350
224
  */
351
225
  get: function () {
352
- return this.color ? this.color & 255 : null;
226
+ var rgb = this.RGB;
227
+ if (rgb == null)
228
+ return undefined;
229
+ // Convert to hex and ensure always 6 digits
230
+ var hex = rgb.toString(16).padStart(6, '0').toUpperCase();
231
+ return '0x' + hex;
353
232
  },
354
233
  enumerable: false,
355
234
  configurable: true
356
235
  });
357
- Object.defineProperty(AcCmColor.prototype, "colorIndex", {
236
+ Object.defineProperty(AcCmColor.prototype, "cssColor", {
358
237
  /**
359
- * Gets the AutoCAD color index value. The index value will be in the range 0 to 256.
360
- * 0 and 256 are special values.
361
- *
362
- * @returns {number | null} The color index.
238
+ * Gets the CSS RGB color string (e.g., "rgb(255,0,255)").
363
239
  */
364
240
  get: function () {
365
- return this._colorIndex;
366
- },
367
- /**
368
- * Sets the AutoCAD color index value. If value less than 0 is set, 0 will be used. If value greater than
369
- * 256 is set, 256 will be used.
370
- * - 0 indicates that the entity uses the color of the BlockReference that's displaying it. If the entity
371
- * is not displayed through a block reference (for example, it is directly owned by the model space
372
- * block table record) and its color is 0, then it will display as though its color were 7.
373
- * - 256 indicates that the entity uses the color specified in the layer table record it references.
374
- *
375
- * @param {number | null} value - The color index to set (0-256).
376
- */
377
- set: function (value) {
378
- if (value == null) {
379
- this._colorIndex = null;
380
- }
381
- else {
382
- this._colorIndex = clamp(value, 0, 256);
383
- if (this._colorIndex > 0 && this._colorIndex < 256) {
384
- this._color = AUTO_CAD_COLOR_INDEX[value];
385
- this._colorName = this.getColorNameByValue(this._color);
386
- }
387
- else {
388
- // If it is ByBlock and ByLayer, value of color and colorName can't be inferenced.
389
- this._color = null;
390
- this._colorName = null;
391
- }
392
- }
241
+ var rgb = this.RGB;
242
+ if (rgb == null)
243
+ return undefined;
244
+ return "rgb(".concat((rgb >> 16) & 0xff, ",").concat((rgb >> 8) & 0xff, ",").concat(rgb & 0xff, ")");
393
245
  },
394
246
  enumerable: false,
395
247
  configurable: true
396
248
  });
397
- Object.defineProperty(AcCmColor.prototype, "colorName", {
398
- /**
399
- * Gets the name of the current color.
400
- *
401
- * @returns {string | null} The color name.
402
- */
249
+ Object.defineProperty(AcCmColor.prototype, "colorIndex", {
250
+ // ---------------------------------------------------------------------
251
+ // ACI accessors (ByACI)
252
+ // ---------------------------------------------------------------------
253
+ /** Gets the AutoCAD Color Index (ACI), or undefined if not ByACI, ByBlock, or ByLayer. */
403
254
  get: function () {
404
- return this._colorName;
255
+ if (this._colorMethod === AcCmColorMethod.ByACI)
256
+ return this._value;
257
+ else if (this._colorMethod === AcCmColorMethod.ByLayer)
258
+ return 256;
259
+ else if (this._colorMethod === AcCmColorMethod.ByBlock)
260
+ return 0;
261
+ else
262
+ return undefined;
405
263
  },
406
264
  /**
407
- * Sets the color by name.
265
+ * Sets the AutoCAD Color Index (0–256).
266
+ *
267
+ * - 0 sets the color method to `ByBlock`
268
+ * - 256 sets the color method to `ByLayer`
269
+ * - 1–255 sets the color method to `ByACI`
408
270
  *
409
- * @param {string | null} value - The color name to set.
271
+ * @param index ACI index
410
272
  */
411
- set: function (value) {
412
- if (value) {
413
- var color = _colorKeywords[value.toLowerCase()];
414
- if (color !== undefined) {
415
- // red
416
- this._colorName = value;
417
- this._color = color;
418
- this._colorIndex = this.getColorIndexByValue(this._color);
419
- }
420
- else {
421
- // unknown color
422
- console.warn('Unknown color: ' + value);
423
- }
273
+ set: function (index) {
274
+ if (index == null)
275
+ return;
276
+ var clamped = Math.max(0, Math.min(256, Math.round(index)));
277
+ if (clamped === 0) {
278
+ this._colorMethod = AcCmColorMethod.ByBlock;
279
+ this._value = undefined;
280
+ }
281
+ else if (clamped === 256) {
282
+ this._colorMethod = AcCmColorMethod.ByLayer;
283
+ this._value = undefined;
424
284
  }
425
285
  else {
426
- this._colorName = null;
286
+ this._colorMethod = AcCmColorMethod.ByACI;
287
+ this._value = clamped;
427
288
  }
428
289
  },
429
290
  enumerable: false,
430
291
  configurable: true
431
292
  });
432
- Object.defineProperty(AcCmColor.prototype, "hasColorName", {
293
+ Object.defineProperty(AcCmColor.prototype, "isByColor", {
433
294
  /**
434
- * Checks if the color has a name.
435
- *
436
- * @returns {boolean} True if the color has no name, false otherwise.
295
+ * Returns true if the color method is ByColor (explicit RGB).
437
296
  */
438
297
  get: function () {
439
- return this._colorName == null;
298
+ return this._colorMethod === AcCmColorMethod.ByColor;
440
299
  },
441
300
  enumerable: false,
442
301
  configurable: true
443
302
  });
444
- Object.defineProperty(AcCmColor.prototype, "hasColorIndex", {
303
+ Object.defineProperty(AcCmColor.prototype, "isByACI", {
445
304
  /**
446
- * Checks if the color has an index.
447
- *
448
- * @returns {boolean} True if the color has no index, false otherwise.
305
+ * Returns true if the color method is ByACI.
449
306
  */
450
307
  get: function () {
451
- return this._colorIndex == null;
308
+ return this._colorMethod === AcCmColorMethod.ByACI;
452
309
  },
453
310
  enumerable: false,
454
311
  configurable: true
455
312
  });
456
313
  Object.defineProperty(AcCmColor.prototype, "isByLayer", {
457
- /**
458
- * Checks if the color is set to "ByLayer".
459
- *
460
- * @returns {boolean} True if the color is "ByLayer", false otherwise.
461
- */
314
+ // ---------------------------------------------------------------------
315
+ // Layer / Block helpers
316
+ // ---------------------------------------------------------------------
317
+ /** Returns true if the color method is ByLayer. */
462
318
  get: function () {
463
- return this.colorIndex == 256;
319
+ return this._colorMethod === AcCmColorMethod.ByLayer;
464
320
  },
465
321
  enumerable: false,
466
322
  configurable: true
467
323
  });
468
324
  /**
469
- * Sets the color to "ByLayer".
470
- *
471
- * @returns {AcCmColor} The current instance.
325
+ * Sets the color to ByLayer.
326
+ * @param value - Option layer color value
472
327
  */
473
- AcCmColor.prototype.setByLayer = function () {
474
- this.colorIndex = 256;
328
+ AcCmColor.prototype.setByLayer = function (value) {
329
+ this._colorMethod = AcCmColorMethod.ByLayer;
330
+ if (value == null) {
331
+ this._value = 256;
332
+ }
333
+ else {
334
+ this._value = value;
335
+ }
475
336
  return this;
476
337
  };
477
338
  Object.defineProperty(AcCmColor.prototype, "isByBlock", {
478
- /**
479
- * Checks if the color is set to "ByBlock".
480
- *
481
- * @returns {boolean} True if the color is "ByBlock", false otherwise.
482
- */
339
+ /** Returns true if the color method is ByBlock. */
483
340
  get: function () {
484
- return this.colorIndex == 0;
341
+ return this._colorMethod === AcCmColorMethod.ByBlock;
485
342
  },
486
343
  enumerable: false,
487
344
  configurable: true
488
345
  });
489
346
  /**
490
- * Sets the color to "ByBlock".
491
- *
492
- * @returns {AcCmColor} The current instance.
493
- */
494
- AcCmColor.prototype.setByBlock = function () {
495
- this.colorIndex = 0;
496
- return this;
497
- };
498
- /**
499
- * Sets the color using a scalar value (RGB).
500
- *
501
- * @param {number} scalar - The scalar value (0-255).
502
- * @returns {AcCmColor} The current instance.
503
- */
504
- AcCmColor.prototype.setScalar = function (scalar) {
505
- this.setRGB(scalar, scalar, scalar);
506
- return this;
507
- };
508
- /**
509
- * Sets the color using RGB values.
510
- *
511
- * @param {number} r - The red component (0-255).
512
- * @param {number} g - The green component (0-255).
513
- * @param {number} b - The blue component (0-255).
514
- * @returns {AcCmColor} The current instance.
515
- */
516
- AcCmColor.prototype.setRGB = function (r, g, b) {
517
- var red = Math.round(clamp(r, 0, 255));
518
- var green = Math.round(clamp(g, 0, 255));
519
- var blue = Math.round(clamp(b, 0, 255));
520
- this.color = (red << 16) + (green << 8) + blue;
521
- return this;
522
- };
523
- /**
524
- * Sets the color by name.
525
- *
526
- * @param {string} style - The color name to set.
527
- * @returns {AcCmColor} The current instance.
347
+ * Sets the color to ByBlock.
348
+ * @param value - Option layer color value
528
349
  */
529
- AcCmColor.prototype.setColorName = function (style) {
530
- var color = _colorKeywords[style.toLowerCase()];
531
- if (color !== undefined) {
532
- // red
533
- this.color = color;
350
+ AcCmColor.prototype.setByBlock = function (value) {
351
+ this._colorMethod = AcCmColorMethod.ByBlock;
352
+ if (value == null) {
353
+ this._value = 0;
534
354
  }
535
355
  else {
536
- // unknown color
537
- console.warn('Unknown color ' + style);
356
+ this._value = value;
538
357
  }
539
358
  return this;
540
359
  };
360
+ Object.defineProperty(AcCmColor.prototype, "colorName", {
361
+ // ---------------------------------------------------------------------
362
+ // Color name (dynamic)
363
+ // ---------------------------------------------------------------------
364
+ /**
365
+ * Gets the color name.
366
+ *
367
+ * For `ByColor` or `ByACI`, resolves the name via `AcCmColorUtil`.
368
+ * For `ByLayer` or `ByBlock`, returns the corresponding string.
369
+ */
370
+ get: function () {
371
+ switch (this._colorMethod) {
372
+ case AcCmColorMethod.ByLayer:
373
+ return 'ByLayer';
374
+ case AcCmColorMethod.ByBlock:
375
+ return 'ByBlock';
376
+ case AcCmColorMethod.ByColor:
377
+ return this._value ? AcCmColorUtil.getNameByColor(this._value) : '';
378
+ case AcCmColorMethod.ByACI:
379
+ return this._value ? AcCmColorUtil.getNameByIndex(this._value) : '';
380
+ default:
381
+ return undefined;
382
+ }
383
+ },
384
+ /**
385
+ * Sets the color by name.
386
+ *
387
+ * Resolves the name to an RGB value via `AcCmColorUtil`.
388
+ *
389
+ * @param name Color name
390
+ */
391
+ set: function (name) {
392
+ if (!name)
393
+ return;
394
+ var color = AcCmColorUtil.getColorByName(name);
395
+ if (color !== undefined) {
396
+ this._value = color;
397
+ this._colorMethod = AcCmColorMethod.ByColor;
398
+ }
399
+ else {
400
+ console.warn('Unknown color name:', name);
401
+ }
402
+ },
403
+ enumerable: false,
404
+ configurable: true
405
+ });
406
+ // ---------------------------------------------------------------------
407
+ // Clone / Copy / Equals
408
+ // ---------------------------------------------------------------------
541
409
  /**
542
- * Clones the current AcCmColor instance.
410
+ * Creates a clone of this color instance.
543
411
  *
544
- * @returns {AcCmColor} A new AcCmColor instance with the same color and index.
412
+ * @returns A new AcCmColor instance with the same method and value
545
413
  */
546
414
  AcCmColor.prototype.clone = function () {
547
- var clonedColor = new AcCmColor();
548
- clonedColor.colorIndex = this.colorIndex;
549
- clonedColor.color = this.color;
550
- clonedColor._colorName = this._colorName;
551
- return this;
415
+ var c = new AcCmColor();
416
+ c._colorMethod = this._colorMethod;
417
+ c._value = this._value;
418
+ return c;
552
419
  };
553
420
  /**
554
- * Copies the color from another AcCmColor instance.
421
+ * Copies color values from another AcCmColor instance.
555
422
  *
556
- * @param {AcCmColor} color - The color to copy from.
557
- * @returns {AcCmColor} The current instance.
423
+ * @param other The source color
424
+ * @returns The current instance
558
425
  */
559
- AcCmColor.prototype.copy = function (color) {
560
- this.colorIndex = color.colorIndex;
561
- this.color = color.color;
562
- this._colorName = color._colorName;
426
+ AcCmColor.prototype.copy = function (other) {
427
+ this._colorMethod = other._colorMethod;
428
+ this._value = other._value;
563
429
  return this;
564
430
  };
565
431
  /**
566
- * Checks if two AcCmColor instances are equal.
432
+ * Checks equality with another color.
567
433
  *
568
- * @param {AcCmColor} c - The color to compare with.
569
- * @returns {boolean} True if the colors and their indices are the same.
434
+ * @param other The color to compare
435
+ * @returns True if color method and value are identical
570
436
  */
571
- AcCmColor.prototype.equals = function (c) {
572
- return (c.color == this.color &&
573
- c.colorIndex == this.colorIndex &&
574
- c._colorName == this._colorName);
437
+ AcCmColor.prototype.equals = function (other) {
438
+ return (this._colorMethod === other._colorMethod && this._value === other._value);
575
439
  };
440
+ // ---------------------------------------------------------------------
441
+ // String representation
442
+ // ---------------------------------------------------------------------
576
443
  /**
577
444
  * Returns a string representation of the color.
578
445
  *
579
- * @returns {string} The color name or hexadecimal string.
446
+ * - "ByLayer" for ByLayer colors
447
+ * - "ByBlock" for ByBlock colors
448
+ * - One number for color index
449
+ * - Three comma-separated numbers for RGB color
580
450
  */
581
451
  AcCmColor.prototype.toString = function () {
582
- if (this.isByLayer) {
583
- return 'ByLayer';
584
- }
585
- else if (this.isByBlock) {
586
- return 'ByBlock';
587
- }
588
- else if (this.colorName) {
589
- return this.colorName;
590
- }
591
- else {
592
- return this.hexColor;
452
+ switch (this._colorMethod) {
453
+ case AcCmColorMethod.ByLayer:
454
+ return 'ByLayer';
455
+ case AcCmColorMethod.ByBlock:
456
+ return 'ByBlock';
457
+ case AcCmColorMethod.ByACI:
458
+ // ACI index as number string: "30"
459
+ return this._value !== undefined ? String(this._value) : '';
460
+ case AcCmColorMethod.ByColor:
461
+ if (!this._value)
462
+ return '';
463
+ return "".concat(this.red, ",").concat(this.green, ",").concat(this.blue);
464
+ default:
465
+ return '';
593
466
  }
594
467
  };
595
468
  /**
596
- * Finds the color name associated with a given RGB value.
597
- *
598
- * @private
599
- * @param {number} target - The RGB value to find a name for.
600
- * @returns {string | null} The color name if found, null otherwise.
469
+ * Creates one AcCmColor from one string
601
470
  */
602
- AcCmColor.prototype.getColorNameByValue = function (target) {
603
- var e_1, _a;
604
- try {
605
- for (var _b = __values(Object.entries(_colorKeywords)), _c = _b.next(); !_c.done; _c = _b.next()) {
606
- var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
607
- if (value === target) {
608
- return key;
609
- }
610
- }
471
+ AcCmColor.fromString = function (name) {
472
+ if (!name)
473
+ return undefined;
474
+ var n = name.trim();
475
+ // 1. ByLayer / ByBlock
476
+ if (/^bylayer$/i.test(n)) {
477
+ return new AcCmColor(AcCmColorMethod.ByLayer);
611
478
  }
612
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
613
- finally {
614
- try {
615
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
616
- }
617
- finally { if (e_1) throw e_1.error; }
479
+ if (/^byblock$/i.test(n)) {
480
+ return new AcCmColor(AcCmColorMethod.ByBlock);
618
481
  }
619
- return null;
620
- };
621
- /**
622
- * Finds the AutoCAD color index associated with a given RGB value.
623
- *
624
- * @private
625
- * @param {number} target - The RGB value to find an index for.
626
- * @returns {number | null} The color index if found, null otherwise.
627
- */
628
- AcCmColor.prototype.getColorIndexByValue = function (target) {
629
- var length = AUTO_CAD_COLOR_INDEX.length - 1;
630
- for (var index = 1; index < length; ++index) {
631
- if (AUTO_CAD_COLOR_INDEX[index] === target) {
632
- return index;
633
- }
482
+ // 2. RGB: "r,g,b"
483
+ if (/^\d{1,3},\d{1,3},\d{1,3}$/i.test(n)) {
484
+ var _a = __read(n.split(',').map(Number), 3), r = _a[0], g = _a[1], b = _a[2];
485
+ var color = new AcCmColor(AcCmColorMethod.ByColor);
486
+ color.setRGB(r, g, b);
487
+ return color;
488
+ }
489
+ // 3. ACI index (must be integer)
490
+ if (/^\d+$/.test(n)) {
491
+ var index = parseInt(n, 10);
492
+ return new AcCmColor(AcCmColorMethod.ByACI, index);
493
+ }
494
+ // 4. Named colors or color book entries
495
+ var colorVal = AcCmColorUtil.getColorByName(n);
496
+ if (colorVal != null) {
497
+ return new AcCmColor(AcCmColorMethod.ByColor, colorVal);
634
498
  }
635
- return null;
499
+ console.warn('Unknown color name:', name);
636
500
  };
637
- AcCmColor.NAMES = _colorKeywords;
638
501
  return AcCmColor;
639
502
  }());
640
503
  export { AcCmColor };