Subversion Repositories oidplus

Rev

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

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  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.  
  50.                 if ($H<0) $H++;
  51.                 if ($H>1) $H--;
  52.         }
  53.  
  54.         return array($H, $S, $V);
  55. }
  56.  
  57. function HSV_TO_RGB($H, $S, $V) { // HSV Values:Number 0-1
  58.                                   // RGB Results:Number 0-255
  59.         $RGB = array();
  60.  
  61.         if($S == 0) {
  62.                 $R = $G = $B = $V * 255;
  63.         } else {
  64.                 $var_H = $H * 6;
  65.                 $var_i = floor( $var_H );
  66.                 $var_1 = $V * ( 1 - $S );
  67.                 $var_2 = $V * ( 1 - $S * (     $var_H - $var_i ));
  68.                 $var_3 = $V * ( 1 - $S * (1 - ($var_H - $var_i )));
  69.  
  70.                 if       ($var_i == 0) { $var_R = $V     ; $var_G = $var_3  ; $var_B = $var_1 ; }
  71.                 else if  ($var_i == 1) { $var_R = $var_2 ; $var_G = $V      ; $var_B = $var_1 ; }
  72.                 else if  ($var_i == 2) { $var_R = $var_1 ; $var_G = $V      ; $var_B = $var_3 ; }
  73.                 else if  ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2  ; $var_B = $V     ; }
  74.                 else if  ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1  ; $var_B = $V     ; }
  75.                 else                   { $var_R = $V     ; $var_G = $var_1  ; $var_B = $var_2 ; }
  76.  
  77.                 $R = $var_R * 255;
  78.                 $G = $var_G * 255;
  79.                 $B = $var_B * 255;
  80.         }
  81.  
  82.         return array($R, $G, $B);
  83. }
  84.  
  85. function rgb2html($r, $g=-1, $b=-1) {
  86.         if (is_array($r) && sizeof($r) == 3) {
  87.                 list($r, $g, $b) = $r;
  88.         }
  89.  
  90.         $r = intval($r);
  91.         $g = intval($g);
  92.         $b = intval($b);
  93.  
  94.         $r = dechex($r<0 ? 0 : ($r>255 ? 255 : $r));
  95.         $g = dechex($g<0 ? 0 : ($g>255 ? 255 : $g));
  96.         $b = dechex($b<0 ? 0 : ($b>255 ? 255 : $b));
  97.  
  98.         $color  = (strlen($r) < 2 ? '0' : '').$r;
  99.         $color .= (strlen($g) < 2 ? '0' : '').$g;
  100.         $color .= (strlen($b) < 2 ? '0' : '').$b;
  101.         return '#'.$color;
  102. }
  103.  
  104. function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
  105.         // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
  106.         $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
  107.                 function ($x) use ($h_shift, $s_shift, $v_shift) {
  108.                         if (strlen($x[1]) == 3) {
  109.                                 $r = hexdec($x[1][0].$x[1][0]);
  110.                                 $g = hexdec($x[1][1].$x[1][1]);
  111.                                 $b = hexdec($x[1][2].$x[1][2]);
  112.                         } else {
  113.                                 $r = hexdec($x[1][0].$x[1][1]);
  114.                                 $g = hexdec($x[1][2].$x[1][3]);
  115.                                 $b = hexdec($x[1][4].$x[1][5]);
  116.                         }
  117.                         list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b);
  118.                         $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1;
  119.                         $s = ($s + $s_shift); while ($s > 1) $s  = 1; while ($s < 0) $s  = 0;
  120.                         $v = ($v + $v_shift); while ($v > 1) $v  = 1; while ($v < 0) $v  = 0;
  121.                         list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v);
  122.                         return rgb2html($r,$g,$b);
  123.                 }, $css_content);
  124.         return $css_content;
  125. }
  126.  
  127. function invertColorsOfCSS($css_content) {
  128.         $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
  129.                 function ($x) {
  130.                         if (strlen($x[1]) == 3) {
  131.                                 $r = hexdec($x[1][0].$x[1][0]);
  132.                                 $g = hexdec($x[1][1].$x[1][1]);
  133.                                 $b = hexdec($x[1][2].$x[1][2]);
  134.                         } else {
  135.                                 $r = hexdec($x[1][0].$x[1][1]);
  136.                                 $g = hexdec($x[1][2].$x[1][3]);
  137.                                 $b = hexdec($x[1][4].$x[1][5]);
  138.                         }
  139.                         $r = 255 - $r;
  140.                         $g = 255 - $g;
  141.                         $b = 255 - $b;
  142.                         return rgb2html($r,$g,$b);
  143.                 }, $css_content);
  144.         return $css_content;
  145. }
  146.