Subversion Repositories php_utils

Rev

Rev 53 | Rev 57 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 53 Rev 54
Line 104... Line 104...
104
}
104
}
105
 
105
 
106
function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
106
function changeHueOfCSS($css_content, $h_shift=0, $s_shift=0, $v_shift=0) {
107
        // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
107
        // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
108
        // TODO: Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept
108
        // TODO: Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept
-
 
109
        $css_content = preg_replace('@(\\}\\s*)#@ismU', '\\1'.chr(1), $css_content);
-
 
110
        $css_content = preg_replace('@(^\\s*)#@isU', '\\1'.chr(1), $css_content);
109
        $css_content = preg_replace_callback('@(:|,)\\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
111
        $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
110
                function ($x) use ($h_shift, $s_shift, $v_shift) {
112
                function ($x) use ($h_shift, $s_shift, $v_shift) {
111
                        if (strlen($x[2]) == 3) {
113
                        if (strlen($x[1]) == 3) {
112
                                $r = hexdec($x[2][0].$x[2][0]);
114
                                $r = hexdec($x[1][0].$x[1][0]);
113
                                $g = hexdec($x[2][1].$x[2][1]);
115
                                $g = hexdec($x[1][1].$x[1][1]);
114
                                $b = hexdec($x[2][2].$x[2][2]);
116
                                $b = hexdec($x[1][2].$x[1][2]);
115
                        } else {
117
                        } else {
116
                                $r = hexdec($x[2][0].$x[2][1]);
118
                                $r = hexdec($x[1][0].$x[1][1]);
117
                                $g = hexdec($x[2][2].$x[2][3]);
119
                                $g = hexdec($x[1][2].$x[1][3]);
118
                                $b = hexdec($x[2][4].$x[2][5]);
120
                                $b = hexdec($x[1][4].$x[1][5]);
119
                        }
121
                        }
120
                        list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b);
122
                        list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b);
121
                        $h = (float)$h;
123
                        $h = (float)$h;
122
                        $s = (float)$s;
124
                        $s = (float)$s;
123
                        $v = (float)$v;
125
                        $v = (float)$v;
124
                        $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1;
126
                        $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1;
125
                        $s = ($s + $s_shift); while ($s > 1) $s  = 1; while ($s < 0) $s  = 0;
127
                        $s = ($s + $s_shift); while ($s > 1) $s  = 1; while ($s < 0) $s  = 0;
126
                        $v = ($v + $v_shift); while ($v > 1) $v  = 1; while ($v < 0) $v  = 0;
128
                        $v = ($v + $v_shift); while ($v > 1) $v  = 1; while ($v < 0) $v  = 0;
127
                        list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v);
129
                        list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v);
128
                        return ':'.rgb2html($r,$g,$b);
130
                        return rgb2html($r,$g,$b);
129
                }, $css_content);
131
                }, $css_content);
-
 
132
        $css_content = str_replace(chr(1), '#', $css_content);
130
        return $css_content;
133
        return $css_content;
131
}
134
}
132
 
135
 
133
function invertColorsOfCSS($css_content) {
136
function invertColorsOfCSS($css_content) {
134
        // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
137
        // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?)
135
        // TODO: Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept
138
        // TODO: Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept
-
 
139
        $css_content = preg_replace('@(\\}\\s*)#@ismU', '\\1'.chr(1), $css_content);
-
 
140
        $css_content = preg_replace('@(^\\s*)#@isU', '\\1'.chr(1), $css_content);
136
        $css_content = preg_replace_callback('@(:|,)\\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
141
        $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU',
137
                function ($x) {
142
                function ($x) {
138
                        if (strlen($x[2]) == 3) {
143
                        if (strlen($x[1]) == 3) {
139
                                $r = hexdec($x[2][0].$x[2][0]);
144
                                $r = hexdec($x[1][0].$x[1][0]);
140
                                $g = hexdec($x[2][1].$x[2][1]);
145
                                $g = hexdec($x[1][1].$x[1][1]);
141
                                $b = hexdec($x[2][2].$x[2][2]);
146
                                $b = hexdec($x[1][2].$x[1][2]);
142
                        } else {
147
                        } else {
143
                                $r = hexdec($x[2][0].$x[2][1]);
148
                                $r = hexdec($x[1][0].$x[1][1]);
144
                                $g = hexdec($x[2][2].$x[2][3]);
149
                                $g = hexdec($x[1][2].$x[1][3]);
145
                                $b = hexdec($x[2][4].$x[2][5]);
150
                                $b = hexdec($x[1][4].$x[1][5]);
146
                        }
151
                        }
147
                        $r = 255 - $r;
152
                        $r = 255 - $r;
148
                        $g = 255 - $g;
153
                        $g = 255 - $g;
149
                        $b = 255 - $b;
154
                        $b = 255 - $b;
150
                        return ':'.rgb2html($r,$g,$b);
155
                        return rgb2html($r,$g,$b);
151
                }, $css_content);
156
                }, $css_content);
-
 
157
        $css_content = str_replace(chr(1), '#', $css_content);
152
        return $css_content;
158
        return $css_content;
153
}
159
}