Percentage to Hexcolor in PHP

2020-04-16 03:29发布

I'd like to make a percentage into a hexidecimal color. Kind of how HSV color degrees work...
I am using PHP.

IE: 
  0% : RED       (#FF0000)
  8% : ORANGE    (#FF7F00)
 17% : YELLOW    (#FFFF00)
 25% : LIMEGREEN (#7FFF00)
 ... : ...
 83% : MAGENTA   (#FF00FF)
 92% : ROSE      (#FF007F)
100% : RED       (#FF0000)

标签: php colors hex hsv
3条回答
Summer. ? 凉城
2楼-- · 2020-04-16 04:13

Try converting the percentage to HSV (divide the percentage by 100 to get the hue and just set the saturation and value to an arbitrary constant like 1) and then using this question to convert that to RGB form: PHP HSV to RGB formula comprehension

查看更多
在下西门庆
3楼-- · 2020-04-16 04:27

This little snippet would appear to do what you're trying to achieve

http://bytes.com/topic/php/insights/890539-how-produce-first-pair-rgb-hex-color-value-percent-value

function percent2Color($value,$brightness = 255, $max = 100,$min = 0, $thirdColorHex = '00')
{       
    // Calculate first and second color (Inverse relationship)
    $first = (1-($value/$max))*$brightness;
    $second = ($value/$max)*$brightness;

    // Find the influence of the middle color (yellow if 1st and 2nd are red and green)
    $diff = abs($first-$second);    
    $influence = ($brightness-$diff)/2;     
    $first = intval($first + $influence);
    $second = intval($second + $influence);

    // Convert to HEX, format and return
    $firstHex = str_pad(dechex($first),2,0,STR_PAD_LEFT);     
    $secondHex = str_pad(dechex($second),2,0,STR_PAD_LEFT); 

    return $firstHex . $secondHex . $thirdColorHex ; 

    // alternatives:
    // return $thirdColorHex . $firstHex . $secondHex; 
    // return $firstHex . $thirdColorHex . $secondHex;

}
查看更多
甜甜的少女心
4楼-- · 2020-04-16 04:29

This is not my code and I don't remember where I found it. I've been using it for a couple of years and it works perfectly for me. It could be what your looking for (or part of it may be).

<?php
class colour
{
  protected $R = 0; //Red
  protected $G = 0; //Green
  protected $B = 0; //Blue
  protected $H = 0; //Hue
  protected $S = 0; //Saturation
  protected $L = 0; //Luminance
  protected $hex = 0;
  protected $web;
  protected $complement;

  public function __construct($r, $g = false, $b = false)
  {
    //set up array of web colour names
    $this->web = array(
        'black'   => '#000000',
        'green'   => '#008000',
          'silver'  => '#C0C0C0',
        'lime'    => '#00FF00',
        'gray'    => '#808080',
        'olive'   => '#808000',
        'white'   => '#FFFFFF',
        'yellow'  => '#FFFF00',
        'maroon'  => '#800000',
        'navy'    => '#000080',
        'red'     => '#FF0000',
        'blue'    => '#0000FF',
        'purple'  => '#800080',
        'teal'    => '#008080',
        'fuchsia' => '#FF00FF' 
    );
    //accept either R,G,B or hex colour
    //If hex, convert to RGB
    //RGB?
    if($r && $g && $b){
      //yes then save
      $this->R = $r;
      $this->G = $g;
      $this->B = $b;
    } elseif(isset($this->web[strtolower($r)])){
      //named colour?
      $this->hex = $this->web[strtolower($r)];
      $this->convertToRGB($this->hex);
    } else {
      //no - so convert
      if($r[0] != '#') $r = '#' . $r;
      $this->hex = $r;
      $this->convertToRGB($r);
    }
    $this->rgbTohsl();
    //$this->complement = $this->getComplement();
  }

  public function getLighter($factor)
  {
    //return colour lightened by %$factor in hex
    //$factor can be negative to give a darker colour
    if($factor > 1 || $factor < -1) $factor = $factor/100;
    $r = (string)round($this->R+((255 - $this->R) * $factor));
    $g = (string)round($this->G+((255 - $this->G) * $factor));
    $b = (string)round($this->B+((255 - $this->B) * $factor));

    if($r > 255)$r = 255;
    if($r < 0)$r = 0;
    if($g > 255)$g = 255;
    if($g < 0)$g = 0;
    if($b > 255)$b = 255;
    if($b < 0)$b = 0;
    var_dump($r, $g, $b);
    return $this->convertToHex($r, $g, $b);
  }

  public function opposite()
  {
    $r = $this->R;
    $g = $this->G;
    $b = $this->B;

    $r = 255 - $r;
    $g = 255 - $g;
    $b = 255 - $b;

    return $this->convertToHex($r, $g, $b); 

  }

  public function getRGB()
  {
    //returns an array (r, g, b)

    $rgb = array( 'r' => $this->R,
                  'g' => $this->G,
                  'b' => $this->B);
    return $rgb;
  }

  public function getHex()
  {
    if($this->hex) return $this->hex;
    $hex = '#' . dechex($this->R) . dechex($this->G) . dechex($this->B);
    return $hex; 
  }

  public static function namedToHex($name)
  {
    //return hex value of named colour
    $web = array(
        'black'   => '#000000',
        'green'   => '#008000',
          'silver'  => '#C0C0C0',
        'lime'    => '#00FF00',
        'gray'    => '#808080',
        'olive'   => '#808000',
        'white'   => '#FFFFFF',
        'yellow'  => '#FFFF00',
        'maroon'  => '#800000',
        'navy'    => '#000080',
        'red'     => '#FF0000',
        'blue'    => '#0000FF',
        'purple'  => '#800080',
        'teal'    => '#008080',
        'fuchsia' => '#FF00FF' 
    );
    if(isset($web[strtolower($name)])){
      return $web[strtolower($name)];
    }
    return false;
  }

  public static function contrast(colour $fg, colour $bg){
    //calculate and return contrast between $fg and $bg
    $cont = 1;
    $fglum = $fg->luminance();
    $bglum = $bg->luminance();

    if($fglum > $bglum) $cont = ($fglum + 0.05) / ($bglum + 0.05);
    if($bglum > $fglum) $cont = ($bglum + 0.05) / ($fglum + 0.05);

    return $cont;

  }

  public static function colourDiff(colour $colour1, colour $colour2)
  {
    //returns the colour difference between two colours
    $rgb1 = $colour1->getRGB();
    $rgb2 = $colour2->getRGB();

    $r1 = $rgb1['r'];
    $r2 = $rgb2['r'];

    $g1 = $rgb1['g'];
    $g2 = $rgb2['g'];

    $b1 = $rgb1['b'];
    $b2 = $rgb2['b'];

    $diff = max($r1, $r2) - min($r1, $r2);
    $diff += max($g1, $g2) - min($g1, $g2);
    $diff += max($b1, $b2) - min($b1, $b2);

    return $diff;
  }

  public function brightness()
  {
    return (($this->R * 299) + ($this->G * 587) + ($this->B * 114))/1000;
  }

  public function luminance()
  {
    //returns luminence of coulour
    $r = $this->lumCheck($this->R/255);
    $g = $this->lumCheck($this->G/255);
    $b = $this->lumCheck($this->B/255);

    $lum = (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
    return $lum; 
  }

  protected function lumCheck($col)
  {
    if($col <= 0.03298){
      return $col/12.92;
    } else {
      return pow((($col + 0.055)/1.055), 2.4);
    }
  }

  protected function convertToRGB($color)
  {
    $color = (string)$color;
    if($color[0] == '#'){
        $color = substr($color, 1);
    }
    if(strlen($color) == 6){
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    }
    elseif(strlen($color) == 3){
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    }
    else{
        return false;
    }

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    $this->R = (int)$r;
    $this->G = (int)$g;
    $this->B = (int)$b;
  }

  protected function convertToHex($r, $g, $b)
  {
    $r = dechex($r);
    if(strlen($r) == 1)$r = '0' . $r;

    $g = dechex($g);
    if(strlen($g) == 1)$g = '0' . $g;

    $b = dechex($b);
    if(strlen($b) == 1)$b = '0' . $b;

    $hex = '#' . $r . $g . $b;
    return $hex;
  }

  protected function rgbTohsl()
  {
    $r = ( $this->R / 255 );                     //RGB from 0 to 255
    $g = ( $this->G / 255 );
    $b = ( $this->B / 255 );

    $var_Min = min( $r, $g, $b );    //Min. value of RGB
    $var_Max = max( $r, $g, $b );    //Max. value of RGB
    $del_Max = $var_Max - $var_Min;  //Delta RGB value

    $l = ( $var_Max + $var_Min ) / 2;
    $this->L = $l;

    if ( $del_Max == 0 ){           //This is a gray, no chroma...
      $this->H = 0;                                //HSL results from 0 to 1
      $this->S = 0;
    } else {                                   //Chromatic data...
      if ( $l < 0.5 ) {
        $this->S = $del_Max / ( $var_Max + $var_Min );
      } else {
        $this->S = $del_Max / ( 2 - $var_Max - $var_Min );

        $del_R = ( ( ( $var_Max - $r ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
        $del_G = ( ( ( $var_Max - $g ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
        $del_B = ( ( ( $var_Max - $b ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;

        if( $r == $var_Max ){
          $h = $del_B - $del_G;
        } elseif( $g == $var_Max ){
          $h = ( 1 / 3 ) + $del_R - $del_B;
        } elseif( $b == $var_Max ) {
          $h = ( 2 / 3 ) + $del_G - $del_R;
        }

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

  protected function hslTorgb($h, $s, $l)
  {
    if ( $s == 0 ){                       //HSL from 0 to 1
      $r = $l * 255;                      //RGB results from 0 to 255
      $g = $l * 255;
      $b = $l * 255;
    } else {
      if ( $l < 0.5 ) {
        $var_2 = $l * ( 1 + $s );
      } else {
        $var_2 = ( $l + $s ) - ( $s * $l );
      }

      $var_1 = 2 * $l - $var_2;
      $r = 255 * $this->hueToRGB( $var_1, $var_2, $h + ( 1 / 3 ) );
      $g = 255 * $this->hueToRGB( $var_1, $var_2, $h );
      $b = 255 * $this->hueToRGB( $var_1, $var_2, $h - ( 1 / 3 ) );
    }
    return array('r'=>$r,'g'=>$g,'b'=>$b);
  }

  protected function hueToRGB($v1, $v2, $vH)
  {

    if($vH < 0)$vH += 1;
    if($vH > 1)$vH -= 1;
    if ( ( 6 * $vH ) < 1 ) return ( $v1 + ( $v2 - $v1 ) * 6 * $vH );
    if ( ( 2 * $vH ) < 1 ) return ( $v2 );
    if ( ( 3 * $vH ) < 2 ) return ( $v1 + ( $v2 - $v1 ) * ( ( 2 / 3 ) - $vH ) * 6 );
    return ( $v1 );
  }

  public function getComplement()
  {
    $h = $this->H + 180;
    if($h > 360)$h -= 360;
    $rgb = $this->hslTorgb($h, $this->S, $this->L);
    return $this->convertToHex($rgb['r'], $rgb['g'], $rgb['b']);
  }
}
查看更多
登录 后发表回答