根据PHP中的字母生成颜色

时间:2010-07-12 17:34:52

标签: php colors

如何根据字符串开头的字母生成颜色 - 也许A可以是蓝色,Z可以是绿色,其他字母是中间逐渐变化的光谱?

5 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

$str = iconv("CURRENT CHARSET HERE", "ASCII//TRANSLIT",  $orig_string);
$letter = ucfirst($str);
//A is 65, Z is 90
$hue = 2*pi() * ((ord($letter) - 65)/(90-65));

这会给你弧度的色调。然后,这只是选择一定的饱和度和亮度并转换为RGB或其他任何东西。请参阅wikipedia

答案 2 :(得分:0)

你肯定会想要使用HSV,因为在那个空间中从一个色调平滑过渡到另一个色调是微不足道的。

可能不是世界上最有效的代码,但是这里有。在代码底部有一个小测试页面,当然可以删除/忽略。

<?php

// RGB_TO_HSV copied from http://www.actionscript.org/forums/showthread.php3?t=50746

function HSV_TO_RGB ($H, $S, $V)  // HSV Values:Number 0-1
{                                 // RGB Results:Number 0-255
    $RGB = array();

    if($S == 0)
    {
        $R = $G = $B = $V * 255;
    }
    else
    {
        $var_H = $H * 6;
        $var_i = floor( $var_H );
        $var_1 = $V * ( 1 - $S );
        $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
        $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );

        if       ($var_i == 0) { $var_R = $V     ; $var_G = $var_3  ; $var_B = $var_1 ; }
        else if  ($var_i == 1) { $var_R = $var_2 ; $var_G = $V      ; $var_B = $var_1 ; }
        else if  ($var_i == 2) { $var_R = $var_1 ; $var_G = $V      ; $var_B = $var_3 ; }
        else if  ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2  ; $var_B = $V     ; }
        else if  ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1  ; $var_B = $V     ; }
        else                   { $var_R = $V     ; $var_G = $var_1  ; $var_B = $var_2 ; }

        $R = $var_R * 255;
        $G = $var_G * 255;
        $B = $var_B * 255;
    }

    $RGB['R'] = $R;
    $RGB['G'] = $G;
    $RGB['B'] = $B;

    return $RGB;
}

function getColorForWord($word) {
    // get the percent of the first letter ranging from 0-1
    $first_letter_code = (ord(strtolower($word[0]))-97)/25.0;

    // add a phase depending on where you want to start on the color spectrum
    // red is 0, green is 0.25, cyan is 0.5, blue is ~0.75, and 1 is back to red
    $hue = $first_letter_code + 0.25;

    // you may also want to divide by how much of the spectrum you want to cover
    // (making the colors range only from green to blue, for instance)
    // but i'll leave that as an exercise

    // constrain it to 0-1
    if ($hue > 1.0)
        $hue -= 1.0;

    // the second value is the saturation ("colorfulness", ranging from gray to fully-colored)
    // the third is the value (brightness)
    $rgb = HSV_TO_RGB($hue, 1, 0.75);

    $hexstring = "#";

    foreach ($rgb as $c)
        $hexstring .= str_pad(dechex($c), 2, "0", STR_PAD_LEFT);

    return $hexstring;
}
?>

<html>

<head>
</head>

<body>
    <form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
        <input type="text" name="target_word" />
        <?php
        if ($_REQUEST["sub"] && $_REQUEST["target_word"] != "") {
            print "<span style=\"font-weight: bold; color: ".getColorForWord($_REQUEST["target_word"]).";\">".$_REQUEST["target_word"]."</span>";
        }
        ?>
        <br />

        <input type="submit" name="sub" value="Colorize" />
    </form>
</body>

答案 3 :(得分:0)

我会指定第一种颜色(100,100,100)的RGB代码和最后一种颜色的RGB代码(200,200,200),基本上是

1..25 B..Y

结果R = firstR +(lastR-firstR)*(1..25 / 26) 结果G = firstG +(lastG-firstG)*(1..25 / 26) 结果B = firstB +(lastB-firstB)*(1..25 / 26)

所以B会给100 +楼((200-100)*(1/26)) 104104104

和Y将是100 + floor((200-100)*(1/26)) 196196196

这是一个基本代码,但它将允许所有3种颜色的渐变,或者只允许1(例如100,100,100到100,100,200),这会对蓝色做渐变

答案 4 :(得分:0)

基于将前六个字母字符(包括空格)转换为相关的十六进制字符的想法,我想到了一个更简单的解决方案。使用十六进制,了解(例如#123456),如果您更改1和2,则朝着零的值将变得更青色,而对于F值将变得更加白。如果您更改3和4,则将很有帮助。朝向零时,它将变得更接近MAGENTA;朝向F时,它将变得更加白。如果更改为5和6,则朝向零时的颜色将变得更加黄,而朝向F的颜色则变得更白色。完成此操作后,我还尝试使用类似的策略来计算补色(对背景有益)。但是十六进制中的中间值变成灰色(7,8)相当困难。

$word="word 1";
$letter = substr(strtolower($word), 0,6);
$i=0;
$hd="#";
while ($i<=5){
$l2=substr($letter,$i,1);
if($l2=="a" || $l2=="b"){$hd=$hd."0";}
elseif($l2=="c" || $l2=="d"){$hd=$hd."1";}
elseif($l2=="e"){$hd=$hd."2";}
elseif($l2=="f" || $l2=="g"){$hd=$hd."3";}
elseif($l2=="h" || $l2=="i"){$hd=$hd."4";}
elseif($l2=="j"){$hd=$hd."5";}
elseif($l2=="k" || $l2=="l"){$hd=$hd."6";}
elseif($l2=="m" || $l2=="n"){$hd=$hd."7";}
elseif($l2=="o"){$hd=$hd."8";}
elseif($l2=="p" || $l2=="q"){$hd=$hd."9";}
elseif($l2=="r" || $l2=="s"){$hd=$hd."A";}
elseif($l2=="t"){$hd=$hd."B";}
elseif($l2=="u" || $l2=="v"){$hd=$hd."C";}
elseif($l2=="w" || $l2=="x"){$hd=$hd."D";}
elseif($l2=="y"){$hd=$hd."E";}
elseif($l2=="z" || $l2==" " || $l2==""){$hd=$hd."F";}
$i++;
}
//calculating the complimentary colour
$o=1;
$comp="#";
while($o<=6){
$see=substr($hd,$o,1);
if($see=="F"){$comp=$comp."0";}
elseif($see=="E"){$comp=$comp."1";}
elseif($see=="D"){$comp=$comp."2";}
elseif($see=="C"){$comp=$comp."3";}
elseif($see=="B"){$comp=$comp."4";}
elseif($see=="A"){$comp=$comp."5";}
elseif($see=="9"){$comp=$comp."F";}
elseif($see=="8"){$comp=$comp."0";}
elseif($see=="7"){$comp=$comp."F";}
elseif($see=="6"){$comp=$comp."0";}
elseif($see=="5"){$comp=$comp."F";}
elseif($see=="4"){$comp=$comp."B";}
elseif($see=="3"){$comp=$comp."C";}
elseif($see=="2"){$comp=$comp."D";}
elseif($see=="1"){$comp=$comp."E";}
elseif($see=="0"){$comp=$comp."F";}
$o++;
}
echo" hexadecimal is $hd, complimentary colour is $comp";