2010-08-18 2 views
3

Я хочу знать, как построить функцию, которая дает код цвета, и отображает градиент этого цвета. Например:Создать градиентный цвет от PHP

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


} 

Пожалуйста, помогите мне

+0

Просьба уточнить, что вы подразумеваете под «градиентом» и «бледными цветами». Реальный пример с изображением или числовыми значениями был бы лучшим. –

ответ

2

В ответ на этот вопрос заключается ваше решение, только в Javascript ...

Generate lighter/darker color in css using javascript

Я не буду писать но простой поиск Google для «lighten hex color 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

5

Код ссылки Michael довольно пугающий. Но решение прост. Это может быть более ясным, если учесть, просто полутоновое изображение:

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.

Смежные вопросы