Home > other >  Get 9 most common RBG values from table of RGB values
Get 9 most common RBG values from table of RGB values

Time:11-13

Quick question.

I've got a table as such:

[
  [ 86, 81, 89 ],    [ 86, 81, 89 ],    [ 86, 81, 89 ],    [ 83, 78, 84 ],
  [ 86, 81, 89 ],    [ 83, 78, 84 ],    [ 86, 81, 89 ],    [ 86, 81, 89 ],
  [ 83, 78, 84 ],    [ 83, 78, 84 ],    [ 80, 74, 80 ],    [ 76, 69, 76 ],
  [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 73, 65, 71 ],    [ 67, 63, 70 ],
  [ 67, 63, 70 ],    [ 68, 60, 64 ],    [ 66, 56, 58 ],    [ 57, 51, 59 ],
  [ 66, 56, 58 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],
  [ 68, 60, 64 ],    [ 67, 63, 70 ],    [ 73, 65, 71 ],    [ 76, 69, 76 ],
  [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 73, 65, 71 ],
  [ 73, 65, 71 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],
  [ 66, 56, 58 ],    [ 57, 51, 59 ],    [ 63, 52, 51 ],    [ 63, 52, 51 ],
  [ 56, 47, 48 ],    [ 56, 47, 48 ],    [ 57, 51, 59 ],    [ 63, 52, 51 ],
  [ 57, 51, 59 ],    [ 66, 56, 58 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],
  [ 66, 56, 58 ],    [ 57, 51, 59 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],
  [ 67, 63, 70 ],    [ 76, 69, 76 ],    [ 83, 78, 84 ],    [ 86, 81, 89 ],
  [ 86, 81, 89 ],    [ 93, 87, 96 ],    [ 100, 96, 105 ],  [ 109, 104, 108 ],
  [ 115, 108, 112 ], [ 119, 114, 120 ], [ 128, 122, 125 ], [ 134, 130, 137 ],
  [ 134, 130, 137 ], [ 127, 125, 135 ], [ 134, 130, 137 ], [ 134, 130, 137 ],
  [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ],
  [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ], [ 135, 133, 143 ],
  [ 143, 140, 145 ], [ 146, 143, 149 ], [ 149, 146, 152 ], [ 149, 146, 152 ],
  [ 149, 146, 152 ], [ 152, 145, 147 ], [ 149, 146, 152 ], [ 149, 143, 144 ],
  [ 146, 143, 149 ], [ 143, 140, 145 ], [ 143, 140, 145 ], [ 141, 137, 141 ],
  [ 149, 146, 152 ], [ 157, 151, 154 ], [ 162, 160, 165 ], [ 168, 166, 171 ],
  [ 174, 169, 172 ], [ 174, 169, 172 ], [ 168, 166, 171 ], [ 169, 164, 165 ],
  [ 166, 160, 161 ], [ 160, 154, 155 ], [ 155, 148, 149 ], [ 149, 143, 144 ],
  ... 50525 more items
]

How would i get the 9 most common RGB values in that there table?

Thanks in advance.

CodePudding user response:

Here's one way to do it. You would need to use your own, full array here. I used only the values you supplied. I am also not returning only the 9 most common rgb values, but all of them in order from most common to least common, so you would have to take the first 9 elements from the sortableRankings-array to get exactly the 9 most common rgbs.

const rgbs = [
    [ 86, 81, 89 ],    [ 86, 81, 89 ],    [ 86, 81, 89 ],    [ 83, 78, 84 ],
    [ 86, 81, 89 ],    [ 83, 78, 84 ],    [ 86, 81, 89 ],    [ 86, 81, 89 ],
    [ 83, 78, 84 ],    [ 83, 78, 84 ],    [ 80, 74, 80 ],    [ 76, 69, 76 ],
    [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 73, 65, 71 ],    [ 67, 63, 70 ],
    [ 67, 63, 70 ],    [ 68, 60, 64 ],    [ 66, 56, 58 ],    [ 57, 51, 59 ],
    [ 66, 56, 58 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],
    [ 68, 60, 64 ],    [ 67, 63, 70 ],    [ 73, 65, 71 ],    [ 76, 69, 76 ],
    [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 76, 69, 76 ],    [ 73, 65, 71 ],
    [ 73, 65, 71 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],    [ 68, 60, 64 ],
    [ 66, 56, 58 ],    [ 57, 51, 59 ],    [ 63, 52, 51 ],    [ 63, 52, 51 ],
    [ 56, 47, 48 ],    [ 56, 47, 48 ],    [ 57, 51, 59 ],    [ 63, 52, 51 ],
    [ 57, 51, 59 ],    [ 66, 56, 58 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],
    [ 66, 56, 58 ],    [ 57, 51, 59 ],    [ 66, 56, 58 ],    [ 68, 60, 64 ],
    [ 67, 63, 70 ],    [ 76, 69, 76 ],    [ 83, 78, 84 ],    [ 86, 81, 89 ],
    [ 86, 81, 89 ],    [ 93, 87, 96 ],    [ 100, 96, 105 ],  [ 109, 104, 108 ],
    [ 115, 108, 112 ], [ 119, 114, 120 ], [ 128, 122, 125 ], [ 134, 130, 137 ],
    [ 134, 130, 137 ], [ 127, 125, 135 ], [ 134, 130, 137 ], [ 134, 130, 137 ],
    [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ],
    [ 134, 130, 137 ], [ 134, 130, 137 ], [ 134, 130, 137 ], [ 135, 133, 143 ],
    [ 143, 140, 145 ], [ 146, 143, 149 ], [ 149, 146, 152 ], [ 149, 146, 152 ],
    [ 149, 146, 152 ], [ 152, 145, 147 ], [ 149, 146, 152 ], [ 149, 143, 144 ],
    [ 146, 143, 149 ], [ 143, 140, 145 ], [ 143, 140, 145 ], [ 141, 137, 141 ],
    [ 149, 146, 152 ], [ 157, 151, 154 ], [ 162, 160, 165 ], [ 168, 166, 171 ],
    [ 174, 169, 172 ], [ 174, 169, 172 ], [ 168, 166, 171 ], [ 169, 164, 165 ],
    [ 166, 160, 161 ], [ 160, 154, 155 ], [ 155, 148, 149 ], [ 149, 143, 144 ]
  ]

  // Something to save the rankings in
  const rankings = {};

  // Loop through the rgb values
  for(let i = 0; i < rgbs.length; i  )
  {
      const rgb = rgbs[i];
      const rgbString = rgb.join(','); // Make a "key" from the rgb triplet
      
      // Store the ranking, or increment it by one if it already exists
      if(!(rgbString in rankings))
          rankings[rgbString] = 1;
      else
          rankings[rgbString]  ;
  }

  // Can't easily sort rankings object and we need to use an object in the previous step to easily find existing rgb rankings by key,
  // So now turn that object into an array which we can sort by the ranking
  const sortableRankings = [];

  for(let ranking in rankings)
  {
      sortableRankings.push([ranking, rankings[ranking]]);
  }


  // Sort by ranking from highest to lowest and log it out
  console.log(sortableRankings.sort((a, b) => b[1] - a[1]));
  • Related