webdesserts / alchemist-js

The extensible color library
https://webdesserts.gitbooks.io/alchemist-js/
MIT License
11 stars 2 forks source link

Bounds #1

Closed webdesserts closed 9 years ago

webdesserts commented 9 years ago

How should alchemist handle boundries and clipping?

The end user api might look something like this:

alchemize.rgb(255, 255, 255) // => Color
alchemize.rgb(256, 255, 255) // => null

what the plugin interface might look like:

alchelmize.use({
  name: 'rgb',
  bounds: function bounds (r, g, b) {
    // success?
    return true
    // clipped?
    return [r, g, b]
    // invalid?
    return false
  },
  to: function () {}
})

Ya, that's pretty messy. If there's a way that I could make that cleaner, it would be nice. I'm not even sure if I want to allow clipping yet. The problem though, is that if I don't implement it inside the library, the color space developer would implement it inside their conversion functions and the user would have no way to turn the clipping off if they just want a null value when something fails.

This also brings up the issue of wether or not we should return a soft error as null, or actually throw an error and let the user handle it with something like promises. Maybe we should make this a configuration option?

  Alchemist.create({bounds: true}).rgb(256,255,255) // => null (the default)
  Alchemist.create({bounds: 'strict'}).rgb(256,255,255) // Throws Error
  Alchemist.create({bounds: 'clip'}).rgb(256,256,256) // => [255,255,255]

...aaaand finally this brings up the issue of whether we should clip on color interpretation or just color conversion?

  var alchemize() = Alchemist.create({bounds: 'clip'})
  alchemize.rgb(256, 255, 255) // Seems like an error would be expected
  alchemize.lch(200, 200, 200).rgb() // => [91, 50, 197] (clipped value)
webdesserts commented 9 years ago

So to update, the current idea for implementing this looks like so:

alchemist.use({
  name: 'rgb',
  limits: {
    upper: [255, 255, 255],
    lower: [0, 0, 0]
  },
  to: {...}
})

This would be used in conjunction with the config option I talked about earlier. alchemist.create({ limits: 'clip' })

One thing I'm not sure about is if we should pass null directly or a color with a null value. If we do the later, we can silently fail multiple attempts to convert until a value export. I'm concerned that although I will be able to handle this case gracefully within my functions, plugins are a different story.

webdesserts commented 9 years ago

Closed by c3cf3bd6d2b57593b12c22886e02e5938c0b7a54 :tada:

webdesserts commented 9 years ago

For anyone wondering, the new api looks like this:

Settings

alchemist.init({ limits: 'clip' })

Plugins

var rgb = {
  name: 'rgb',
  limits: {
    max: [255, 255, 255],
    min: [0, 0, 0]
  }
  to: {}
}