Repository: playcanvas/engine
File: src/core/math/color.js (JavaScript)

Code before:
            return `${r.toFixed(3)}, ${g.toFixed(3)}, ${b.toFixed(3)}, ${a.toFixed(3)}`;
        }

        let s = `#${((1 << 24) + (Math.round(r * 255) << 16) + (Math.round(g * 255) << 8) + Math.round(b * 255)).toString(16).slice(1)}`;
        if (alpha === true) {
            const aa = Math.round(a * 255).toString(16);
            if (this.a < 16 / 255) {
                s += `0${aa}`;
            } else {
                s += aa;
            }
        }
        return s;
    }

    /**
     * Converts the color to an array of numbers.
     *
     * @param {boolean} [alpha] - If true, the output array will include the alpha value.
     * @returns {number[]} The color as an array of numbers.
     * @example
     * const c = new pc.Color(1, 1, 1);
     * // Outputs [1, 1, 1, 1]
     * console.log(c.toArray(true));
     */
    toArray(alpha) {
        if (alpha === true) {
            return [this.r, this.g, this.b, this.a];
        }
        return [this.r, this.g, this.b];
    }

    /**
     * A constant color set to black [0, 0, 0, 1].
     *
     * @type {Color}
     * @readonly
     */
    static BLACK = Object.freeze(new Color(0, 0, 0, 1));

    /**
     * A constant color set to blue [0, 0, 1, 1].
     *
     * @type {Color}
     * @readonly
     */
    static BLUE = Object.freeze(new Color(0, 0, 1, 1));

    /**
     * A constant color set to cyan [0, 1, 1, 1].
     *

Review comment (suggestion):
and this here

```
toArray(alpha) {
    return alpha ? [this.r, this.g, this.b, this.a] : [this.r, this.g, this.b];
}
```

Code after:
        // If any component exceeds 1 (HDR), return the color as an array
        if (asArray || r > 1 || g > 1 || b > 1) {
            return `${r.toFixed(3)}, ${g.toFixed(3)}, ${b.toFixed(3)}, ${a.toFixed(3)}`;
        }

        let s = `#${((1 << 24) + (Math.round(r * 255) << 16) + (Math.round(g * 255) << 8) + Math.round(b * 255)).toString(16).slice(1)}`;
        if (alpha === true) {
            const aa = Math.round(a * 255).toString(16);
            if (this.a < 16 / 255) {
                s += `0${aa}`;
            } else {
                s += aa;
            }
        }
        return s;
    }

    /**
     * Converts the color to an array of numbers.
     *
     * @param {number[]} [arr] - The array to populate with the color components. If not specified,
     * a new array is created. Default is true.
     * @param {number} [offset] - The zero-based index at which to start copying elements to the
     * array. Default is 0.
     * @param {boolean} [alpha] - If true, the output array will include the alpha value.
     * @returns {number[]} The color as an array of numbers.
     * @example
     * const c = new pc.Color(1, 1, 1);
     * // Outputs [1, 1, 1, 1]
     * console.log(c.toArray());
     */
    toArray(arr = [], offset = 0, alpha = true) {
        arr[offset] = this.r;
        arr[offset + 1] = this.g;
        arr[offset + 2] = this.b;
        if (alpha) {
            arr[offset + 3] = this.a;
        }
        return arr;
    }

    /**
     * A constant color set to black [0, 0, 0, 1].
     *
     * @type {Color}
     * @readonly
     */
    static BLACK = Object.freeze(new Color(0, 0, 0, 1));

    /**
     * A constant color set to blue [0, 0, 1, 1].