Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/palette.git
<?php

class Color {
	public $red = 0;
	public $green = 0;
	public $blue = 0;

	public function __construct($red, $green, $blue) {
		$this->red = $red;
		$this->green = $green;
		$this->blue = $blue;
	}
}

function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }

  return new Color( $r, $g, $b );
}

function rgb2hex($r, $g, $b) {
 $hex = "#";
 $hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
 $hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
 $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);

 return $hex; // returns the hex value including the number sign (#)
}

$colors = [];
$colours = [];

$fh = fopen( $argv[1], "r" );

if( $fh ) {
	while( ($line = fgets( $fh )) !== false ) {
		$c = hex2rgb($line);
    array_push( $colors, $c );
		array_push( $colours, ['color' => $c ] );
  }

  fclose( $fh );
}
else {
	exit;
}

function renderColors($colors, $sortBy) {
	$color_width = 30;

	$width = count($colors) * $color_width;
	$height = 30;

	$im = imagecreatetruecolor($width, $height);
	$background_color = imagecolorallocate($im, 255, 255, 255);

	$x = 0;

	foreach ($colors as $color) {
		$colour = imagecolorallocate($im, $color->red, $color->green, $color->blue);
		imagefilledrectangle($im, $x, 0, $x + $color_width,$height, $colour);

		$x = $x + $color_width;

		echo "<rect x='$x' y='10' height='$height' width='$color_width' style='fill:". rgb2hex( $color->red, $color->green, $color->blue ). "'/>";
    echo "\n";
	}

	imagepng($im, "colors-$sortBy.png");
	imagedestroy($im);
}

function toHue($color) {
	$red = $color->red / 255;
	$green = $color->green / 255;
	$blue = $color->blue / 255;
 
	$min = min($red, $green, $blue);
	$max = max($red, $green, $blue);
 
	switch ($max) {
			case 0:
					// If the max value is 0.
					$hue = 0;
					break;
			case $min:
					// If the maximum and minimum values are the same.
					$hue = 0;
					break;
			default:
					$delta = $max - $min;
					if ($red == $max) {
							$hue = 0 + ($green - $blue) / $delta;
					} elseif ($green == $max) {
							$hue = 2 + ($blue - $red) / $delta;
					} else {
							$hue = 4 + ($red - $green) / $delta;
					}
					$hue *= 60;
					if ($hue < 0) {
							$hue += 360;
					}
	}
	return $hue;
}


function colorToLum($color) {
  $r = $color->red;
  $g = $color->green;
  $b = $color->blue;
  return (0.299 * $r + 0.587 * $g + 0.114 * $b);
}

// Set up the ranges array.
$ranges = [];
 
foreach ($colors as $color) {
	$hue = toHue($color);
 
	// Simplify the hue to create 12 segments.
	$simplifiedHue = floor($hue / 30);
 
	if (!isset($ranges[$simplifiedHue])) {
		// Add the simplified hue to the ranges array if it doesn't exist.
		$ranges[$simplifiedHue] = [];
	}
 
	// Add the color to the correct segment.
	$ranges[$simplifiedHue][] = $color;
}
 
// Sort the ranges by their keys (the simplified hue).
ksort($ranges);

$newColors = [];
foreach ($ranges as $id => $colorRange) {
	usort($colorRange, function ($a, $b) {
		 return ($a->red + $a->green + $a->blue) <=> ($b->red + $b->green + $b->blue);
	});
	$newColors = array_merge($newColors, $colorRange);
}

usort(
  $newColors,
  function ($one, $two) {
    return colorToLum($one) - colorToLum($two);
  }
);

$path_parts = pathinfo($argv[1]);
$f = $path_parts['filename'];



renderColors($newColors, $f);

#renderColors($colors, $f);

?>