Skip to content

Commit

Permalink
Merge pull request #172 from carbonplan/handle-hex
Browse files Browse the repository at this point in the history
convert hex to rgb
  • Loading branch information
Shane98c authored Feb 8, 2024
2 parents 12f07e9 + bf94af0 commit 67eb253
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/colorbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ 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 isHex = colormap[0].startsWith('#')
const values = colormap.map((color, i) => {
const rgbColor = isHex ? hexToRgb(color) : color
const position = `${i * step}% ${
discrete && i < colormap.length - 1 ? `${(i + 1) * step}%` : ''
}`
return `rgb(${rgbColor}) ${position}`
})

const css = `linear-gradient(to ${
Expand Down

0 comments on commit 67eb253

Please sign in to comment.