@mlightcad/common
Version:
[](https://opensource.org/licenses/MIT) [](https://www.npmjs.com/package/@mlightcad/common)
331 lines • 11.4 kB
JavaScript
import { AcCmTransparencyMethod } from './AcCmTransparencyMethod';
/**
* Class representing transparency similar to AutoCAD’s `AcCmTransparency`.
*
* Stores both method and alpha information.
*/
var AcCmTransparency = /** @class */ (function () {
/**
* Creates a new transparency object.
*
* @param alpha
* When provided, constructs with `ByAlpha` method and sets alpha.
* Must be between 0 and 255.
*/
function AcCmTransparency(alpha) {
if (alpha !== undefined) {
this._method = AcCmTransparencyMethod.ByAlpha;
this._alpha = AcCmTransparency.clampAlpha(alpha);
}
else {
this._method = AcCmTransparencyMethod.ByLayer;
this._alpha = 255;
}
}
Object.defineProperty(AcCmTransparency.prototype, "method", {
/** Gets the current transparency method */
get: function () {
return this._method;
},
/**
* Sets the transparency method.
* If setting to ByAlpha with no prior alpha, alpha stays 255 (opaque).
*
* @param method The new transparency method
*/
set: function (method) {
this._method = method;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "alpha", {
/**
* Gets the alpha value.
* Only meaningful if `method === ByAlpha`.
*/
get: function () {
return this._alpha;
},
/**
* Sets the alpha value and force the method to `ByAlpha`.
*
* @param alpha 0–255 alpha, clamped internally if out of range
*/
set: function (alpha) {
this._alpha = AcCmTransparency.clampAlpha(alpha);
this._method = AcCmTransparencyMethod.ByAlpha;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "percentage", {
/**
* Gets the AutoCAD-style transparency percentage.
*
* Mapping rules:
* - 0% = fully opaque (alpha = 255)
* - 100% = fully transparent (alpha = 0)
*
* This matches how AutoCAD displays and stores transparency
* values in UI and DXF files.
*
* If the transparency method is not `ByAlpha`,
* this method returns `undefined`, because AutoCAD
* does not define a percentage for ByLayer or ByBlock.
*
* @returns Transparency percentage (0–100), or undefined
*/
get: function () {
if (this._method !== AcCmTransparencyMethod.ByAlpha) {
return undefined;
}
return Math.round((1 - this._alpha / 255) * 100);
},
/**
* Sets the transparency using an AutoCAD-style percentage.
*
* Mapping rules (AutoCAD compatible):
* - 0% → fully opaque (alpha = 255)
* - 100% → fully transparent (alpha = 0)
*
* Internally, the alpha value is calculated as:
* alpha = round(255 × (1 − percentage / 100))
*
* This method:
* - Forces the transparency method to `ByAlpha`
* - Clamps the percentage to the range 0–100
* - Preserves ObjectARX value semantics
*
* @param percentage Transparency percentage (0–100)
* @returns This instance (for fluent chaining)
*
* @example
* const t = new AcCmTransparency();
* t.setPercentage(50); // ≈ alpha 128
*
* t.setPercentage(0); // alpha = 255 (opaque)
* t.setPercentage(100); // alpha = 0 (clear)
*/
set: function (percentage) {
var p = Math.max(0, Math.min(100, percentage));
var alpha = Math.round(255 * (1 - p / 100));
this.alpha = alpha;
},
enumerable: false,
configurable: true
});
/**
* Ensures alpha always stays within 0–255.
*/
AcCmTransparency.clampAlpha = function (alpha) {
return Math.max(0, Math.min(255, Math.floor(alpha)));
};
Object.defineProperty(AcCmTransparency.prototype, "isByAlpha", {
/**
* True if the method is `ByAlpha`.
*/
get: function () {
return this._method === AcCmTransparencyMethod.ByAlpha;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "isByBlock", {
/**
* True if the method is `ByBlock`.
*/
get: function () {
return this._method === AcCmTransparencyMethod.ByBlock;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "isByLayer", {
/**
* True if the method is `ByLayer`.
*/
get: function () {
return this._method === AcCmTransparencyMethod.ByLayer;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "isClear", {
/**
* True if transparency is exactly clear (alpha==0 and ByAlpha).
*/
get: function () {
return this.isByAlpha && this._alpha === 0;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "isSolid", {
/**
* True if transparency is solid (alpha==255 and ByAlpha).
*/
get: function () {
return this.isByAlpha && this._alpha === 255;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcCmTransparency.prototype, "isInvalid", {
/**
* True if current state is invalid (ErrorValue).
*/
get: function () {
return this._method === AcCmTransparencyMethod.ErrorValue;
},
enumerable: false,
configurable: true
});
/**
* Convert this transparency to an integer suitable for storage.
* Uses a simple bit-encoding: highbits for method and lowbits for alpha.
*
* 31 24 23 8 7 0
* +-------------+--------------+------------+
* | flags | reserved | alpha |
* +-------------+--------------+------------+
*/
AcCmTransparency.prototype.serialize = function () {
var methodVal = this._method;
return (methodVal << 24) | this._alpha;
};
/**
* Creates a deep copy of this transparency object.
*
* This mirrors the value-semantics of ObjectARX `AcCmTransparency`,
* where copying results in an independent object with the same
* transparency method and alpha value.
*
* @returns A new `AcCmTransparency` instance with identical state.
*/
AcCmTransparency.prototype.clone = function () {
var copy = new AcCmTransparency();
copy._method = this._method;
copy._alpha = this._alpha;
return copy;
};
/**
* Compares this transparency with another one for equality.
*
* Two `AcCmTransparency` objects are considered equal if:
* - Their transparency methods are identical
* - Their alpha values are identical
*
* This mirrors the value semantics of ObjectARX
* `AcCmTransparency`.
*
* @param other The transparency to compare with
* @returns True if both represent the same transparency
*
* @example
* const a = new AcCmTransparency(128);
* const b = new AcCmTransparency(128);
* a.equals(b); // true
*/
AcCmTransparency.prototype.equals = function (other) {
return this._method === other._method && this._alpha === other._alpha;
};
/**
* Returns a human-readable string representation of the transparency.
*
* Behavior:
* - `"ByLayer"` if transparency is inherited from layer
* - `"ByBlock"` if transparency is inherited from block
* - Numeric alpha value (`"0"`–`"255"`) if method is `ByAlpha`
*
* This format is intentionally simple and mirrors common
* AutoCAD UI and DXF text usage.
*
* @returns String representation of the transparency
*
* @example
* new AcCmTransparency().toString(); // "ByLayer"
* new AcCmTransparency(128).toString(); // "128"
*/
AcCmTransparency.prototype.toString = function () {
if (this.isByLayer)
return 'ByLayer';
if (this.isByBlock)
return 'ByBlock';
return this._alpha.toString();
};
/**
* Creates an `AcCmTransparency` instance from a string representation.
*
* Accepted formats:
* - `""`, `"."`, `"use current"` or `"ByLayer"` (case-insensitive)
* - `"ByBlock"` (case-insensitive)
* - Transparency percentage `"0"`–`"90"`
* - Numeric alpha value `"91"`–`"255"` for compatibility
*
* Invalid or out-of-range values will produce an
* `ErrorValue` transparency.
*
* @param value String to parse
* @returns Parsed `AcCmTransparency` instance
*
* @example
* AcCmTransparency.fromString("ByLayer");
* AcCmTransparency.fromString("25");
* AcCmTransparency.fromString("ByBlock");
*/
AcCmTransparency.fromString = function (value) {
var v = value.trim();
var normalized = v.toLowerCase();
if (normalized === '' ||
normalized === '.' ||
normalized === 'use current' ||
normalized === 'bylayer') {
var t_1 = new AcCmTransparency();
t_1._method = AcCmTransparencyMethod.ByLayer;
return t_1;
}
if (normalized === 'byblock') {
var t_2 = new AcCmTransparency();
t_2._method = AcCmTransparencyMethod.ByBlock;
return t_2;
}
var numericValue = Number(v);
if (Number.isInteger(numericValue) &&
numericValue >= 0 &&
numericValue <= 255) {
var t_3 = new AcCmTransparency();
if (numericValue <= 90) {
t_3.percentage = numericValue;
}
else {
t_3.alpha = numericValue;
}
return t_3;
}
// Invalid input → ErrorValue
var t = new AcCmTransparency();
t._method = AcCmTransparencyMethod.ErrorValue;
return t;
};
/**
* Deserialize an integer back into a transparency object.
*
* @param value 32-bit stored transparency representation
*/
AcCmTransparency.deserialize = function (value) {
var methodIndex = (value >>> 24) & 0xff;
var alpha = value & 0xff;
var method = methodIndex >= AcCmTransparencyMethod.ByLayer &&
methodIndex <= AcCmTransparencyMethod.ErrorValue
? methodIndex
: AcCmTransparencyMethod.ErrorValue;
var t = new AcCmTransparency();
t._method = method;
t._alpha = AcCmTransparency.clampAlpha(alpha);
return t;
};
return AcCmTransparency;
}());
export { AcCmTransparency };
//# sourceMappingURL=AcCmTransparency.js.map