Color Sorting In PHP: Part 5

3rd November 2019

Since my last visit to this subject I have been thinking about how to represent a collection of random colors so that it looks sorted and that no information is lost during that process. I quickly realised that I needed to use all three aspects of the color space, which lends itself to generating 3D objects. Indeed, the red, green, blue color space is built around a cube so it can often be represented as a cube.

The hue, saturation, and value color space is built around the concept of a cylinder, which means that 2 of the axis lend themselves to a circumference of a circle (the hue) and the diameter of a circle (the saturation). This is an example of the polar coordinate system. We can therefore draw a circle using hue and saturation and the value can be used to represent a different aspect of the color space.

You might have already seen the hue and saturation used to draw a circle in something called a color wheel. This is a favourite of applications that allow you to pick a color. Here is an example from the Pages application on a Mac.

Color wheel

One thing to realise is that the value is actually underneath the color wheel, in which case what we are looking at is the same problem I am trying to overcome. Namely, removing some of the data to make the wheel work. What I want to do is incorporate the value into the color wheel so that we can represent all three at once.

To start with I need to create a Color class. This accepts red, green and blue and calculates the hue, saturation and value based on these values.

  1. class Color {
  2. public $red = 0;
  3. public $green = 0;
  4. public $blue = 0;
  5.  
  6. public $hue;
  7. public $saturation;
  8. public $value;
  9.  
  10. public function __construct($red, $green, $blue)
  11. {
  12. $this->red = $red;
  13. $this->green = $green;
  14. $this->blue = $blue;
  15.  
  16. $this->calculageHsv();
  17. }
  18.  
  19. function calculageHsv() {
  20. $red = $this->red / 255;
  21. $green = $this->green / 255;
  22. $blue = $this->blue / 255;
  23.  
  24. $min = min($red, $green, $blue);
  25. $max = max($red, $green, $blue);
  26.  
  27. switch ($max) {
  28. case 0:
  29. // If the max value is 0.
  30. $hue = 0;
  31. $saturation = 0;
  32. $value = 0;
  33. break;
  34. case $min:
  35. // If the maximum and minimum values are the same.
  36. $hue = 0;
  37. $saturation = 0;
  38. $value = round($max, 4);
  39. break;
  40. default:
  41. $delta = $max - $min;
  42. if ($red == $max) {
  43. $hue = 0 + ($green - $blue) / $delta;
  44. } elseif ($green == $max) {
  45. $hue = 2 + ($blue - $red) / $delta;
  46. } else {
  47. $hue = 4 + ($red - $green) / $delta;
  48. }
  49. $hue *= 60;
  50. if ($hue < 0) {
  51. $hue += 360;
  52. }
  53. $saturation = $delta / $max;
  54. $value = round($max, 4);
  55. }
  56.  
  57. $this->hue = $hue;
  58. $this->saturation = $saturation;
  59. $this->value = $value;
  60. }
  61. }

To generate the colors I created a loop that created 1000 random colors based on their red, green and blue values.

  1. for ($i = 0; $i <1000; $i++) {
  2. $red = ceil(mt_rand(0, 255));
  3. $green = ceil(mt_rand(0, 255));
  4. $blue = ceil(mt_rand(0, 255));
  5.  
  6. $colors[] = new Color($red, $green, $blue);
  7. }

This generates 100 colors, which is good enough to start seeing the sorting working, but without so many colors that we just create a color wheel and can't see the difference between the colors present.

The first step is to generate the color wheel using just pixels. There is plenty of comments in the code, but the drawing of the pixels along the circumference of the circle is done using the same maths that I employed when drawing circles in a previous post. Essentially, we use the hue to work out what angle the pixel should lie at and then use the saturation to work out how far that pixel should be from the centre of the circle.

  1. function colorWheelPixels($colors) {
  2. // Set the width and height of the image.
  3. $width = $height = 300;
  4.  
  5. // Create the image handler.
  6. $im = imagecreatetruecolor($width, $height);
  7.  
  8. // Set a background color of white and fill the image with it.
  9. $bgColor = imagecolorallocate($im, 255, 255, 255);
  10. imagefill($im, 0, 0, $bgColor);
  11.  
  12. // Calcualte the centre of the image, based on the width and height.
  13. $centerX = $centerY = $width / 2;
  14.  
  15. foreach ($colors as $color) {
  16. // Create a color.
  17. $allocatedColor = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  18.  
  19. // Calculate the radius of the color based on the saturation.
  20. $radius = $color->saturation * $centerX;
  21.  
  22. // We need to convert the degrees to radians so we can position the pixel on the
  23. // correct point on the circumference of the circle.
  24. $angle = deg2rad($color->hue);
  25.  
  26. // Calculate the x,y coordinates of the hue and offset it by the radius.
  27. $x = $centerX + $radius * cos($angle);
  28. $y = $centerY + $radius * sin($angle);
  29.  
  30. // Place the colored pixel in the calculated position.
  31. imagesetpixel($im, $x, $y, $allocatedColor);
  32. }
  33.  
  34. // Create the image and destroy the image handler.
  35. imagepng($im, 'color-wheel-pixels-' . time() . '.png');
  36. }

This generates the following image. It's quite faint, and we are still missing some of the color information, but the colors are obviously sorted.

