2

I have RGB colors in the form:

[0.00784, 0.62745, 0.77647]

I would like to convert them to hex color, but I'm not sure how. I've seen other types of RGB components converted to hex, but not in this format.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
user379468
  • 3,989
  • 10
  • 50
  • 68
  • Is it just a normalized version from 0 to 1? If so just `*255` and do a [rgb to hex conversion](http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb). – Spencer Wieczorek Aug 20 '16 at 17:16

1 Answers1

2

Multiply all the values by 255, round it and then use the solution from this question.

const componentToHex = c => {
  const hex = c.toString(16)
  return hex.length === 1 ? '0' + hex : hex
}

const rgbToHex = (r, g, b) => '#' + [r, g, b].map(componentToHex).join('')

const rgb = [0.00784, 0.62745, 0.77647].map(x => Math.round(x * 255))
console.log(rgbToHex(...rgb))
Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177