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

107
Visualizações
How to isolate primary colors from a background color extraction from css

I want to make a non-canvas html element change color in a pattern based off it's current color using javascript

in the example, boxyBox has a background color of rgba(100, 100, 100, 0.9), and I want to change the background color of any element with an alpha value of less than 1

var box = document.getElementById("boxyBox");
var styleOfBox = window.getComputedStyle(box);
var colorOfBox = styleOfBox.getPropertyValue("background-color");
console.log(colorOfBox); //returns rgba(100, 100, 100, 0.9);
console.log(typeof colorOfBox); //returns string
var alphaOfBox = ???;
if(alphaOfBox < 1) {
    box.style.color = red;
}

Is there a way to extract individual primary color values, or to parse them from the background color string?

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

0

After a few hours more of fumbling about, I found this solution

var box = document.getElementById("boxyBox");
var styleOfBox = window.getComputedStyle(box);
var colorArr = styleOfBox.getPropertyValue("background-color").split(", ");
var red = parseFloat(colorArr[0].match(/[\d\.]+/)[0]);
var green = parseFloat(colorArr[1]);
var blue;
var alpha;
if(typeof colorArr[3] == "string") {
    alpha = parseFloat(colorArr[3].match(/[\d\.]+/)[0]);
    blue = parseFloat(colorArr[2]);
else {
    blue = parseFloat(colorArr[2].match(/[\d\.]+/)[0]);
    alpha = 1;
}

these variables now correspond to the primary computer color they are named after, including non-rgba and decimal values in the stylesheet

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