Subversion Repositories php_utils

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * Color utilities
  5.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. // Some of these functions were taken from other sources.
  21.  
  22. function RGB_TO_HSV($R, $G, $B) { // RGB Values:Number 0-255
  23.                                   // HSV Results:Number 0-1
  24.         $HSL = array();
  25.  
  26.         $var_R = ($R / 255);
  27.         $var_G = ($G / 255);
  28.         $var_B = ($B / 255);
  29.  
  30.         $var_Min = min($var_R, $var_G, $var_B);
  31.         $var_Max = max($var_R, $var_G, $var_B);
  32.         $del_Max = $var_Max - $var_Min;
  33.  
  34.         $V = $var_Max;
  35.  
  36.         if ($del_Max == 0) {
  37.                 $H = 0;
  38.                 $S = 0;
  39.         } else {
  40.                 $S = $del_Max / $var_Max;
  41.  
  42.                 $del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max;
  43.                 $del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max;
  44.                 $del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max;
  45.  
  46.                 if      ($var_R == $var_Max) $H = $del_B - $del_G;
  47.                 else if ($var_G == $var_Max) $H = (1/3) + $del_R - $del_B;
  48.                 else if ($var_B == $var_Max) $H = (2/3) + $del_G - $del_R;
  49.                 else $H = 0;
  50.  
  51.                 if ($H<0) $H++;
  52.                 if ($H>1) $H--;
  53.         }
  54.  
  55.         return array($H, $S, $V);
  56. }
  57.  
  58. function HSV_TO_RGB($H, $S, $V) { // HSV Values:Number 0-1
  59.                                   // RGB Results:Number 0-255
  60.         $RGB = array();
  61.  
  62.         if($S == 0) {
  63.                 $R = $G = $B = $V * 255;
  64.         } else {
  65.                 $var_H = $H * 6;
  66.                 $var_i = floor( $var_H );
  67.                 $var_1 = $V * ( 1 - $S );
  68.                 $var_2 = $V * ( 1 - $S * (     $var_H - $var_i ));
  69.                 $var_3 = $V * ( 1 - $S * (1 - ($var_H - $var_i )));
  70.  
  71.                 if       ($var_i == 0) { $var_R = $V     ; $var_G = $var_3  ; $var_B = $var_1 ; }
  72.                 else if  ($var_i == 1) { $var_R = $var_2 ; $var_G = $V      ; $var_B = $var_1 ; }
  73.                 else if  ($var_i == 2) { $var_R = $var_1 ; $var_G = $V      ; $var_B = $var_3 ; }
  74.                 else if  ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2  ; $var_B = $V     ; }
  75.                 else if  ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1  ; $var_B = $V     ; }
  76.                 else                   { $var_R = $V     ; $var_G = $var_1  ; $var_B = $var_2 ; }
  77.  
  78.                 $R = $var_R * 255;
  79.                 $G = $var_G * 255;
  80.                 $B = $var_B * 255;
  81.         }
  82.  
  83.         return array($R, $G, $B);
  84. }
  85.  
  86. function rgb2html($r, $g=-1, $b=-1) {
  87.         if (is_array($r) && sizeof($r) == 3) {
  88.                 list($r, $g, $b) = $r;
  89.         }
  90.  
  91.         $r = intval($r);
  92.         $g = intval($g);
  93.         $b = intval($b);
  94.  
  95.         $r = dechex($r<0 ? 0 : ($r>255 ? 255 : $r));
  96.         $g = dechex($g<0 ? 0 : ($g>255 ? 255 : $g));
  97.         $b = dechex($b<0 ? 0 : ($b>255 ? 255 : $b));
  98.  
  99.         $color  = (strlen($r) < 2 ? '0' : '').$r;
  100.         $color .= (strlen($g) < 2 ? '0' : '').$g;
  101.         $color .= (strlen($b) < 2 ? '0' : '').$b;
  102.         return '#'.$color;
  103. }
  104.  
  105. function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
  106.         // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
  107.         $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
  108.                 function ($x) use ($h_shift, $s_shift, $v_shift) {
  109.                         if (strlen($x[1]) == 3) {
  110.                                 $r = hexdec($x[1][0].$x[1][0]);
  111.                                 $g = hexdec($x[1][1].$x[1][1]);
  112.                                 $b = hexdec($x[1][2].$x[1][2]);
  113.                         } else {
  114.                                 $r = hexdec($x[1][0].$x[1][1]);
  115.                                 $g = hexdec($x[1][2].$x[1][3]);
  116.                                 $b = hexdec($x[1][4].$x[1][5]);
  117.                         }
  118.                         list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b);
  119.                         $h = (float)$h;
  120.                         $s = (float)$s;
  121.                         $v = (float)$v;
  122.                         $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1;
  123.                         $s = ($s + $s_shift); while ($s > 1) $s  = 1; while ($s < 0) $s  = 0;
  124.                         $v = ($v + $v_shift); while ($v > 1) $v  = 1; while ($v < 0) $v  = 0;
  125.                         list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v);
  126.                         return rgb2html($r,$g,$b);
  127.                 }, $css_content);
  128.         return $css_content;
  129. }
  130.  
  131. function invertColorsOfCSS($css_content) {
  132.         $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
  133.                 function ($x) {
  134.                         if (strlen($x[1]) == 3) {
  135.                                 $r = hexdec($x[1][0].$x[1][0]);
  136.                                 $g = hexdec($x[1][1].$x[1][1]);
  137.                                 $b = hexdec($x[1][2].$x[1][2]);
  138.                         } else {
  139.                                 $r = hexdec($x[1][0].$x[1][1]);
  140.                                 $g = hexdec($x[1][2].$x[1][3]);
  141.                                 $b = hexdec($x[1][4].$x[1][5]);
  142.                         }
  143.                         $r = 255 - $r;
  144.                         $g = 255 - $g;
  145.                         $b = 255 - $b;
  146.                         return rgb2html($r,$g,$b);
  147.                 }, $css_content);
  148.         return $css_content;
  149. }
  150.