Color Sorting In PHP: Part 4

2nd January 2019

Following on from by previous post about sorting colors I decided to take another step towards sorting colors by segmenting the data to create a further dimension to the multi-dimensional array.

The array is already split into segments based on the hue of the color, but we can further split this by separating out saturation and value into separate arrays within hue. To do this we set the saturation or value to be a constant and push them into separate arrays.

The new code looks like this. Please excuse the duplication of code here. This is just a simple example to show how the array is put together.

  1. $colorRanges = array_fill(0, 360, []);
  2.  
  3. foreach ($colorRanges as $hue => $range) {
  4. // Setup saturation and value arrays.
  5. $colorRanges[$hue]['saturation'] = [];
  6. $colorRanges[$hue]['value'] = [];
  7.  
  8. for ($i = 0; $i < 150; ++$i) {
  9. // Generate random values for saturation.
  10. $saturation = mt_rand(0, 100) / 100;
  11. $value = 1;
  12.  
  13. // Convert the color from hsv to rgb.
  14. $rgb = hsvToRbg($hue, $saturation, $value);
  15.  
  16. // Create Color object.
  17. $colorRanges[$hue]['saturation'][] = new Color($rgb['red'], $rgb['blue'], $rgb['green']);
  18. }
  19.  
  20. for ($i = 0; $i < 150; ++$i) {
  21. // Generate random values for value.
  22. $saturation = 1;
  23. $value = mt_rand(0, 100) / 100;
  24.  
  25. // Convert the color from hsv to rgb.
  26. $rgb = hsvToRbg($hue, $saturation, $value);
  27.  
  28. // Create Color object.
  29. $colorRanges[$hue]['value'][] = new Color($rgb['red'], $rgb['blue'], $rgb['green']);
  30. }
  31. }

Setting the value of saturation or value of a color to be 1 (or 100%) is important in order to actually show the colors. With a value of 0 the colors will be either all black (for saturation) or a combination of black or white (for value). With a value in between 0 and 1 showing pale or washed out colors.

Rendering

As I have added another dimension to the array I need to alter the render function slightly to render out all of the data in the 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 = max(count($colorRange['saturation']), count($colorRange['value'])) * $pixelSize;
  11. if ($potentialHeight > $height) {
  12. $height = $potentialHeight;
  13. }
  14. }
  15.  
  16. // Height will be double.
  17. $height = $height * 2;
  18.  
  19. // Initialize image.
  20. $im = imagecreatetruecolor($width, $height);
  21.  
  22. // Fill image with colored pixels.
  23. $x = 0;
  24. foreach ($colorRanges as $colorRange) {
  25. $y = 0;
  26. foreach ($colorRange['saturation'] as $color) {
  27. $pixelColour = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  28. imagefilledrectangle($im, $x, $y, $x + $pixelSize, $y + $pixelSize, $pixelColour);
  29. $y = $y + $pixelSize;
  30. }
  31. foreach ($colorRange['value'] as $color) {
  32. $pixelColour = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  33. imagefilledrectangle($im, $x, $y, $x + $pixelSize, $y + $pixelSize, $pixelColour);
  34. $y = $y + $pixelSize;
  35. }
  36. $x = $x + $pixelSize;
  37. }
  38.  
  39. // Save image.
  40. imagepng($im, 'colors-' . $sortedBy . '.png');
  41. }

What we are doing here is rendering out the image in two parts. The saturation part of the image is on top and the value part of the image is at the bottom. This generates an image that looks like this with the above random data.

Colors, segmented by hue, saturation and value, but not sorted.

We can now go about sorting this data structure.

Sorting

I quickly discovered that sorting this data becomes quite easy a few different comparisons (RGB, HSV, lightness etc) produced very similar (if not identical) results.

As an example, here is RGB sorting of the colors array.

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

This generates the following image.

Colors, segmented by hue, saturation and value, sorted by RGB values.

The HVS sorting code is a little different.

  1. foreach ($colorRanges as $id => $colorRange) {
  2. usort($colorRange['saturation'], function ($a, $b) {
  3. $hsv1 = rgbTohsv($a);
  4. $hsv2 = rgbTohsv($b);
  5.  
  6. return $hsv1['saturation'] <=> $hsv2['saturation'];
  7. });
  8. $colorRanges[$id]['saturation'] = $colorRange['saturation'];
  9.  
  10. usort($colorRange['value'], function ($a, $b) {
  11. $hsv1 = rgbTohsv($a);
  12. $hsv2 = rgbTohsv($b);
  13.  
  14. return $hsv2['value'] <=> $hsv1['value'];
  15. });
  16.  
  17. $colorRanges[$id]['value'] = $colorRange['value'];
  18. }

This results in the following image.

Colors, segmented by hue, saturation and value and then sorted by saturation and value.

This is very close to the original intension of the research. This was to take an array of colors and sort them into a coherent looking spectrum.

One thing of interest was the HEX sorting algorithm. This worked for the most part, but produced some odd artefacts in the sorting or the value part of the image. This is the image that was produced.

Colors, segmented by hue, saturation and value, sorted by HEX.

Still Not Enough?

Those with keen eyes will probably spot that although we have created a nice looking spectrum, we have actually lost information here. Setting the saturation and value amounts to 1 is essentially removing that value from the equation. This means we are still losing data to get this working correctly. Additionally, we have artificially tied together hue with either a varying saturation or value. In the real world all three values would not be linked to each other so using this algorithm to sort colors from real world data would not work.

Add new comment

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