Take the separate background gradient values and save to a new array

Asked

Viewed 42 times

-4

I have these background attributes of an element:

"background: rgba(0, 0, 0, 0) linear-gradient(0.25turn, rgb(117, 117, 117), rgb(46, 184, 46), rgb(21, 21, 21), rgb(166, 166, 166)) repeat scroll 0% 0%;"

How can I take the 4 different colors of the gradient and separate them into an array like this:

  [117,117,117],[46,184,46],[21,21,21],[45,175,230],[166,166,166],

I think the way I’m trying is wrong: (I left well divided to understand better)

    var colorsP = "background: rgba(0, 0, 0, 0) linear-gradient(0.25turn, rgb(117, 117, 117), rgb(46, 184, 46), rgb(21, 21, 21), rgb(166, 166, 166)) repeat scroll 0% 0%;"

    colorsP = colorsP.split('0.25turn, ');
    colorsP = colorsP[1];
    colorsP = colorsP.split(') ');
    colorsP = colorsP[0];
    colorsP = colorsP.split('rgb');
    colorsP = colorsP.filter((value)=>value!='');
    
    console.log(colorsP);

1 answer

0


I think that solves your problem: Using a regular expression to identify the contents of each rgb(R,G,B), where R, G and B are numerical values:

From these, iterate for each string rgb(R, G, B) and use another ER to identify numerical values. After that, iterate through the results and place each one in an array.

Finally, create an array with the previous arrays.

const er = new RegExp(/rgb\(.*?\)/gi); //ER que identifica os textos 'rgb(R, G, B)'
const texto = "background: rgba(0, 0, 0, 0) linear-gradient(0.25turn, rgb(117, 117, 117), rgb(46, 184, 46), rgb(21, 21, 21), rgb(166, 166, 166)) repeat scroll 0% 0%;"; // o texto completo

matches = [... texto.matchAll(er)]; //criando um vetor com os resultados encontrados no texto completo

valores = matches.map(function(v){ // passando por cada string do vetor acima (que tem o conteúdo similar a 'rgb(R, G, B)'
    erV = new RegExp(/\d+/g); //criando uma ER para identficar os números dentro do texto atual
    resultados = [... v[0].matchAll(erV)]; // encontrando todos 
    arrayRetorno = resultados.map(function(vr){ //passando por cada um deles e...
         return parseInt(vr[0]); // ... retornando como números inteiros
    })
    return arrayRetorno; // devolvendo o vetor atual para o 'vetor de vetores'
}); 

console.log(valores);

Will result in:

[[117, 117, 117], [46, 184, 46], [21, 21, 21], [166, 166, 166]]

Remember that any change that affects the pattern rbg(R, G, B) (for example of rgb(0,0,0) for #000000) will cause failures.

  • Thanks Marcos, you killed him. I need to learn more about regular expressions because I couldn’t understand anything about how you identified the characters. Can you tell me why the question was so negative? It’s a bad question? Was it the way I wrote it ? Or my solution was so far away that I really deserved it ?

  • Dispose, we’re here to help. About the ER, at first it’s kind of scary really, but a little training and reading, quickly you get the hang of it. I suggest you use https://regexr.com/ to play with it. There’s a bit of explanation and you can see right away how the ER works. About the negativity of the question, it is very subjective, each vote comes from a person and each one with a point of view. In any case, read the website helps, especially the part of the questions that offer great tips.

  • 1

    Thanks Marcão, I’ll take a look! I edited the question, I think it looks better. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.