Search code examples
javascriptsubstringrgbcolor-pickerextendscript

Delete "," from Strig (and return RGB\n String)


so I've been stuck at this problem for a few days: My Input is this:

"255,255,255,10,251,91,31,4,220,220,220,1"

Its a String with 3 different RGB values, which also come with a number which indicates their quantity going from hightest to lowest. You could translate the String from above to:

"RED1, GREEN1, BLUE1, QUANTITY1, RED2, GREEN2 ... "

While the 1 stands for the first color and 2 for the second and so on.

What I need to return is the first color without its quantity. So my output should look like this:

"255,255,255,251,91,31,220,220,220"

I've tried various of things, one is this function:

var firstC, secondC, thirdC;
var pointer = "firstC";

function getEachColorValue(color) {
    var startIndex = 0;
    var endIndex = 0;

    for (var i = 0; i < color.length; i++) {

        if (color.charAt(i) == ",") {
        endIndex = i;

            if (pointer == "firstC") {
                firstC = color.substring(startIndex, endIndex);
                startIndex = endIndex + 1;
                pointer = "secondC";
            } else if (pointer == "secondC") {
                secondC = color.substring(startIndex, endIndex);
                startIndex = endIndex + 1;
                pointer = "thirdC";
            } else if (pointer == "thirdC") {
                thirdC = color.substring(startIndex, endIndex);
                startIndex = endIndex;
                pointer = "firstC";
            }
        }
    }
}

This pushes RED1 in firstC, GREEN1 in secondC and BLUE1 in thirdC

I thought about doing that one time, use a function to write firstC, secondC, thirdC into an array, reset them. Then cut col3 into a substring without the first color pallet, then repeat.

// Global variables
var stringHolder;
var newString;
var counter = 0;
var colorSetHT = new Array;

// main
createSubstring(col3);

function createSubstring(color) {
    var startIndex = 0;
    var endIndex = 0;

    for (var i = 0; i < color.length; i++) {
        if (color.charAt(i) == ",") {
            counter++;
            endIndex = i;
    }
    if (counter == 4) {
        stringHolder = color.substring(startIndex, endIndex);
        alert(stringHolder);
        newString = color.substring(endIndex+1, color.length);

        getEachColorValue(stringHolder);
        colorSetHT.push(firstC, secondC, thirdC)
        colorReset();

        counter = 0;
        stringHolder = "";
//          createSubstring(newString); // ?
        }
    }
}

I've tried this, but had no luck so far. I even tried to do it recursively. Im kinda new to Javascript (actually doing it for Extendscript), I think theres a way easier way, working with split/slice but I havent been able to find one yet. I tried to make it as easy and fast to read as possible, please let me know if I can provide any further information, thanks in advance!


Solution

  • Here is how to do it with split.

    var input = "255,255,255,10,251,91,31,4,220,220,220,1";
    var inputArray = input.split(",");
    var outputArray = [];
    for(let i = 0;i<inputArray.length;i++)
    {
      if(i%4 != 3)
      {
        outputArray.push(inputArray[i]);
      }
    }
    var output = outputArray.join(",");
    console.log(output);