BabylonJS/Babylon.js · error · ReferenceError

Can not negate a color

Error message

Can not negate a color

What it means

Color3.negate is an intentional stub that throws ReferenceError("Can not negate a color"). Producing a negated (inverted-sign) color is not a supported operation in this API. Every call throws; there is no valid path.

Source

Thrown at packages/dev/core/src/Maths/math.color.pure.ts:348

        return this.r === r && this.g === g && this.b === b;
    }

    /**
     * Returns true if the current Color3 and the given color coordinates are distant less than epsilon
     * @param otherColor defines the second operand
     * @param epsilon defines the minimal distance to define values as equals
     * @returns true if both colors are distant less than epsilon
     */
    public equalsWithEpsilon(otherColor: DeepImmutable<IColor3Like>, epsilon: number = Epsilon): boolean {
        return WithinEpsilon(this.r, otherColor.r, epsilon) && WithinEpsilon(this.g, otherColor.g, epsilon) && WithinEpsilon(this.b, otherColor.b, epsilon);
    }

    /**
     * @internal
     * Do not use
     */
    public negate(): never {
        throw new ReferenceError("Can not negate a color");
    }

    /**
     * @internal
     * Do not use
     */
    public negateInPlace(): never {
        throw new ReferenceError("Can not negate a color");
    }

    /**
     * @internal
     * Do not use
     */
    public negateToRef(_result: IColor3Like): never {
        throw new ReferenceError("Can not negate a color");
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. If you want the color complement, compute new Color3(1-r, 1-g, 1-b).
  2. If you truly need negated components, construct manually: new Color3(-c.r, -c.g, -c.b).
  3. Use a Vector3 for full negation support.
  4. Do not assume Color3 implements every Vector3 method.

Example fix

// before
const inverted = color.negate();
// after
const inverted = new Color3(1 - color.r, 1 - color.g, 1 - color.b); // complement
// or true negation:
const negated = new Color3(-color.r, -color.g, -color.b);
Defensive patterns

Strategy: validation

Validate before calling

if (color instanceof Color3) {
  var inverted = new Color3(1 - color.r, 1 - color.g, 1 - color.b);
} else {
  var inverted = color.negate();
}

Type guard

function isColor3(c: unknown): c is Color3 {
  return c instanceof Color3;
}

Try / catch

try {
  inverted = color.negate();
} catch (e) {
  if (e instanceof ReferenceError && e.message === 'Can not negate a color') {
    inverted = new Color3(1 - color.r, 1 - color.g, 1 - color.b);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling color3.negate() directly expecting -color, or generic math code that negates any vector-like operand including colors.

Common situations: Reversing/diffing computations ported from Vector3 code; mistaking negate for color inversion (complement); autocomplete-driven misuse.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/94fa8b87b8150c16. Report an issue: GitHub.