logo

INTERNETNE STORITVE

- IZDELAVA SPLETNIH STRANI - OBLIKOVANJE - PROGRAMIRANJE DODATKOV

PHP - pretvorba RGB vrednosti v HTML hex color - HTML hex color v RGB vrednost (RGB to HTML, HTML to RGB)

Včasih se zgodi, da potrebujemo RGB vrednost namesto HTML kod, včasih pa ravno obratno in sicer iz HTML kod v RGB vrednost. Zato si poglejmo dve kratki PHP funkciji, ki skrbita za pretvorbo med RGB in HTML (RGB v HTML, HTML v RGB).

Najprej si poglejmo HTML2RGB. Malce več informacij o HTML hex colors:

HTML color codes are hexadecimal triplets representing the colors red, green, and blue. For example, in the the color red, the color code is FF0000, which is '255' red, '0' green, and '0' blue.

PHP:
  1. function HTML2RGB($htmlHexColor, $defaultColor = 'FF0000') {
  2.     // Remove #
  3.     $htmlHexColor = str_replace('#', '', $htmlHexColor);
  4.  
  5.     // We can have FF 00 00 or F 0 0
  6.     if (strlen($htmlHexColor) == 3 OR strlen($htmlHexColor) == 6) {
  7.         $rgbValues = array();
  8.  
  9.         // Create from F 0 0 -> FF 00 00
  10.         if (strlen($htmlHexColor) == 3) {
  11.             for($i=0; $i<3; $i++) {
  12.                 $rgbValues[] = $htmlHexColor[$i].$htmlHexColor[$i];
  13.             }
  14.         } else {
  15.             for($i=0; $i<6; $i=$i+2) {
  16.                 $rgbValues[] = $htmlHexColor[$i].$htmlHexColor[$i+1];
  17.             }
  18.         }
  19.  
  20.         // Go trough rgb values and convert from hex to dec
  21.         foreach($rgbValues as $key => $hex) {
  22.             $rgbValues[$key] = hexdec($hex);
  23.         }
  24.  
  25.         return $rgbValues;
  26.     }
  27.  
  28.     // Return default rgb color
  29.     return HTML2RGB($defaultColor);
  30. }

Sedaj pa še v obratni smeri in sicer RGB2HTML:

PHP:
  1. function RGB2HTML($rgbColors, $defaultColor = array(255, 0, 0)) {
  2.     if (is_array($rgbColors) && count($rgbColors) == 3) {
  3.         // Go trough all values
  4.         foreach($rgbColors as $key => $color) {
  5.             // trim any spaces
  6.             $rgbColors[$key] = trim($rgbColors[$key]);
  7.  
  8.             // Convert values greater then 255 to 255, values smaller then 0 to 0
  9.             if (intval($color)> 255) $rgbColors[$key] = 255;
  10.             elseif (intval($color) <0) $rgbColors[$key] = 0;
  11.  
  12.             // Convert dec to hex
  13.             $rgbColors[$key] = dechex($rgbColors[$key]);
  14.  
  15.             // If we only have X Y Z convert to XX YY ZZ
  16.             if (strlen($rgbColors[$key]) == 1) {
  17.                 $rgbColors[$key] = '0'.$rgbColors[$key];
  18.             }
  19.         }
  20.  
  21.         $htmlColor = implode('', $rgbColors);
  22.         return "#$htmlColor";
  23.     }
  24.  
  25.     return RGB2HTML($defaultColor);
  26. }

Dodatne informacije:

Preizkusite delovanje pretvorbe med RGB v HTML in HTML v RGB s pomočjo PHP skripte - testna skripta.

Dodaj komentar