Color Comparison

Hi all,

I feel like I must be missing something basic, because I can’t seem to do an equals check on two colors. As to what I’ve been testing:

console.log(pc.app.root.findByName("ClientCharacter" + i + "Border").element.color);
console.log(Lib.Black); //Lib.Black is set as: Lib.Black = new pc.Color(0, 0, 0, 1);
console.log(pc.app.root.findByName("ClientCharacter" + i + "Border").element.color === Lib.Black);
console.log(pc.app.root.findByName("ClientCharacter" + i + "Border").element.color == Lib.Black);
let color1 = new pc.Color(1, 1, 1, 1);
let color2 = new pc.Color(1, 1, 1, 1);
console.log(color1 === color2);
console.log(color1.equals(color2));

The results of the previous code snippets is:

Color {r: 0, g: 0, b: 0, a: 1}
Color {r: 0, g: 0, b: 0, a: 1}
false
false
false
Uncaught TypeError: color1.equals is not a function

The last error is somewhat confusing, as the documentation has dst.equals(src) as a valid function.

Either way, if == isn’t working, === isn’t working, and equals() isn’t a method, is there a simple way of comparing colors, outside of checking r === r, g === g, etc?

Hi @TingleyWJ,

It seems that for some reason the equals method was omitted in the pc.Color class. The Vec3 and its variations support this but in the pc.Color class there isn’t an implementation for equals.

Which doesn’t do anything more or special than what you are proposing, here is its definition in the pc.Vec3 class:

        /**
         * @function
         * @name pc.Vec3#equals
         * @description Reports whether two vectors are equal.
         * @param {pc.Vec3} rhs - The vector to compare to the specified vector.
         * @returns {boolean} True if the vectors are equal and false otherwise.
         * @example
         * var a = new pc.Vec3(1, 2, 3);
         * var b = new pc.Vec3(4, 5, 6);
         * console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
         */
        equals: function (rhs) {
            return this.x === rhs.x && this.y === rhs.y && this.z === rhs.z;
        },

I’d say this is a valid feature request if you would like to submit it to the engine repo. In the meantime you can write your own method to do this with colors.

1 Like

I created the request to add this.

2 Likes