BabylonJS/Babylon.js · error · ReferenceError

Can not divide a color

Error message

Can not divide a color

What it means

The pure color3-like record (IColor3Like) intentionally does not implement division — colors have no meaningful division operation. divide() always throws a ReferenceError as a guard against calling numeric-vector math on pure color records.

Source

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

    }

    /**
     * Returns a new Color3 set with the result of the multiplication of the current Color3 coordinates by the given floats
     * @param r defines the r coordinate of the operand
     * @param g defines the g coordinate of the operand
     * @param b defines the b coordinate of the operand
     * @returns the new Color3
     */
    public multiplyByFloats(r: number, g: number, b: number): Color3 {
        return new Color3(this.r * r, this.g * g, this.b * b);
    }

    /**
     * @internal
     * Do not use
     */
    public divide(_other: DeepImmutable<IColor3Like>): never {
        throw new ReferenceError("Can not divide a color");
    }

    /**
     * @internal
     * Do not use
     */
    public divideToRef(_other: DeepImmutable<IColor3Like>, _result: IColor3Like): never {
        throw new ReferenceError("Can not divide a color");
    }

    /**
     * @internal
     * Do not use
     */
    public divideInPlace(_other: DeepImmutable<IColor3Like>): never {
        throw new ReferenceError("Can not divide a color");
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Divide component-wise manually: { r: c.r / d.r, g: c.g / d.g, b: c.b / d.b } using a mutable Color3.
  2. Convert the pure record to a mutable Color3 (new Color3(c.r, c.g, c.b)) and use Color3 math, if division semantics are truly needed.
  3. Refactor to multiply by a reciprocal color if that is the underlying intent.

Example fix

// before
const out = pureColor.divide(other); // always throws
// after
const out = new Color3(pureColor.r / other.r, pureColor.g / other.g, pureColor.b / other.b);
Defensive patterns

Strategy: type-guard

Validate before calling

// Detect pure color records before applying vector math
function isPureColorRecord(v: unknown): v is { r: number; g: number; b: number } {
  return !!v && typeof v === "object" && !((v as any) instanceof Color3) &&
    typeof (v as any).divide === "function" === false;
}

Type guard

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

Try / catch

try {
  result = colorLike.divide(other);
} catch (e) {
  if (String(e.message) === "Can not divide a color") {
    result = new Color3(colorLike.r / other.r, colorLike.g / other.g, colorLike.b / other.b);
  }
}

Prevention

When it happens

Trigger: Calling .divide(other) on a pure IColor3Like instance (e.g. a Color3 converted to its pure/record form used by vectorized math paths), instead of converting to a full mutable Color3/Vector3 first.

Common situations: Generic numeric code that applies the same ops to colors and vectors, migrating code from mutable Color3 to the pure record representation, generated shader math code assuming full operator support.

Related errors


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