Color Sorting In PHP: Part 3

1st January 2019

The last time I looked at sorting colors I had produced a nice band or sorted colors, but to do so I had essentially removed a third of the data from the color information. This meant that there was no white or black colors in the band of sorted colors.

After a bit of thinking on how to solve this I hit upon a way of using a two dimensional array of colors to filter the colors into blocks. This would allow the missing color information to be rendered correctly, and would only mean a small amount of work to allow it to work with the rendering function used in the previous examples.

Generating The Data

I could easily just generate every color available and use that as the data. What I wanted to generate was a random assortment of colors that would represent the sort of data being produced by a system or other input.

To generate the data I created an array of 360 blank arrays. The 360 represents the hue value of the color so all that was needed is to create the saturation and values for the color and then store them in the correct array.

The following code initialises the color arrays and then fills them with some random data. The hsvToRgb() function can be found in part 2.

  1. $colorRanges = array_fill(0, 360, []);
  2.  
  3. foreach ($colorRanges as $hue => $range) {
  4. for ($i = 0; $i < 300; ++$i) {
  5. // Generate random values for saturation and value.
  6. $saturation = mt_rand(0, 100) / 100;
  7. $value = mt_rand(0, 100) / 100;
  8.  
  9. // Convert the color from hsv to rgb.
  10. $rgb = hsvToRbg($hue, $saturation, $value);
  11.  
  12. // Create Color object.
  13. $colorRanges[$hue][] = new Color($rgb['red'], $rgb['blue'], $rgb['green']);
  14. }
  15. }

The loop of 300 is just an arbitrary value to generate a decent amount of data in each hue array.

Rendering

The rendering function just needs a little bit of alteration so that it can render a multidimensional array of Color objects instead of just a flat array.

  1. function renderColors($colorRanges, $sortedBy) {
  2. $pixelSize = 1;
  3.  
  4. // Work out width.
  5. $width = count($colorRanges) * $pixelSize;
  6.  
  7. // Ensure the correct height is worked out.
  8. $height = 0;
  9. foreach ($colorRanges as $colorRange) {
  10. $potentialHeight = count($colorRange) * $pixelSize;
  11. if ($potentialHeight > $height) {
  12. $height = $potentialHeight;
  13. }
  14. }
  15.  
  16. // Initialize image.
  17. $im = imagecreatetruecolor($width, $height);
  18.  
  19. // Fill image with colored pixels.
  20. $x = 0;
  21. foreach ($colorRanges as $colorRange) {
  22. $y = 0;
  23. foreach ($colorRange as $yColor => $color) {
  24. $pixelColour = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  25. imagefilledrectangle($im, $x, $y, $x + $pixelSize, $y + $pixelSize, $pixelColour);
  26. $y = $y + $pixelSize;
  27. }
  28. $x = $x + $pixelSize;
  29. }
  30.  
  31. // Save image.
  32. imagepng($im, 'colors-' . $sortedBy . '.png');
  33. }

Without sorting this generates the following image.

Colors randomly sorted.

This shows that the hue is already sorted into bands of colors, so all we need to do is go through each band and sort it by other values.

Sorting

With that in place we need to sort each of the arrays and see if we can get any decent results by sorting just the bands of colors. This differs from the sorting done in the other posts, with the difference that we loop through the array and sort each band of colors.

RGB

Sorting by RGB is just as it sounds, just add together the reg, green and blue values and compare them.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange, function ($a, $b) {
  3. return ($a->red + $a->green + $a->blue) <=> ($b->red + $b->green + $b->blue);
  4. });
  5. $colorRanges[$id] = $colorRange;
  6. }
  7.  
  8. renderColors($colorRanges, 'rgb' . time());

This generates the following image.

Colors, sorted by RGB

It's clear that some sorting has taken place here. The whiter colors are pushed towards the bottom and the darker colors are pushed towards the top. Grey is still a bit of a problem as it doesn't appear to have moved much.

