Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zamiana koloru RGB na HEX, Funkcja, php 4 i 5
tiraeth
post 17.10.2004, 16:59:22
Post #1





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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

  1. <?
  2. /***************************************************
  3.  * Autorem funkcji jest tiraeth *
  4.  * Funkcja odpowiada za zamianę koloru RGB na *
  5.  * szesnastkowy system HEX. *
  6.  ***************************************************/
  7.  
  8. function rgb2hex($red, $green, $blue)
  9. {
  10. // Jeżeli podana liczba danego koloru jest nieprawidłowa wywalamy błąd
  11. if($red > 255 || $green > 255 || $blue > 255)
  12. {
  13. return 'Podany kolor jest nieprawidłowy';
  14. die();
  15. }
  16.  
  17. // Tablica z odpowiednimi zamiennikami liczb 10-15
  18. $hex = array(
  19. 10 => 'A', 
  20. 11 => 'B', 
  21. 12 => 'C', 
  22. 13 => 'D', 
  23. 14 => 'E', 
  24. 15 => 'F');
  25.  
  26. // Kolor czerwony
  27. $hex_red_a = floor($red/16);
  28. $hex_red_b = ($red/16)-$hex_red_a;
  29. $hex_red_c = $hex_red_b*16;
  30. if($hex_red_c>9) { $hex_red_c = $hex[$hex_red_c]; }
  31. $hex_red = $hex_red_a.$hex_red_c;
  32.  
  33. // Kolor zielony
  34. $hex_green_a = floor($green/16);
  35. $hex_green_b = ($green/16)-$hex_green_a;
  36. $hex_green_c = $hex_green_b*16;
  37. if($hex_green_c>9) { $hex_green_c = $hex[$hex_green_c]; }
  38. $hex_green = $hex_green_a.$hex_green_c;
  39.  
  40. // Kolor niebieski
  41. $hex_blue_a = floor($blue/16);
  42. $hex_blue_b = ($blue/16)-$hex_blue_a;
  43. $hex_blue_c = $hex_blue_b*16;
  44. if($hex_blue_c>9) { $hex_blue_c = $hex[$hex_blue_c]; }
  45. $hex_blue = $hex_blue_a.$hex_blue_c;
  46.  
  47. // Nowy kolor HEX
  48. $new_hex_color = $hex_red.$hex_green.$hex_blue;
  49.  
  50. return $new_hex_color;
  51. }
  52. ?>


Przykład zastosowania:
  1. <?php
  2. echo '#'.rgb2red(112, 90, 15);
  3. ?>

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
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.06.2025 - 07:25