bgrins / TinyColor

Fast, small color manipulation and conversion for JavaScript
https://bgrins.github.io/TinyColor/
MIT License
5.08k stars 438 forks source link

Don't mutate input objects #95

Closed benekastah closed 8 years ago

benekastah commented 9 years ago

Currently the tinycolor constructor actually modifies input objects:

var hsv = {h: 100, s: 0.5, v: 0.5};
tinycolor(hsv);
console.log(hsv);  // Object {h: 100, s: "50%", v: "50%"}

I spent a while trying to figure out why I was getting NaNs in my app :)

bgrins commented 9 years ago

Yeah, looks like inputToRgb should make a clone of the input if it's an object: https://github.com/bgrins/TinyColor/blob/f7cd274b12f0aaecb974270632b24d0200175b97/tinycolor.js#L300

WickyNilliams commented 9 years ago

Yes please. This makes the API very painful to use as it stands, as you have to clone input at every call site to ensure that they don't get mutated

Klortho commented 8 years ago

+1 I was shocked to find that even new tinycolor(old) doesn't make a copy.

cpsubrian commented 8 years ago

👍 Ran into this and was majorly hard to track down what was breaking.

bgrins commented 8 years ago

Would anyone like to take this on?

cpsubrian commented 8 years ago

If I get a chance this weekend I'll work up a PR.

bgrins commented 8 years ago

If I get a chance this weekend I'll work up a PR

thanks!

bgrins commented 8 years ago

Fixed by https://github.com/bgrins/TinyColor/pull/132