Subversion Repositories php_utils

Rev

Rev 57 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 57 Rev 86
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * Color Utils for PHP
4
 * Color Utils for PHP
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
6
 * Revision 2023-01-03
6
 * Revision 2023-08-29
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 103... Line 103...
103
        return '#'.$color;
103
        return '#'.$color;
104
}
104
}
105
 
105
 
106
// TODO: Also support hsl() and hsla() color schemes
106
// TODO: Also support hsl() and hsla() color schemes
107
function changeCSSWithRgbFunction($css_content, $rgb_function) {
107
function changeCSSWithRgbFunction($css_content, $rgb_function) {
-
 
108
 
-
 
109
        $i = 0;
-
 
110
        do {
-
 
111
                $i++;
-
 
112
                $dummy = "[$i]";
-
 
113
        } while (strpos($css_content, $dummy) !== false);
-
 
114
 
108
        $css_content = preg_replace('@(\\}\\s*)#@ismU', '\\1'.chr(1), $css_content);
115
        $css_content = preg_replace('@(\\}\\s*)#@ismU', '\\1'.$dummy, $css_content);
109
        $css_content = preg_replace('@(^\\s*)#@isU', '\\1'.chr(1), $css_content);
116
        $css_content = preg_replace('@(^\\s*)#@isU', '\\1'.$dummy, $css_content);
110
        $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
117
        $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
111
                function ($x) use ($rgb_function) {
118
                function ($x) use ($rgb_function) {
112
                        if (strlen($x[1]) == 3) {
119
                        if (strlen($x[1]) == 3) {
113
                                $r = hexdec($x[1][0].$x[1][0]);
120
                                $r = hexdec($x[1][0].$x[1][0]);
114
                                $g = hexdec($x[1][1].$x[1][1]);
121
                                $g = hexdec($x[1][1].$x[1][1]);
Line 145... Line 152...
145
                        $g = $x[2];
152
                        $g = $x[2];
146
                        $b = $x[3];
153
                        $b = $x[3];
147
                        $rgb_function($r,$g,$b);
154
                        $rgb_function($r,$g,$b);
148
                        return "-rgb:$r,$g,$b;";
155
                        return "-rgb:$r,$g,$b;";
149
                }, $css_content);
156
                }, $css_content);
150
        $css_content = str_replace(chr(1), '#', $css_content);
157
        $css_content = str_replace($dummy, '#', $css_content);
151
        return $css_content;
158
        return $css_content;
152
}
159
}
153
 
160
 
154
function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
161
function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
155
        $rgb_function = function(&$r,&$g,&$b) use ($h_shift, $s_shift, $v_shift) {
162
        $rgb_function = function(&$r,&$g,&$b) use ($h_shift, $s_shift, $v_shift) {