PHP中的Hexcolor百分比 [英] Percentage to Hexcolor in PHP

查看:186
本文介绍了PHP中的Hexcolor百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个百分比变成十六进制颜色。种类的HSV颜色度如何工作...

我使用PHP。

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

这个小代码片段似乎是做你想达到的目的



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')
{
//计算第一和第二个颜色(反向关系)
$ first =(1 - ($ value / $ max))* $亮度;
$ second =($ value / $ max)* $ brightness;

//查找中间颜色的影响(黄色,如果第一和第二是红色和绿色)
$ diff = abs($ first- $ second);
$ influence =($亮度$ diff)/ 2;
$ first = intval($ first + $ influence);
$ second = intval($ second + $ influence);

//转换为HEX格式并返回
$ firstHex = str_pad(dechex($ first),2,0,STR_PAD_LEFT);
$ secondHex = str_pad(dechex($ second),2,0,STR_PAD_LEFT);

return $ firstHex。 $ secondHex。 $ thirdColorHex;

//替代方法:
// return $ thirdColorHex。 $ firstHex。 $ secondHex;
// return $ firstHex。 $ thirdColorHex。 $ secondHex;

}


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)

解决方案

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;

}

这篇关于PHP中的Hexcolor百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