-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[color-transformers > Hsbl] Increase precision of hue, saturation, lightness, and alpha #3640
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0b7c3a8
update float precision of hue, saturation, lightness, and alpha
MLDimitry 32e3c82
add UNRELEASED.md entry
MLDimitry 70dd238
add roundNumberToDecimalPlaces for accurate rounding
MLDimitry 73aa735
improved hsbla rounding in color-transformers
MLDimitry 0a2a175
push eslint ignore
MLDimitry dc99dcc
make rounding function more readable/fix eslint + ts
MLDimitry 87221e8
remove trivially inferred type annotations
MLDimitry daccab1
more rounding of values before being stringified
MLDimitry 89be2ae
update failing tests
MLDimitry 1a49311
Update UNRELEASED.md
MLDimitry ddd0952
Update comment in src/utilities/roundNumberToDecimalPlaces.ts
MLDimitry 7beca04
Merge branch 'master' into color-transformer-moar-precision
MLDimitry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Because everything is a float in JS, Number.toFixed sometimes rounds in the | ||
| // "wrong" direction because of float imprecision. For instance: | ||
| // `(1.005).toFixed(2)` is `1.00`, NOT `1.01` because 1.005 in floating point is | ||
| // actually 1.004999995. By using exponentiation tricks here we can work around | ||
| // this imprecision, so `roundNumberToDecimalPlaces(1.005)` returns the expected | ||
| // value of `1.01` | ||
| // See https://www.jacklmoore.com/notes/rounding-in-javascript/ | ||
| export function roundNumberToDecimalPlaces(value: number, decimals: number) { | ||
| const exponent = Number(`${value}e${decimals}`); | ||
| const roundedExponent = Math.round(exponent); | ||
| const numberWithDecimalPlaces = Number(`${roundedExponent}e-${decimals}`); | ||
| return numberWithDecimalPlaces; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import {roundNumberToDecimalPlaces} from '../roundNumberToDecimalPlaces'; | ||
|
|
||
| describe('roundNumberToDecimalPlaces', () => { | ||
| it('rounds 1.004 to 1.00', () => { | ||
| expect(roundNumberToDecimalPlaces(1.004, 2)).toBe(1.0); | ||
| }); | ||
| it('rounds 1.005 to 1.01', () => { | ||
| expect(roundNumberToDecimalPlaces(1.005, 2)).toBe(1.01); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my continued exploration of floating point fun, I found that
toFixed(2)(which usesMath.roundfor it's rounding) does not accurately round all floating point numbers. i.e. 1.005 in floating point is really 1.004999995, or a value very close but below 1.005 which causes it to be rounded to 1.00 instead of 1.01. (I have a test for this case)Here's an interesting blog post on rounding which led me to this solution which converts the numbers to exponential notation for more accurate rounding.