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

Code before:
        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.
     * @param {number[]} [out] - The array to populate with the color components. If not specified,
     * a new array is created.
     * @param {number} [offset] - The zero-based index at which to start copying elements to the
     * array. Default is 0.
     * @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, out = [], offset = 0) {
        out[offset] = this.r;
        out[offset + 1] = this.g;
        out[offset + 2] = this.b;
        if (alpha) {
            out[offset + 3] = this.a;
        }
        return out;
    }

    /**
     * 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));


Review comment (suggestion):
I'm not keen on `alpha` being the first parameter here. It would be nice to have symmetry for arguments for `fromArrray` and `toArray` (e.g. first arg is `array` and second arg is `offset`). Then you could add `alpha` as the third arg for `Color`.

Code after:
        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].
     *
     * @type {Color}
     * @readonly
     */
    static BLUE = Object.freeze(new Color(0, 0, 1, 1));