【发布时间】:2015-07-02 14:26:34
【问题描述】:
首先让我说我整天在互联网上寻找解决方案,但我只是难住了。我设法找到足够的代码 sn-ps 来组合我需要的“几乎”工作版本 - 但老实说,我只是迷失了如何让它工作。
这是我想要做的:
我正在尝试制作一个 php 函数,该函数将采用 2 种或 3 种颜色,并将它们作为平滑渐变应用于文本字符串。 我需要该函数来输出渐变的实际 HTML 代码。我设想它的工作方式是:它将获取消息字符串并将其拆分为单个字符,然后以这样一种方式为每个字符着色,当它与 html 输出一起显示时,它看起来就像从一种颜色平滑淡入到下一个。现在我正在使用我刚刚在其中定义的 2 种颜色(FF0000 和 0000FF)测试该函数。我似乎无法让它为整个字符串着色。它似乎抓住了第一个字母,并进行了部分转换,然后就停止了。
这是我试图让它看起来像的屏幕截图:
这是我的屏幕截图(包括用于解释的 html 输出)
这是我正在使用的代码:
<?php
function html2rgb($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);
return array($r, $g, $b);
}
function rgb2html($r, $g=-1, $b=-1)
{
if (is_array($r) && sizeof($r) == 3)
list($r, $g, $b) = $r;
$r = intval($r); $g = intval($g);
$b = intval($b);
$r = dechex($r<0?0:($r>255?255:$r));
$g = dechex($g<0?0:($g>255?255:$g));
$b = dechex($b<0?0:($b>255?255:$b));
$color = (strlen($r) < 2?'0':'').$r;
$color .= (strlen($g) < 2?'0':'').$g;
$color .= (strlen($b) < 2?'0':'').$b;
return '#'.$color;
}
echo "<h1>Result:</h1>";
$src_color = html2rgb('FF0000');
$dst_color = html2rgb('0000FF');
print_r($dst_color);
for($i=0; $i<3; $i++)
$step_color[$i] = ( $dst_color[$i] - $src_color[$i] ) / 30.30;
// step_color array contains difference between adjacent color stripes
$html_out = ''; // html code container
for($j=0; $j<60; $j++)
{
// generate color stripe code
$message = 'I am trying to make this text string fade from one color to another';
$counter = strlen($message);
$array = str_split($message);
$mycount = 0;
if($mycount < $counter){
$line = '<b><font color=" '.rgb2html($src_color).';">'.$array[$mycount].'</font></b>';
$html_out .= "{$line}"; // save color stripe to display HTML code later
$mycount = $mycount + 1;
}
echo $line; // output color stripe to browser
for($i=0; $i<1; $i++) // incrementing each color component
$src_color[$i] += $step_color[$i];
}
?>
<h1>HTML Code:</h1>
<pre><?php
// output HTML code replacing < and > with < and >
$stuff = strtr($html_out, array('&' => '&',
'<' => '<',
'>'=> '>'));
echo $stuff;
我对这种事情还很陌生,所以如果我的代码“落后”或很糟糕,请不要对我太残忍。谁能指出我正确的方向?我只是不知道如何让它做我想做的事。
非常感谢您抽出宝贵时间阅读本文以及您可以提供的任何建议!
编辑:底部屏幕截图的图片链接,使其更易于查看 http://i.stack.imgur.com/vsfdQ.jpg
更新——好的,我已经重写了大部分函数,并且几乎可以正常工作了。我现在遇到的问题是它一遍又一遍地重复整个字符串。它正在应用淡入淡出,但不是它应该的方式。我需要它在字符串中从一种颜色褪色到另一种颜色。这是它现在正在做什么的新屏幕截图: 这是该链接,因此您可以更轻松地查看它: http://i.stack.imgur.com/X0Pmq.jpg
这是我正在使用的新代码:
<?php
function rgb($rgb) {
$ret = '';
foreach ($rgb as $x) {
// Make sure the RGB values are 0-255...
$x = max(0, min(255, $x));
// create a 2 digit hex value for this color component...
$ret .= ($x < 16 ? '0'.dechex($x) : dechex($x));
}
return '#'.$ret;
}
// Returns a fade of $from into $to by $amount ($from and $to are RGB arrays)...
function fade($from, $to, $amount) {
$rgb = array();
// Go through the RGB values and adjust the values by $amount...
for ($i = 0; $i <= 2; $i++) {
$rgb[$i] = (($to[$i] - $from[$i]) * $amount) + $from[$i];
}
return $rgb;
}
$string = 'testing out the fade function here';
//$string1 = strlen($string);
for ($j = 0; $j < 1; $j += .01) {
$color = rgb(fade(array(255,0,0), array(0,255,0), $j));
for($i=0;$i<strlen($string);$i++){
echo "<font style='color:$color'>$string[$i]</font>";
}
}
有谁能告诉我如何让它只打印一次字符串,并将淡入淡出正确应用于字符串?
非常感谢你们所有的时间和专业知识!
【问题讨论】:
-
请看我的编辑。起初它为整条线着色。现在它只适用于文本