473,718 Members | 2,016 Online

Colors Sorting Light to Dark or Sorting By hue

1 New Member
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..
Feb 15 '21 #1
3 24497
dev7060
644 Recognized Expert Contributor
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.
Feb 15 '21 #2
bitten
2 New Member
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 :)

Expand|Select|Wrap|Line Numbers
1.
2.             for (int i = 0; i < MG.bmp.Width; i++)
3.             {
4. for (int j = 0; j < MG.bmp.Height; j++)
5.                 {
6.                     Color picColor = MG.bmp.GetPixel(i, j);
7.                     int alfa = picColor.A;
8.                     int red = picColor.R;
9.                     int green = picColor.G;
10.                     int blue = picColor.B;
11.                     int gray = (byte)(.2989 * red + .5870 * green + .1140 * blue);
12.                     work1[i, j] = gray;
13. }
14. }
15.
16.

Good luck
Apr 14 '22 #3
salmawisoky
1 New Member
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.
Sep 14 '23 #4