<?php

class ColorGradient
{
  public $pct;
  public $color;

  public static function RGB2HSV($R, $G, $B)    // RGB values:    0-255, 0-255, 0-255
  {                                // HSV values:    0-360, 0-100, 0-100
    // Convert the RGB byte-values to percentages
    $R = ($R / 255);
    $G = ($G / 255);
    $B = ($B / 255);

    // Calculate a few basic values, the maximum value of R,G,B, the
    //   minimum value, and the difference of the two (chroma).
    $maxRGB = max($R, $G, $B);
    $minRGB = min($R, $G, $B);
    $chroma = $maxRGB - $minRGB;

    // Value (also called Brightness) is the easiest component to calculate,
    //   and is simply the highest value among the R,G,B components.
    // We multiply by 100 to turn the decimal into a readable percent value.
    $computedV = 100 * $maxRGB;

    // Special case if hueless (equal parts RGB make black, white, or grays)
    // Note that Hue is technically undefined when chroma is zero, as
    //   attempting to calculate it would cause division by zero (see
    //   below), so most applications simply substitute a Hue of zero.
    // Saturation will always be zero in this case, see below for details.
    if ($chroma == 0)
        return array(0, 0, $computedV);

    // Saturation is also simple to compute, and is simply the chroma
    //   over the Value (or Brightness)
    // Again, multiplied by 100 to get a percentage.
    $computedS = 100 * ($chroma / $maxRGB);

    // Calculate Hue component
    // Hue is calculated on the "chromacity plane", which is represented
    //   as a 2D hexagon, divided into six 60-degree sectors. We calculate
    //   the bisecting angle as a value 0 <= x < 6, that represents which
    //   portion of which sector the line falls on.
    if ($R == $minRGB)
        $h = 3 - (($G - $B) / $chroma);
    elseif ($B == $minRGB)
        $h = 1 - (($R - $G) / $chroma);
    else // $G == $minRGB
        $h = 5 - (($B - $R) / $chroma);

    // After we have the sector position, we multiply it by the size of
    //   each sector's arc (60 degrees) to obtain the angle in degrees.
    $computedH = 60 * $h;

    return array($computedH, $computedS, $computedV);
  }

  public static function HSV2RGB($h, $s, $v)
  {
    $s /= 256.0;
    if ($s == 0.0) return array($v,$v,$v);
    $h /= (256.0 / 6.0);
    $i = floor($h);
    $f = $h - $i;
    $p = (integer)($v * (1.0 - $s));
    $q = (integer)($v * (1.0 - $s * $f));
    $t = (integer)($v * (1.0 - $s * (1.0 - $f)));
    switch($i) {
    case 0: return array($v,$t,$p);
    case 1: return array($q,$v,$p);
    case 2: return array($p,$v,$t);
    case 3: return array($p,$q,$v);
    case 4: return array($t,$p,$v);
    default: return array($v,$p,$q);
    }
  }

   public static function gradient($from_color, $to_color, $graduations = 10) 
   {
        $graduations--;
        $startcol = str_replace("#", "", $from_color);
        $endcol = str_replace("#", "", $to_color);
        $RedOrigin = hexdec(substr($startcol, 0, 2));
        $GrnOrigin = hexdec(substr($startcol, 2, 2));
        $BluOrigin = hexdec(substr($startcol, 4, 2));
        if ($graduations >= 2) { // for at least 3 colors
            $GradientSizeRed = (hexdec(substr($endcol, 0, 2)) - $RedOrigin) / $graduations; //Graduation Size Red
            $GradientSizeGrn = (hexdec(substr($endcol, 2, 2)) - $GrnOrigin) / $graduations;
            $GradientSizeBlu = (hexdec(substr($endcol, 4, 2)) - $BluOrigin) / $graduations;
            for ($i = 0; $i <= $graduations; $i++) {
                $RetVal[$i] = strtoupper("#" . str_pad(dechex($RedOrigin + ($GradientSizeRed * $i)), 2, '0', STR_PAD_LEFT) .
                        str_pad(dechex($GrnOrigin + ($GradientSizeGrn * $i)), 2, '0', STR_PAD_LEFT) .
                        str_pad(dechex($BluOrigin + ($GradientSizeBlu * $i)), 2, '0', STR_PAD_LEFT));
            }
        } elseif ($graduations == 1) { // exactlly 2 colors
            $RetVal[] = $from_color;
            $RetVal[] = $to_color;
        } else { // one color
            $RetVal[] = $from_color;
        }
        return $RetVal;
    }

    public static function hex2rgb($hex) 
    { 
      return sscanf($hex, "#%02x%02x%02x"); 
    }

    // ---
    // --- Local fonctions
    // ---
    public static function rgb2hex($rgb) 
    {
      $hex = "#";
      $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
      $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
      $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

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

function getGDcolorGradientArray($vImage, $nb_colors, $couleurRGB, $fondRGB, $alpha = 0)
{
  $vColor = array();

  // Calculer un gradient de couleurs
  $hex1      = ColorGradient::rgb2hex($couleurRGB);
  $hex2      = ColorGradient::rgb2hex([255,255,255]);
  if ($hex2 == ColorGradient::rgb2hex($fondRGB)) $hex2 = ColorGradient::rgb2hex([0,0,0]);
  $hex_val   = array( $hex1, $hex2 );
  $gradient = ColorGradient::gradient($hex_val[0], $hex_val[1], $nb_colors);

  // Pour chaque couleur, on rajoute deux informations
  //  - un pourcentage
  //  - une couleur GD
  for($i=0;$i<$nb_colors;$i++)
  {
    $rgbval = ColorGradient::hex2rgb($gradient[$i]);
    $vColor[$i] = new ColorGradient();
    $vColor[$i]->pct   = ($i * 1.0) / $nb_colors; 
    $vColor[$i]->color = imagecolorallocatealpha($vImage, $rgbval[0], $rgbval[1], $rgbval[2], $alpha);
  }

  return $vColor;
}

?>
