@itwin/core-common
Version:
iTwin.js components common to frontend and backend
81 lines • 3.37 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Symbology
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RgbColor = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const ColorDef_1 = require("./ColorDef");
/** An immutable representation of a color with red, green, and blue components each in the integer range [0, 255].
* @public
*/
class RgbColor {
r;
g;
b;
/** Constructs from red, green, and blue components.
* @param r Red
* @param g Green
* @param b Blue
*/
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.r = Math.max(0, Math.min(this.r, 0xff));
this.g = Math.max(0, Math.min(this.g, 0xff));
this.b = Math.max(0, Math.min(this.b, 0xff));
}
/** Constructs from the red, green, and blue components of a ColorDef. The transparency component is ignored. */
static fromColorDef(colorDef) {
const colors = colorDef.colors;
return new RgbColor(colors.r, colors.g, colors.b);
}
/** Converts this RgbColor to a ColorDef.
* @param transparency Value to use for the transparency component of the ColorDef.
* @param out If defined, this ColorDef will be modified in-place and returned; otherwise a new ColorDef will be allocated.
* @returns A ColorDef with RGB components equivalent to those of this RgbColor and transparency component as specified.
*/
toColorDef(transparency = 0) {
return ColorDef_1.ColorDef.from(this.r, this.g, this.b, transparency);
}
/** Convert this color to its JSON representation. */
toJSON() {
return { r: this.r, g: this.g, b: this.b };
}
/** Create an RgbColor from its JSON representation.
* If `json` is `undefined`, the result is pure white.
*/
static fromJSON(json) {
let r = 0xff;
let g = 0xff;
let b = 0xff;
if (undefined !== json) {
if (typeof json.r === "number")
r = json.r;
if (typeof json.g === "number")
g = json.g;
if (typeof json.b === "number")
b = json.b;
}
return new RgbColor(r, g, b);
}
/** Returns true if this color's red, green, and blue components are identical to those of `other`. */
equals(other) {
return this.r === other.r && this.g === other.g && this.b === other.b;
}
/** Compare this color to another color using the rules of an [OrderedComparator]($bentley). */
compareTo(other) {
return (0, core_bentley_1.compareNumbers)(this.r, other.r) || (0, core_bentley_1.compareNumbers)(this.g, other.g) || (0, core_bentley_1.compareNumbers)(this.b, other.b);
}
/** Convert this color to a string in the form "#rrggbb" where the values are the hex digits of the respective color components. */
toHexString() {
return this.toColorDef().toHexString();
}
}
exports.RgbColor = RgbColor;
//# sourceMappingURL=RgbColor.js.map