Colors Sorting Light to Dark or Sorting By hue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yudhister jain
    New Member
    • Feb 2021
    • 1

    #1

    Colors Sorting Light to Dark or Sorting By hue

    I have list of color hex code, now i want it to rearrange so that light color comes first, after that color which is darker than previous one comes and so on..
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    I have list of color hex code, now i want it to rearrange so that light color comes first, after that color which is darker than previous one comes and so on..
    Extract the individual RGB components and analyze.

    Comment

    • bitten
      New Member
      • Apr 2022
      • 2

      #3
      and there are different types of sorting algorithms too, depending on the size and numbers of data and the wanted speed of sorting.

      Here's a clip of how I separate A R G B from a color (from a pixel). bad coding guaranteed but it works for me :)

      Code:
                  for (int i = 0; i < MG.bmp.Width; i++)
                  {
      for (int j = 0; j < MG.bmp.Height; j++)
                      {
                          Color picColor = MG.bmp.GetPixel(i, j);
                          int alfa = picColor.A;
                          int red = picColor.R;
                          int green = picColor.G;
                          int blue = picColor.B;
                          int gray = (byte)(.2989 * red + .5870 * green + .1140 * blue);
                          work1[i, j] = gray;
      }
      }

      Good luck

      Comment

      • salmawisoky
        New Member
        • Sep 2023
        • 1

        #4
        To rearrange a list of color hex codes so that lighter colors come first, followed by progressively darker colors, you can compare the brightness or luminosity values of each color. One way to achieve this is by converting the hex codes to the HSL (Hue, Saturation, Lightness) color model and sorting based on the lightness value.

        Here's an example in Python to illustrate the process:

        ```python
        import colorsys

        def get_brightness( hex_code):
        r, g, b = tuple(int(hex_c ode[i:i+2], 16) for i in (0, 2, 4))
        hls = colorsys.rgb_to _hls(r / 255, g / 255, b / 255)
        return hls[1] # Lightness value

        def rearrange_color s(colors):
        return sorted(colors, key=get_brightn ess)

        # Example usage
        color_list = ["#FFFFFF", "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#000000"]
        rearranged_colo rs = rearrange_color s(color_list)
        print(rearrange d_colors)
        ```

        In this example, the `get_brightness ` function takes a hex code, converts it to the RGB color space, and then converts it to the HSL color space using the `colorsys` module. It returns the lightness value (ranging from 0 to 1) of the color.

        The `rearrange_colo rs` function takes a list of color hex codes and uses the `sorted` function with a `key` parameter, which specifies the function to extract the lightness value from each color. The `sorted` function will then sort the colors based on Drift Boss this extracted value.

        The resulting `rearranged_col ors` list will contain the colors sorted from light to dark based on their perceived brightness.

        Note that this example assumes you are using Python, but the concept can be adapted to other programming languages as well. Additionally, keep in mind that color perception can be subjective, so depending on your specific use case, you may need to consider other factors such as saturation or adjust the sorting criteria accordingly.

        Comment

        Working...