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

Fix(adjustHue): account for potential negative hue value #438

Merged
merged 4 commits into from May 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/color/adjustHue.js
Expand Up @@ -32,7 +32,7 @@ function adjustHue(degree: number | string, color: string): string {
const hslColor = parseToHsl(color)
return toColorString({
...hslColor,
hue: (hslColor.hue + parseFloat(degree)) % 360,
hue: hslColor.hue + parseFloat(degree),
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/internalHelpers/_hslToRgb.js
Expand Up @@ -21,10 +21,10 @@ function hslToRgb(
return convert(lightness, lightness, lightness)
}

// formular from https://en.wikipedia.org/wiki/HSL_and_HSV
const huePrime = hue % 360 / 60
// formulae from https://en.wikipedia.org/wiki/HSL_and_HSV
const huePrime = (((hue % 360) + 360) % 360) / 60
const chroma = (1 - Math.abs(2 * lightness - 1)) * saturation
const secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1))
const secondComponent = chroma * (1 - Math.abs((huePrime % 2) - 1))

let red = 0
let green = 0
Expand Down
18 changes: 18 additions & 0 deletions src/internalHelpers/test/__snapshots__/_hslToRgb.test.js.snap
Expand Up @@ -6,6 +6,24 @@ Object {
}
`;

exports[`hslToRgb should convert correctly even when passed a "faulty" negative hue 1`] = `
Object {
"background": "rgb(255,0,43)",
}
`;

exports[`hslToRgb should convert correctly even when passed a "faulty" negative hue 2`] = `
Object {
"background": "rgb(85,0,255)",
}
`;

exports[`hslToRgb should convert correctly even when passed a "faulty" negative hue 3`] = `
Object {
"background": "rgb(170,255,0)",
}
`;

exports[`hslToRgb should convert numbers to a hex color 1`] = `
Object {
"background": "rgb(179,25,25)",
Expand Down
6 changes: 6 additions & 0 deletions src/internalHelpers/test/_hslToRgb.test.js
Expand Up @@ -34,4 +34,10 @@ describe('hslToRgb', () => {
it('should convert black', () => {
expect({ background: `rgb(${hslToRgb(360, 0, 0.4)})` }).toMatchSnapshot()
})

it('should convert correctly even when passed a "faulty" negative hue', () => {
expect({ background: `rgb(${hslToRgb(-10, 1, 0.5)})` }).toMatchSnapshot()
expect({ background: `rgb(${hslToRgb(-100, 1, 0.5)})` }).toMatchSnapshot()
expect({ background: `rgb(${hslToRgb(-1000, 1, 0.5)})` }).toMatchSnapshot()
})
})