Skip to content

Added Chroma.js workaround for it not yet supporting 'deg' units: #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/colorStops.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ module.exports = (colors, coordinates, alphaDecimals = 5, colorMode = 'lrgb') =>
coordinates.forEach(coordinate => {
const ammount = coordinate.y
const percent = coordinate.x * 100
let color = chroma.mix(colors[0], colors[1], ammount, colorMode).css('hsl')
let color = chroma.mix(
helpers.chromaDegreesWorkaround(colors[0]),
helpers.chromaDegreesWorkaround(colors[1]),
ammount,
colorMode
).css('hsl')
color = helpers.roundHslAlpha(color, alphaDecimals)
if (Number(coordinate.x) !== 0 && Number(coordinate.x) !== 1) {
colorStops.push(`${color} ${+percent.toFixed(2)}%`)
Expand Down
22 changes: 22 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ exports.roundHslAlpha = (color, alphaDecimals) => {
return color
}

/**
* Chroma.js 'deg' unit workaround.
*
* Chroma.js doesn't yet support hsl()/hsla() colors that have the 'deg' unit,
* so this strips the unit out to prevent Chroma.js throwing an error. If no
* unit is specified for the hue value, the CSS spec says that it must be
* interpreted as degrees for backward compatibility.
*
* @param {String} color A CSS color value.
*
* @return {String} The color parameter with any 'deg' unit stripped from the
* hue.
*
* @see https://github.com/gka/chroma.js/issues/297
* Open Chroma.js issue regarding the newer color formats.
*
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl
*/
exports.chromaDegreesWorkaround = (color) => {
return color.replaceAll(/(hsla?\(\s*\d+)deg([,\s].+)$/gm, '$1$2');
}

/**
* Wrap a string telling the user we couldn't parse it
* @param {String} input A string
Expand Down