Lightness

For lightness I am re-using the calculateLightness() function from part 1.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange, function ($a, $b) {
  3. return calculateLightness($a) <=> calculateLightness($b);
  4. });
  5. $colorRanges[$id] = $colorRange;
  6. }
  7.  
  8. renderColors($colorRanges, 'lightness' . time());

This produces the following output.

Colors, sorted by lightness.

Similar to the RGB sorting this works for lighter and darker colors, but greys are largely ignored. I thought this would create better results than just adding together the RGB values but this doesn't appear to be the case.

Luminance

The luminance of the color is calculated by working out the proportions of red, green and blue to create a perceived level of lightness. Re-using the calculateLuminance() function from part 1.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange, function ($a, $b) {
  3. return calculateLuminance($a) <=> calculateLuminance($b);
  4. });
  5. $colorRanges[$id] = $colorRange;
  6. }
  7.  
  8. renderColors($colorRanges, 'luminance' . time());

This produces the following image.

Colors, sorted by luminance.

This creates a better result than lightness or RGB sorting, but grey is still causing problems.

HSV

Sorting by hue, saturation and value is not necessary as we have technically already sorted by hue. As a result we just need to sort by the sum of saturation and value.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange, function ($a, $b) {
  3. $hsv1 = rgbTohsv($a);
  4. $hsv2 = rgbTohsv($b);
  5.  
  6. return ($hsv1['saturation'] + $hsv1['value']) <=> ($hsv2['saturation'] + $hsv2['value']);
  7. });
  8. $colorRanges[$id] = $colorRange;
  9. }
  10.  
  11. renderColors($colorRanges, 'hsv' . time());

This creates the following image.

Colors, sorted by HSV

This has pushed the high saturation colors to the bottom and the low saturation colors to the top. This results in the whites and grey colors being distributed across the spectrum and not sorted into the correct result.

HEX

Out of interest (and because HEX sorting is quite good when sorting with single bands of colors) I decided to try sorting by the HEX value of the color.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange, function ($a, $b) {
  3.  
  4. $aValue['red'] = str_pad(dechex($a->red), 2, '0', STR_PAD_LEFT);
  5. $aValue['green'] = str_pad(dechex($a->green), 2, '0', STR_PAD_LEFT);
  6. $aValue['blue'] = str_pad(dechex($a->blue), 2, '0', STR_PAD_LEFT);
  7.  
  8. $bValue['red'] = str_pad(dechex($b->red), 2, '0', STR_PAD_LEFT);
  9. $bValue['green'] = str_pad(dechex($b->green), 2, '0', STR_PAD_LEFT);
  10. $bValue['blue'] = str_pad(dechex($b->blue), 2, '0', STR_PAD_LEFT);
  11.  
  12. $aValue = implode($aValue);
  13. $bValue = implode($bValue);
  14.  
  15. return $aValue <=> $bValue;
  16. });
  17. $colorRanges[$id] = $colorRange;
  18. }
  19.  
  20. renderColors($colorRanges, 'hex' . time());

This generates the following image.

Colors, sorted by hex.

Yup, pretty terrible. Weirdly, this reminds me of the color absorption spectrum of chlorophyll. This pattern is created because of the way that the hex value is created. Because of the way in which the hex value is added together I have essentially created a process that gives more precedence to red colors so those colors are slightly better sorted in this example.

Not Enough?

This approach of sorting a multidimensional array is closer than sorting a single dimensional array, but there are still a few problems. I stated before that creating bands of hue creates separate blocks of colors. Although this is technically true we still can't sort because we essentially have to combine one or more values in order to generate a sorting algorithm. Although we can separate and sort each color there are still a lot of grey values that don't fit nicely into the sorting algorithm because we are essentially trying to force a three-dimensional data item (i.e. the color) into a two-dimensional array.

Add new comment

The content of this field is kept private and will not be shown publicly.