Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
how to convert rgb color to color name string in javascript

how to convert rgb color to color name string in javascript

 let btn = document.querySelector("#demo");
         let css = getComputedStyle(btn);
           console.log(css.color);

output is in rgb value rgb (255,0,0)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to convert the RGB string to a hex value, and then you can compare it against a list like this of HTML color names.

const colors={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff"};

// Pass in the string
function toHex(rgb) {

  // Grab the numbers
  const match = rgb.match(/\d+/g);

  // `map` over each number and return its hex
  // equivalent making sure to `join` the array up
  // and attaching a `#` to the beginning of the string 
  return `#${match.map(color => {
    const hex = Number(color).toString(16);
    return hex.length === 1 ? `0${hex}` : hex;
  }).join('')}`;
}

// Now just iterate over the colors object and return the
// key from the property where the value matches the hex value
function findColor(hex) {
  const pair = Object.entries(colors).find(([key, value]) => {
    return value === hex;
  });
  return pair[0];
}

const hex1 = toHex('rgb(255, 228, 196)');
console.log(findColor(hex1));

const hex2 = toHex('rgb(127, 255, 212)');
console.log(findColor(hex2));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda