From 62c66166ab7c5e98f31b6ccff2c1403dd47f046e Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Tue, 6 Feb 2024 17:50:37 -0600 Subject: [PATCH] convert hex to rgb --- src/colorbar.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/colorbar.js b/src/colorbar.js index c93e2f8..3fb56d9 100644 --- a/src/colorbar.js +++ b/src/colorbar.js @@ -25,12 +25,24 @@ const DIMENSIONS = { height: ['80px', '110px', '110px', '130px'], } +const hexToRgb = (hex) => { + let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) + return result + ? `${parseInt(result[1], 16)}, ${parseInt(result[2], 16)}, ${parseInt( + result[3], + 16 + )}` + : null +} + const Gradient = ({ colormap, discrete, horizontal, width, height }) => { const step = (1 / colormap.length) * 100 - const values = colormap.map((d, i) => { - return `rgb(${d}) ${i * step}% ${ + const values = colormap.map((color, i) => { + const rgbColor = color.startsWith('#') ? hexToRgb(color) : color + const position = `${i * step}% ${ discrete && i < colormap.length - 1 ? `${(i + 1) * step}%` : '' }` + return `rgb(${rgbColor}) ${position}` }) const css = `linear-gradient(to ${ @@ -114,6 +126,7 @@ const Colorbar = ({ bottom = false, sx, sxClim, + type, ...props }) => { if (!Array.isArray(colormap)) {