2010-08-30 14 views
15

posso convertire RGB valori HSV con il seguente codice ...PHP HSV a RGB comprensione formula

$r = $r/255; 
$g = $g/255; 
$b = $b/255; 

$h = 0; 
$s = 0; 
$v = 0; 

$min = min(min($r, $g),$b); 
$max = max(max($r, $g),$b); 

$r = $max-$min; 
$v = $max; 


if($r == 0){ 
    $h = 0; 
    $s = 0; 
} 
else { 
    $s = $r/$max; 

    $hr = ((($max - $r)/6) + ($r/2))/$r; 
    $hg = ((($max - $g)/6) + ($r/2))/$r; 
    $hb = ((($max - $b)/6) + ($r/2))/$r; 

    if ($r == $max) $h = $hb - $hg; 
    else if($g == $max) $h = (1/3) + $hr - $hb; 
    else if ($b == $max) $h = (2/3) + $hg - $hr; 

    if ($h < 0)$h += 1; 
    if ($h > 1)$h -= 1; 
} 

Ma come si fa a convertire HSV-RGB in PHP ???

Quello che segue è su wikipedia, ma io non lo capisco,

Sto indovinando che è abbastanza evidente

alt text

+0

Ci sono formule qui: http://en.wikipedia.org/wiki/HSV_color_space#Conversion_from_RGB_to_HSL_or_HSV – NullUserException

+1

lo so, ma qualcuno può spiegare le formule –

+0

'la formula ** ** davvero lavorando per voi? Perché hai reintrodotto la variabile $ r (niente come il linguaggio dinamico ...). – greenoldman

risposta

15

Questo è per i valori HSV della gamma [0,1] (e dando valori RGB nell'intervallo [0,1], invece di {0, 1, ..., 255}:

function HSVtoRGB(array $hsv) { 
    list($H,$S,$V) = $hsv; 
    //1 
    $H *= 6; 
    //2 
    $I = floor($H); 
    $F = $H - $I; 
    //3 
    $M = $V * (1 - $S); 
    $N = $V * (1 - $S * $F); 
    $K = $V * (1 - $S * (1 - $F)); 
    //4 
    switch ($I) { 
     case 0: 
      list($R,$G,$B) = array($V,$K,$M); 
      break; 
     case 1: 
      list($R,$G,$B) = array($N,$V,$M); 
      break; 
     case 2: 
      list($R,$G,$B) = array($M,$V,$K); 
      break; 
     case 3: 
      list($R,$G,$B) = array($M,$N,$V); 
      break; 
     case 4: 
      list($R,$G,$B) = array($K,$M,$V); 
      break; 
     case 5: 
     case 6: //for when $H=1 is given 
      list($R,$G,$B) = array($V,$M,$N); 
      break; 
    } 
    return array($R, $G, $B); 
} 
+0

Cosa intendi per Questo è per i valori HSV nell'intervallo [0,1] (e per dare valori RGB nell'intervallo [0,1], invece di {0, 1, ..., 255} Si prega di spiegare –

+0

Voglio dire che i valori devono essere compresi tra 0 e 1. – Artefacto

+1

Ohhh così invece di h: 0, s: 100 e v: 100 dovrebbe essere h: 0 s: 1 v: 1 –

1
private class HSLPixel{ 
     double hue; 
     double saturation; 
     double lightness; 

     .... your code 
} 

public Color HSLToRGB(HSLPixel pixel){ 

      double v; 
      double r,g,b; 
      double l = pixel.lightness; 
      double h = pixel.hue; 
      double s = pixel.saturation; 

      r = l; // default to gray 
      g = l; 
      b = l; 
      v = (l <= 0.5) ? (l * (1.0 + s)) : (l + s - l * s); 
      if (v > 0){ 
        double m; 
        double sv; 
        int sextant; 
        double fract, vsf, mid1, mid2; 

        m = l + l - v; 
        sv = (v - m)/v; 
        h *= 6.0; 
        sextant = (int)h; 
        fract = h - sextant; 
        vsf = v * sv * fract; 
        mid1 = m + vsf; 
        mid2 = v - vsf; 

        switch (sextant) 
        { 
         case 0: 
           r = v; 
           g = mid1; 
           b = m; 
           break; 
         case 1: 
           r = mid2; 
           g = v; 
           b = m; 
           break; 
         case 2: 
           r = m; 
           g = v; 
           b = mid1; 
           break; 
         case 3: 
           r = m; 
           g = mid2; 
           b = v; 
           break; 
         case 4: 
           r = mid1; 
           g = m; 
           b = v; 
           break; 
         case 5: 
           r = v; 
           g = m; 
           b = mid2; 
           break; 
        } 
      } 
      Color rgb = new Color((int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0)); 
      return rgb; 
    } 
+0

Puoi implementarlo in PHP ??? –

0

ovviamente è possibile, basta modificare le definizioni di funzione in stile php, modificare tutte le variabili ecc ma mantenere il codice core lo stesso, non dovrebbe richiedere più di 30 minuti per farlo e testarlo.

+0

Io non parlo C bene .. –

+0

dacci un colpo e le linee su cui tieni bloccato chiedi a noi, non ho mai fatto php prima. – rolls

+0

Sono bloccato all'inizio, ma questo è un ottimo esempio! Io lo invierò a un valore uguale a –

5

Traduzione di rotoli di rispondere per HSL da C a PHP

function ColorHSLToRGB($h, $s, $l){ 

     $r = $l; 
     $g = $l; 
     $b = $l; 
     $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s); 
     if ($v > 0){ 
       $m; 
       $sv; 
       $sextant; 
       $fract; 
       $vsf; 
       $mid1; 
       $mid2; 

       $m = $l + $l - $v; 
       $sv = ($v - $m)/$v; 
       $h *= 6.0; 
       $sextant = floor($h); 
       $fract = $h - $sextant; 
       $vsf = $v * $sv * $fract; 
       $mid1 = $m + $vsf; 
       $mid2 = $v - $vsf; 

       switch ($sextant) 
       { 
        case 0: 
          $r = $v; 
          $g = $mid1; 
          $b = $m; 
          break; 
        case 1: 
          $r = $mid2; 
          $g = $v; 
          $b = $m; 
          break; 
        case 2: 
          $r = $m; 
          $g = $v; 
          $b = $mid1; 
          break; 
        case 3: 
          $r = $m; 
          $g = $mid2; 
          $b = $v; 
          break; 
        case 4: 
          $r = $mid1; 
          $g = $m; 
          $b = $v; 
          break; 
        case 5: 
          $r = $v; 
          $g = $m; 
          $b = $mid2; 
          break; 
       } 
     } 
     return array('r' => $r * 255.0, 'g' => $g * 255.0, 'b' => $b * 255.0); 
} 
+0

Vedere [questa risposta] (http://stackoverflow.com/a/34363975/3437608) per il contrario di questa funzione e funzionalità aggiunte. – Cullub

1

ColorJizz consente di convertire da/a diversi formati. Esiste anche una versione PHP.