2010-08-18 112 views
3

我想知道如何構建一個給出顏色代碼的函數,並且 會顯示此顏色的漸變。例如:從PHP生成漸變顏色

function generate_color(int colorindex) 
{ ....... 
    ....... 
    Generate 10 pale colors of this color. 


} 

請幫我

+0

請說明您的意思是「漸變」和「蒼白的顏色」。具有圖像或數字值的真實世界的例子將是最好的。 – 2010-08-18 10:40:45

回答

2

在這個問題的答案在於你的解決方案,只有在Javascript ...

Generate lighter/darker color in css using javascript

我不打算把它寫但一個簡單的谷歌搜索'減淡十六進制顏色php'產量:

function colourBrightness($hex, $percent) { 
// Work out if hash given 
$hash = ''; 
if (stristr($hex,'#')) { 
    $hex = str_replace('#','',$hex); 
    $hash = '#'; 
} 
/// HEX TO RGB 
$rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2))); 
//// CALCULATE 
for ($i=0; $i<3; $i++) { 
    // See if brighter or darker 
    if ($percent > 0) { 
    // Lighter 
    $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent)); 
    } else { 
    // Darker 
    $positivePercent = $percent - ($percent*2); 
    $rgb[$i] = round($rgb[$i] * $positivePercent) + round(0 * (1-$positivePercent)); 
    } 
    // In case rounding up causes us to go to 256 
    if ($rgb[$i] > 255) { 
    $rgb[$i] = 255; 
    } 
} 
//// RBG to Hex 
$hex = ''; 
for($i=0; $i < 3; $i++) { 
    // Convert the decimal digit to hex 
    $hexDigit = dechex($rgb[$i]); 
    // Add a leading zero if necessary 
    if(strlen($hexDigit) == 1) { 
    $hexDigit = "0" . $hexDigit; 
    } 
    // Append to the hex string 
    $hex .= $hexDigit; 
} 
return $hash.$hex; 
} 

http://lab.pxwebdesign.com.au/?p=14

您的Google和我一樣好!

+1

你可以給我一些在PHP中的東西 – eni 2010-08-18 11:02:25

5

邁克爾引用的代碼是相當可怕的。但解決方案很簡單。如果您僅考慮灰度圖像,則可能會更清晰:

function create_pallette($start, $end, $entries=10) 
{ 
    $inc=($start - $end)/($entries-1); 
    $out=array(0=>$start); 
    for ($x=1; $x<$entries;$x++) { 
     $out[$x]=$start+$inc * $x; 
    } 
    return $out; 
} 

僅使用3D矢量(RGB)代替1D矢量。

C.