From 0b7c3a899439aabb170ab69471c24b926646735b Mon Sep 17 00:00:00 2001 From: Mark Dimitry Date: Mon, 16 Nov 2020 17:06:17 -0500 Subject: [PATCH 01/11] update float precision of hue, saturation, lightness, and alpha --- src/utilities/color-transformers.ts | 17 ++++++------ .../tests/color-transformers.test.ts | 27 ++++++++++++++----- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/utilities/color-transformers.ts b/src/utilities/color-transformers.ts index 474bf2467bd..ca43f1a2317 100644 --- a/src/utilities/color-transformers.ts +++ b/src/utilities/color-transformers.ts @@ -165,14 +165,15 @@ function rgbToHsbl(color: RGBAColor, type: 'b' | 'l' = 'b'): HSBLAColor { huePercentage = (red - green) / delta + 4; } - const hue = Math.round((huePercentage / 6) * 360); + const hue = (huePercentage / 6) * 360; + const clampedHue = clamp(hue, 0, 360); return { - hue: clamp(hue, 0, 360) || 0, - saturation: parseFloat(clamp(saturation, 0, 1).toFixed(2)), + hue: clampedHue ? parseFloat(clampedHue.toFixed(2)) : 0, + saturation: parseFloat(clamp(saturation, 0, 1).toFixed(4)), brightness: parseFloat(clamp(largestComponent, 0, 1).toFixed(4)), - lightness: parseFloat(lightness.toFixed(2)), - alpha: parseFloat(alpha.toFixed(2)), + lightness: parseFloat(lightness.toFixed(4)), + alpha: parseFloat(alpha.toFixed(4)), }; } @@ -281,9 +282,9 @@ function hslToObject(color: string): HSLAColor { const [hue, saturation, lightness, alpha] = colorMatch[1].split(','); const objColor = { - hue: parseInt(hue, 10), - saturation: parseInt(saturation, 10), - lightness: parseInt(lightness, 10), + hue: parseFloat(hue), + saturation: parseFloat(saturation), + lightness: parseFloat(lightness), alpha: parseFloat(alpha) || 1, }; return objColor; diff --git a/src/utilities/tests/color-transformers.test.ts b/src/utilities/tests/color-transformers.test.ts index d03b5555f74..84b20cc4a49 100644 --- a/src/utilities/tests/color-transformers.test.ts +++ b/src/utilities/tests/color-transformers.test.ts @@ -131,6 +131,19 @@ describe('colorUtilities', () => { expect(saturation).toBe(0); expect(brightness).toBe(0.5294); }); + + // test for an issue where Hex colours were losing precision, due to hue rounding + // https://github.com/Shopify/shopify/issues/265949 + it('returns 16.5517/0.521/0.6549 hsb value when passed rgb(167, 104, 80)', () => { + const {hue, saturation, brightness} = rgbToHsb({ + red: 167, + green: 104, + blue: 80, + }); + expect(hue).toBe(16.55); + expect(saturation).toBe(0.521); + expect(brightness).toBe(0.6549); + }); }); describe('colorToHsla', () => { @@ -138,7 +151,7 @@ describe('colorUtilities', () => { expect(colorToHsla('#dddddd')).toStrictEqual({ alpha: 1, hue: 0, - lightness: 87, + lightness: 86.67, saturation: 0, }); }); @@ -146,9 +159,9 @@ describe('colorUtilities', () => { it('returns the hsla color for rgb', () => { expect(colorToHsla('rgb(132, 11, 2)')).toStrictEqual({ alpha: 1, - hue: 4, - lightness: 26, - saturation: 97, + hue: 4.15, + lightness: 26.27, + saturation: 97.00999999999999, }); }); @@ -164,9 +177,9 @@ describe('colorUtilities', () => { it('returns the hsla color for rgba', () => { expect(colorToHsla('rgb(132, 11, 2, 0.2)')).toStrictEqual({ alpha: 1, - hue: 4, - lightness: 26, - saturation: 97, + hue: 4.15, + lightness: 26.27, + saturation: 97.00999999999999, }); }); From 32e3c82c39ece26a5f04ac7a81d2c85dcbb7d1df Mon Sep 17 00:00:00 2001 From: Mark Dimitry Date: Mon, 16 Nov 2020 17:24:00 -0500 Subject: [PATCH 02/11] add UNRELEASED.md entry --- UNRELEASED.md | 1 + 1 file changed, 1 insertion(+) diff --git a/UNRELEASED.md b/UNRELEASED.md index 44a2d18278c..df80d4f0696 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -20,6 +20,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f - Removed `tabIndex=-1` from `Popover` when `preventAutoFocus` is true ([#3595](https://github.com/Shopify/polaris-react/pull/3595)) - Fixed `Modal` header border color ([#3616](https://github.com/Shopify/polaris-react/pull/3616)) - Fixed `Banner` `secondaryAction` only rendering if `action` is set ([#2949](https://github.com/Shopify/polaris-react/pull/2949)) +- Increased precision of hue, saturation, lightness, and alpha in HSBLA `color-transformers` (https://github.com/Shopify/polaris-react/pull/3640) ### Documentation From 70dd23821f50a46778493580a0016ee1ea1c79f2 Mon Sep 17 00:00:00 2001 From: Mark Dimitry Date: Mon, 16 Nov 2020 18:30:06 -0500 Subject: [PATCH 03/11] add roundNumberToDecimalPlaces for accurate rounding --- UNRELEASED.md | 1 + src/utilities/roundNumberToDecimalPlaces.ts | 5 +++++ src/utilities/tests/roundNumberToDecimals.test.ts | 10 ++++++++++ 3 files changed, 16 insertions(+) create mode 100644 src/utilities/roundNumberToDecimalPlaces.ts create mode 100644 src/utilities/tests/roundNumberToDecimals.test.ts diff --git a/UNRELEASED.md b/UNRELEASED.md index df80d4f0696..662285dfd96 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -12,6 +12,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f - **`Button`:** New `role` prop for `