Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:14:17+00:00 2026-06-09T14:14:17+00:00

Using javascript/jquery, I want to sort an array of rgba values to the colours

  • 0

Using javascript/jquery, I want to sort an array of rgba values to the colours of the visible spectrum. By doing this, like shades should be bunched together. Is there a plugin to do this or how would I go about doing it?

Spectrum image: http://www.gamonline.com/catalog/colortheory/images/spectrum.gif

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:14:18+00:00Added an answer on June 9, 2026 at 2:14 pm

    If your array of colors is like this:

    var rgbArr = [c1, c2, c3, ...]
    

    where each color ci is an array of three numbers between 0 and 255

    ci = [red, green, blue]
    

    then, you can use this function to convert the colors to HSL

    function rgbToHsl(c) {
      var r = c[0]/255, g = c[1]/255, b = c[2]/255;
      var max = Math.max(r, g, b), min = Math.min(r, g, b);
      var h, s, l = (max + min) / 2;
    
      if(max == min) {
        h = s = 0; // achromatic
      } else {
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
          case r: h = (g - b) / d + (g < b ? 6 : 0); break;
          case g: h = (b - r) / d + 2; break;
          case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
      }
      return new Array(h * 360, s * 100, l * 100);
    }
    

    and sort them by hue

    var sortedRgbArr = rgbArr.map(function(c, i) {
      // Convert to HSL and keep track of original indices
      return {color: rgbToHsl(c), index: i};
    }).sort(function(c1, c2) {
      // Sort by hue
      return c1.color[0] - c2.color[0];
    }).map(function(data) {
      // Retrieve original RGB color
      return rgbArr[data.index];
    });
    

    Here is a runnable example (thanks Ionică Bizău):

    function display(container, arr) {
      container = document.querySelector(container);
      arr.forEach(function(c) {
        var el = document.createElement("div");
        el.style.backgroundColor = "rgb(" + c.join(", ") + ")";
        container.appendChild(el);
      })
    }
    function rgbToHsl(c) {
      var r = c[0] / 255,
          g = c[1] / 255,
          b = c[2] / 255;
      var max = Math.max(r, g, b),
          min = Math.min(r, g, b);
      var h, s, l = (max + min) / 2;
    
      if (max == min) {
        h = s = 0; // achromatic
      } else {
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch (max) {
          case r:
            h = (g - b) / d + (g < b ? 6 : 0);
            break;
          case g:
            h = (b - r) / d + 2;
            break;
          case b:
            h = (r - g) / d + 4;
            break;
        }
        h /= 6;
      }
      return new Array(h * 360, s * 100, l * 100);
    }
    
    var rgbArr = [];
    for (var i = 0; i < 100; ++i) {
      rgbArr.push([
        Math.floor(Math.random() * 256),
        Math.floor(Math.random() * 256),
        Math.floor(Math.random() * 256)
      ]);
    }
    display("#before", rgbArr);
    
    var sortedRgbArr = rgbArr.map(function(c, i) {
      // Convert to HSL and keep track of original indices
      return {color: rgbToHsl(c), index: i};
    }).sort(function(c1, c2) {
      // Sort by hue
      return c1.color[0] - c2.color[0];
    }).map(function(data) {
      // Retrieve original RGB color
      return rgbArr[data.index];
    });
    display("#after", sortedRgbArr);
    #before > div,
    #after > div {
      width: 1%;
      height: 20px;
      display: inline-block;
    }
    Random colors: <div id="before"></div>
    Same colors, sorted by hue: <div id="after"></div>

    sortedRgbArr will contain the rgb colors of rgbArr sorted more or less like the colors of the visible spectrum.

    The problem is that the HSL spectrum looks like this:

    HSL spectrum

    Your spectrum is weird because it doesn’t have all colors, such as pink.

    I guess that’s because pink doesn’t exist in the nature, it’s a combination of the colors of the opposite extremes of light’s spectrum. But we have it in rgb, so you have to decide where do you want it.

    Moreover, it seems that your spectrum goes from lower to higher wavelength, not frequency. But then your spectrum is a reverse of HSL’s spectrum.

    Replace c1.color[0] - c2.color[0] with c2.color[0] - c1.color[0] if you want it like your spectrum.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to enable facebox jquery using javascript. here in normal html <a href=stairs.jpg
I was creating a countdown timer using javascript; I can use jQuery. I want
I want to set focus on particular div using jquery or javascript. i have
How to generate page numbers like the below using javascript/jquery? If the 5 th
I want to get the current URL using javascript/jquery. The URL should include subfolders/subsites
I want to add one more li at last but using JavaScript/jQuery for example
I'm doing sorting of a list of elements, using: jQuery.fn.sort = function() { return
does anybody know how can I sort words in string using javascript, jquery. For
Is it possible to overwrite CSS file permanently using JavaScript / jQuery? I want
I want to detect (using javascript/jQuery) whether the user's browser supports multiple attribute to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.