/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Rendering
 */
import { assert, compareNumbers, IndexMap } from "@itwin/core-bentley";
import { ColorDef } from "@itwin/core-common";
/** @internal */
export class ColorMap extends IndexMap {
    constructor() {
        super(compareNumbers, 0xffff);
        this._hasTransparency = false;
    }
    hasColor(color) { return -1 !== this.indexOf(color); }
    insert(color) {
        // The table should never contain a mix of opaque and translucent colors.
        if (this.isEmpty)
            this._hasTransparency = ColorMap.isTranslucent(color);
        else
            assert(ColorMap.isTranslucent(color) === this.hasTransparency);
        return super.insert(color);
    }
    get hasTransparency() { return this._hasTransparency; }
    get isUniform() { return 1 === this.length; }
    toColorIndex(index, indices) {
        index.reset();
        if (0 === this.length) {
            assert(false, "empty color map");
            return;
        }
        else if (1 === this.length) {
            index.initUniform(this._array[0].value);
        }
        else {
            const colors = new Uint32Array(this.length);
            for (const entry of this._array)
                colors[entry.index] = entry.value;
            index.initNonUniform(colors, indices, this.hasTransparency);
        }
    }
    static isTranslucent(tbgr) {
        return !ColorDef.isOpaque(tbgr);
    }
}
//# sourceMappingURL=ColorMap.js.map