Color wheel pixels.

The first way I thought to show integrate the value into the color wheel was to use lines. Using the worked out coordinate from the saturation and hue I just drew a line that pointed away from the centre of the circle. We just use the imageline() function to create this line. The length of the line is proportional to the value of the color. This means that lighter colors are longer and darker colors are shorter.

  1. function colorWheelLines($colors) {
  2. $width = $height = 300;
  3. $im = imagecreatetruecolor($width, $height);
  4.  
  5. $bgColor = imagecolorallocate($im, 255, 255, 255);
  6. imagefill($im, 0, 0, $bgColor);
  7.  
  8. $centerX = $centerY = $width / 2;
  9.  
  10. foreach ($colors as $color) {
  11. $allocatedColor = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  12.  
  13. $radius = $color->saturation * $centerX;
  14.  
  15. $angle = deg2rad($color->hue);
  16.  
  17. $x = $centerX + $radius * cos($angle);
  18. $y = $centerY + $radius * sin($angle);
  19.  
  20. // Draw a line from the calculated x,y coordinates away from the center.
  21. $x2 = $x + ($color->value * 10) * cos($angle);
  22. $y2 = $y + ($color->value * 10) * sin($angle);
  23. imageline($im, $x, $y, $x2, $y2, $allocatedColor);
  24. }
  25.  
  26. imagepng($im, 'color-wheel-lines-' . time() . '.png');
  27. }

This code produces the following output. The colors are much more apparent in this example and it's clear that the they have been sorted.

Color wheel using lines.

The next idea I had was to use circles to show the value. This came in two forms, circles and filled circles.

The circles based on the value of the color were pretty easy to generate using the imageellipse(). The value was multiplied by 10 in order to make the circles it draws visible (value is a decimal value between 0 and 1).

  1. function colorWheelCircles($colors) {
  2. $width = $height = 300;
  3. $im = imagecreatetruecolor($width, $height);
  4.  
  5. $bgColor = imagecolorallocate($im, 255, 255, 255);
  6. imagefill($im, 0, 0, $bgColor);
  7.  
  8. $centerX = $centerY = $width / 2;
  9.  
  10. foreach ($colors as $color) {
  11. $allocatedColor = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  12.  
  13. $radius = $color->saturation * $centerX;
  14.  
  15. $angle = deg2rad($color->hue);
  16.  
  17. $x = $centerX + $radius * cos($angle);
  18. $y = $centerY + $radius * sin($angle);
  19.  
  20. // Calculate the size of the little circles based on the value.
  21. $colorSize = $color->value * 10;
  22. imageellipse($im, $x, $y, $colorSize, $colorSize, $allocatedColor);
  23. }
  24.  
  25. imagepng($im, 'color-wheel-circles-' . time() . '.png');
  26. }

This produces the following image. It's very clear from this image that we have formed a color wheel, and the circles are a nice way of representing the value of the colors.

Color wheel circles.

The filled circles use the same code, but in this case they use the iamgefilledellipse() function to generate the circle.

  1. function colorWheelFilledCircles($colors) {
  2. // Sort the colors by the value to prevent any overlapping of colors.
  3. usort($colors, function ($a, $b) {
  4. return $b->value <=> $a->value;
  5. });
  6.  
  7. $width = $height = 300;
  8. $im = imagecreatetruecolor($width, $height);
  9.  
  10. $bgColor = imagecolorallocate($im, 255, 255, 255);
  11. imagefill($im, 0, 0, $bgColor);
  12.  
  13. $centerX = $centerY = $width / 2;
  14.  
  15. foreach ($colors as $color) {
  16. $allocatedColor = imagecolorallocate($im, $color->red, $color->green, $color->blue);
  17.  
  18. $radius = $color->saturation * $centerX;
  19.  
  20. $angle = deg2rad($color->hue);
  21.  
  22. $x = $centerX + $radius * cos($angle);
  23. $y = $centerY + $radius * sin($angle);
  24.  
  25. // Calculate the size of the little circles based on the value.
  26. $colorSize = $color->value * 10;
  27. imagefilledellipse($im, $x, $y, $colorSize, $colorSize, $allocatedColor);
  28. }
  29.  
  30. imagepng($im, 'color-wheel-filled-circles-' . time() . '.png');
  31. }

This produces the following image.

Color wheel with filled in circles.

Everything there appears to be working, but the random collection of colors I am using isn't lending itself well to the idea that these colors are sorted. To demonstrate that the colors are sorted I decided to take a subsection of colors, rather than colors from all over the spectrum, and show them in the color wheel.

Here are some examples of using just sections of the color spectrum to generate color wheels.

Color wheel with limited colors.Color wheel with limited colors.Color wheel with a selection of colors.

Incidentally, I did have a go at generating all of the colors available as Color objects. This was essentially 16,777,216 different colors (256 red x 256 green x 256 blue). Creating this many objects and rendering them using any of the above functions takes about 4gig of memory in PHP. This does, however, generate a perfect color wheel.

A perfect color wheel.

That image was generated using the original pixels approach to creating the color wheel, but it proves that if we saturate the colors space available we get a perfect circle.

Personally, I think generating a filled circle color wheel is a good way of representing the spectrum of colors available in a given data set.

Add new comment

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