Witam serdecznie populację forum

Kilka minut temu mój brat nauczył mnie systemu szesnastkowego, którym zapisany jest kolor hex. Postanowiłem napisać funkcję

<?
/***************************************************
* Autorem funkcji jest tiraeth *
* Funkcja odpowiada za zamianę koloru RGB na *
* szesnastkowy system HEX. *
***************************************************/
function rgb2hex($red, $green, $blue)
{
// Jeżeli podana liczba danego koloru jest nieprawidłowa wywalamy błąd
if($red > 255 || $green > 255 || $blue > 255)
{
return 'Podany kolor jest nieprawidłowy';
}
// Tablica z odpowiednimi zamiennikami liczb 10-15
10 => 'A',
11 => 'B',
12 => 'C',
13 => 'D',
14 => 'E',
15 => 'F');
// Kolor czerwony
$hex_red_a = floor($red/16
); $hex_red_b = ($red/16)-$hex_red_a;
$hex_red_c = $hex_red_b*16;
if($hex_red_c>9) { $hex_red_c = $hex[$hex_red_c]; }
$hex_red = $hex_red_a.$hex_red_c;
// Kolor zielony
$hex_green_a = floor($green/16
); $hex_green_b = ($green/16)-$hex_green_a;
$hex_green_c = $hex_green_b*16;
if($hex_green_c>9) { $hex_green_c = $hex[$hex_green_c]; }
$hex_green = $hex_green_a.$hex_green_c;
// Kolor niebieski
$hex_blue_a = floor($blue/16
); $hex_blue_b = ($blue/16)-$hex_blue_a;
$hex_blue_c = $hex_blue_b*16;
if($hex_blue_c>9) { $hex_blue_c = $hex[$hex_blue_c]; }
$hex_blue = $hex_blue_a.$hex_blue_c;
// Nowy kolor HEX
$new_hex_color = $hex_red.$hex_green.$hex_blue;
return $new_hex_color;
}
?>
Przykład zastosowania:
<?php
echo '#'.rgb2red
(112
, 90
, 15
); ?>
wyświetli #705A0F
Przykład on-line: http://php.kom.pl/rgb2hex.php - rgb(112, 90, 15)
Ten post edytował tiraeth 29.03.2006, 17:29:31