Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeErrors for certain values of hue and dist #8

Open
backhand opened this issue Nov 23, 2015 · 1 comment
Open

TypeErrors for certain values of hue and dist #8

backhand opened this issue Nov 23, 2015 · 1 comment
Assignees

Comments

@backhand
Copy link

Hi,

I'm assuming that hue should be an int between 0-255 and distance a float between 0 and 1.
Given that, I've experienced the following error with various combinations of hue and distance:

TypeError: Cannot read property '0' of undefined
    at mutablecolor.ColorScheme.mutablecolor.mutablecolor.set_hue (../color-scheme/lib/color-scheme.js:505:49)
    at mutablecolor.ColorScheme.mutablecolor.mutablecolor.rotate (../color-scheme/lib/color-scheme.js:514:21)
    at Object.dispatch.triade (../color-scheme/lib/color-scheme.js:107:31)
    at ColorScheme.colors (../color-scheme/lib/color-scheme.js:134:31)

hue 170 and distance 0.16 will throw this error. This will list out some of the values:

/*jslint node:true, multistr: true */
'use strict';

var ColorScheme = require('color-scheme');

for(let hue = 0; hue < 256; hue++) {
  for(let dist = 0.0; dist < 1.0; dist+=0.01) {
    // try {
      let scheme = new ColorScheme();
      scheme.from_hue(hue)
        .scheme('triade')
        .variation('hard')
        .distance(dist);

      let colors = scheme.colors();
    } catch(err) {
      console.log(hue, dist, err);
    }
  }
}

Did I misunderstand something in the use of your library?

@c0bra
Copy link
Owner

c0bra commented May 17, 2017

@backhand sorry for taking such an extraordinarily long time to respond.

Hue should be an int between 0 and 360. The hue represents a degree around the color wheel. You can see a live version here: http://paletton.com/#uid=1000u0kllllaFw0g0qFqFg0w0aF

It seems like this is a problem with floating point imprecision in js. If you try this:

/*jslint node:true, multistr: true */
'use strict';

var ColorScheme = require('color-scheme');

for(let hue = 0; hue < 256; hue++) {
  for(let dist = 0.0; dist < 1.0; dist+=0.01) {
    try {
      let scheme = new ColorScheme();
      scheme.from_hue(hue)
        .scheme('triade')
        .variation('hard')
        .distance(dist);

      let colors = scheme.colors();
      console.log(colors[0]);
    } catch(err) {
      console.log(hue, dist, err);
    }
  }
}

You'll see that there are only errors for certain colors, not all.

And if you modify dist like this dist = dist.toFixed(2); it seems like the problem goes away.

The solution may be to just clamp distances to 2 floating points (or somewhere around there) in the library.

@c0bra c0bra self-assigned this May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants