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

Fails to generate colors for small values #3

Closed
dkoprowski opened this issue Mar 9, 2021 · 2 comments · Fixed by #4
Closed

Fails to generate colors for small values #3

dkoprowski opened this issue Mar 9, 2021 · 2 comments · Fixed by #4

Comments

@dkoprowski
Copy link

Hi, I've found a bug that when opacity value is really small (like 1% - 4%) function will generate wrong hex.

Failing test example:

    Expected: "#FF700003"
    Received: "#FF70003"

      32 |   it('returns proper values', () => {
      33 |     expect(opacity('#FF7000', 0.0)).toBe('#FF700000')
    > 34 |     expect(opacity('#FF7000', 0.01)).toBe('#FF700003')

Fix

To fix I changed those lines:

  if (opacity === 0) color += '00'
  else if (opacity !== 1) color += Math.round(opacity * 255).toString(16)
  return `#${color}`

into this:

  const hexOpacity = Math.round(percentage * 255)
    .toString(16)
    .padStart(2, '0')
    .toUpperCase()

  return `#${color}${hexOpacity}`

This adds zeros at the beginning if generated opacity doesn't contain 2 characters. This also works for 0.0 value so no if needed.

@dominicegginton
Copy link
Owner

Hi @dkoprowski, a fix has been merged and will be released in 0.1.2 soon. Thanks for reporting 😄

@dkoprowski
Copy link
Author

Wow that was fast :)

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

Successfully merging a pull request may close this issue.

2 participants