Skip to content

Refactor Color Code #6680

@JustZambetti

Description

@JustZambetti

Increasing Access

Making the code easier to read

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build Process
  • Unit Testing
  • Internalization
  • Friendly Errors
  • Other (specify if possible)

Feature enhancement details

I noticed a bunch of tiny problems in src/color/p5.Color.js:

  • Criptic names that could be replaced:
    pInst and vals for example could be replaced with p5Instance and values respectively
  constructor(pInst, vals) {
    // Record color mode and maxes at time of construction.
    this._storeModeAndMaxes(pInst._colorMode, pInst._colorMaxes);
  • Literals in setRed, setBlue, setGreen, setAlpha which could be replaced with constants
    For example, this._array[1] could become this._array[GreenLevel]
setGreen(new_green) {
    this._array[1] = new_green / this.maxes[constants.RGB][1];
    this._calculateLevels();
  }
  • _calculateLevels() might be broken down using a simpler method _updateLevel(level). In many cases only that smaller method would be called (on setRed(), setBlue(),... for example)
_calculateLevels() {
   const array = this._array;
   // (loop backwards for performance)
   const levels = (this.levels = new Array(array.length));
   for (let i = array.length - 1; i >= 0; --i) {
     levels[i] = Math.round(array[i] * 255);
   }

   // Clear cached HSL/HSB values
   this.hsla = null;
   this.hsba = null;
 }
  • _getMode(), _getMaxes() and the export default are never used, they can be removed.
  • there are errors in the javadoc (non-matching field names)
  • method _parseInputs() is too long (207 lines of code) and therfore hard to read
  • namedColors enum could be moved in another file since it's very long

I would like to work on this issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions