Skip to content

Throw error when passed incorrect color mode instead of silently accepting it? #5449

@gregsadetsky

Description

@gregsadetsky

How would this new feature help increase access to p5.js?

p5.js could show an error message when colorMode() is passed an invalid color mode, instead of silently ignoring it.

Most appropriate sub-area of p5.js?

  • Friendly error system

New feature details:

colorMode accepts any string as its mode

I mistakenly passed "HSL" to it, e.g. a string, instead of the correct constant. I did not realize that colorMode() silently ignored my request to change the color mode, and stayed in RGB mode. It feels to me like this could be quite confusing to others as well.

I would suggest doing the following change: if the passed argument is not any of the known modes, throw an error, i.e.:

p5.prototype.colorMode = function(mode, max1, max2, max3, maxA) {
  p5._validateParameters('colorMode', arguments);
  if (
    mode === constants.RGB ||
    mode === constants.HSB ||
    mode === constants.HSL
  ) {
  // ...
  // ...
  // *** New code below ***
  } else {
    throw new Error('invalid color mode')
  }

I'd be happy to do a commit/PR (with a test) for this. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions