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

Some colors get rounded by iro.Color #26

Closed
nickpearson opened this issue Feb 3, 2018 · 5 comments
Closed

Some colors get rounded by iro.Color #26

nickpearson opened this issue Feb 3, 2018 · 5 comments
Assignees
Labels

Comments

@nickpearson
Copy link

iro.Color seems to round the rgb values in some cases:

new iro.Color("#fff").hexString //=> "#fff"    (ok)
new iro.Color("#eee").hexString //=> "#ededed" (rounded)
new iro.Color("#ddd").hexString //=> "#dedede" (rounded)
new iro.Color("#ccc").hexString //=> "#ccc"    (ok)

You can also see this effect by modifying the CodePen Demo (linked on the intro page) to use #eee as the color option rather than {r: 255, g: 0, b: 0}.

@jaames jaames self-assigned this Feb 3, 2018
@jaames jaames added the bug label Feb 3, 2018
@jaames
Copy link
Owner

jaames commented Feb 4, 2018

Thanks for letting me know!

I think it might be due to how all iro.Color instances use HSV colors internally, and some rounding when converting RGB -> HSV -> RGB is causing the effect you're seeing. I'll have to investigate further, though

@mbaierl
Copy link

mbaierl commented Mar 21, 2018

Just noticed the same issue... #FF0FF0 -> #FF0FEF and some others...

@jaames
Copy link
Owner

jaames commented Mar 22, 2018

Finally found the cause; it was a combination of excessive rounding and a strange quirk where converting decimal numbers to hex with .toString(16) would produce unexpected output

I have a working fix in the bug/color-rounding branch if anyone wants to try it

One minor side effect of this fix is that hexString will only provide 6-digit hex output now (e.g new iro.Color("#fff").hexString will be "#ffffff", not "#fff" as before). I can reimplement the shorthand 3-digit output if someone complains, but tbh I feel like keeping it consistent makes more sense anyway

@jaames
Copy link
Owner

jaames commented Mar 22, 2018

Okay I think I got it, the fix should be in master now. Thanks @nickpearson and @mbaierl :)

@jaames jaames closed this as completed Mar 22, 2018
@nickpearson
Copy link
Author

Works great. Thank you!

I agree that always using the full hex string for consistency is good. If you do happen to decide to convert to the shorthand hex value, here's a simple implementation:

function shortHex(hex) {
  return hex.replace(/^#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/, "#$1$2$3")
}
shortHex("#aabbcc") //=> "#abc"
shortHex("#aabbcd") //=> "#aabbcd"
shortHex("testing") //=> "testing" (returns non-compactible values untouched)
shortHex("#abc")    //=> "#abc"

This implementation of course doesn't do validation and only compacts all-lowercase hex strings, but it's a start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants