Percentage to Hexcolor in PHP

前端 未结 3 1878
渐次进展
渐次进展 2021-01-23 04:11

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          


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 04:55

    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;
    
    }
    

提交回复
热议问题