How do I convert RGB decimal to Hex colors in JavaScript?

user379468

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

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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert hex to decimal in Python?

How do I convert hex to decimal in Python?

How do I convert a hex triplet to an RGB tuple and back?

How do I convert '\x80' hex to decimal

How to convert hex to decimal rgb565 using java?

How to convert hex to decimal WITH A LOOP in JavaScript

How to convert HTML String with RGB colors to HTML String with HEX colors in Android JAVA

How to convert ARGB to RGB and HEX?

How can I convert RGB hex value into UIColor in Swift?

How do convert unsigned hex to decimal in Windows 10 Calculator?

How would I use js to convert a decimal to number to a hex string?

How do I convert a string into HEX representation?

How do I convert a string to hex in Rust?

Can I compare two RGB colors in Javascript? And how?

How do I convert RGB to float values?

Can I/how to specify colors in hex or RGB in nano syntax highlight config?

How do I convert sum of hex values back to hex values

How do i convert from ASCII to Decimal

How to convert hex to rgb using Java?

How to convert vector of RGB values to Hex

How to convert RGBA colors (with opacity) to hex/solid colors?

How to properly convert hsl colors to rgb colors in lua?

Assembly: How to convert hex input to decimal?

How to convert hex string into decimal value

How to convert Decimal To Hex using VB?

Python How to convert a float as hex to decimal

How to convert decimal to hexadecimal and XOR of the hex string

How to convert a hex decimal column in scala to int

How to convert big hex strings to decimal strings?