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

alpha precision #174

Closed
ferllings opened this issue Mar 6, 2020 · 6 comments
Closed

alpha precision #174

ferllings opened this issue Mar 6, 2020 · 6 comments
Labels

Comments

@ferllings
Copy link

Hello,

When I parse hex values with alpha #000000aa
I get { model: 'rgb', color: [ 0, 0, 0 ], valpha: 0.67 }
The same valpha value is calculated for #000000ab

This is probably due to the rounding when converting from 255 base to 1 base.
Is there a way to access the intermediate 255 base value?

I understand the difference isn't much between both values, but the problem is that I can't retrieve the original hex value from the object... both giving #000000aa

Any idea?

@Qix-
Copy link
Owner

Qix- commented Mar 6, 2020

I don't quite understand what you're asking - can you show some code with expected output and what you're really getting?

@Qix- Qix- added the question label Mar 6, 2020
@ferllings
Copy link
Author

ferllings commented Mar 6, 2020

Color("#000000aa") returns
{ model: 'rgb', color: [ 0, 0, 0 ], valpha: 0.67 }
Color("#000000ab") returns
{ model: 'rgb', color: [ 0, 0, 0 ], valpha: 0.67 }

So 2 hexa values produce the same object

And of course when I'm trying to reconstitute the alpha value using the object I get the same result for both. "aa" which is not correct for #000000ab

@Qix-
Copy link
Owner

Qix- commented Oct 9, 2020

Wow, this is definitely a bug. Thank you for reporting.

I'm actually surprised we support RGBA as the constructor - I don't remember that ever being the case (though it has been a while since I've worked on this module).

I originally thought this could have been a precision error in the node prompt but doing a comparison of the two values shows that it is indeed the same value:

Color("#000000ab").valpha == Color("#000000aa").valpha
> true

@Qix- Qix- added bug and removed question labels Oct 9, 2020
@Qix-
Copy link
Owner

Qix- commented Oct 9, 2020

Okay so digging deep into color-string's history, this was a bug that was introduced as a solution to another bug.

  • Hex support wasn't added until Qix-/color-string#33
  • A bug was uncaught there that rounded the alpha to either 0 or 1, which was "fixed" in Qix-/color-string@eb857c2
  • However, the real fix should have been simply to remove the Math.round. Not sure what the rationale was to lock it to two digit precision (perhaps for aesthetic? which I'm over now). But there was no technical reason for it that I can find, and the other string parsers do not make the same guarantee.

I'll fix this in color-string and release shortly. Apologies for the delay, the pandemic made life a lot harder.

EDIT: npm has insanely aggressive caching now apparently, so the newly pushed color-string version isn't showing up yet. I'll update color when it finally does.

@Qix- Qix- closed this as completed in d78959c Oct 9, 2020
@Qix-
Copy link
Owner

Qix- commented Oct 9, 2020

Released as 3.1.3, thanks again for the report and apologies for how long it took to address.

@ferllings
Copy link
Author

Thanks a lot.
And no worries, I know what it's like to work on opensource projects.

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

2 participants