processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.47k stars 3.29k forks source link

Logic behind colorMode being attached to color instance rather than global #1620

Open tomchambers2 opened 7 years ago

tomchambers2 commented 7 years ago

I've been having some headaches with Color today - trying to get pixel colors from two images and then combining them using the hue value of one and the saturation and brightness from another. In the end I had to use this slightly circuitous code to get it:

Palette.prototype.getColor = function(x, y) {
    push()
    colorMode(HSL, 1)
    colorMode(RGB, 255)
    var hueColor = color(this.huesImage.get(x, y))
    var otherColor = color(this.otherImage.get(x, y))
    colorMode(HSB, 1)
    var c = color(hue(hueVar), saturation(marble), brightness(marble))
    pop()
    return c
}

It seems to me like it would be simpler generally to store a mode-agnostic representation of the color in Color and then export it via hue(), red() etc according to the currently set colorMode, simplifying the example to:

Palette.prototype.getColor = function(x, y) {
    push()
    colorMode(HSB, 1)
    var hueColor = color(this.huesImage.get(x, y))
    var otherColor = color(this.otherImage.get(x, y))
    var c = color(hue(hueColor), saturation(otherColor), brightness(otherColor))
    pop()
    return c
}

I might have a niche use case, so fair enough if this isn't an issue! But this seems to me to be closer to how colors are done in Processing. For me it's unintuitive to have the colorMode attached to the color when it isn't passed as an argument to the constructor.

ecridge commented 7 years ago

I think that this also ties in with #1517 (i.e. making Colors mutable).

Ideally there would be a canonical array that describes the RGB screen colour (currently p5.Color.levels), and all of the other colour descriptors (red, green, hue, saturation, etc.) would be scaled and calculated/interpreted on the fly using the global colorMode at the time that the corresponding getter/setter/constructor is called.

This would make Colors completely transparent to the user (so you don’t have to keep track of e.g. whether saturation is HSL-style or HSB-style for a particular Color, or whether it is scaled from 0 to 100 or 0 to 255 for that Color…), but it would also be quite a breaking change – even from Processing – which is not necessarily desirable.