Rev 54 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 54 | Rev 57 | ||
---|---|---|---|
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 - 2022 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * Revision 2022-12-27 |
6 | * Revision 2023-01-03 |
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 101... | Line 101... | ||
101 | $color .= (strlen($g) < 2 ? '0' : '').$g; |
101 | $color .= (strlen($g) < 2 ? '0' : '').$g; |
102 | $color .= (strlen($b) < 2 ? '0' : '').$b; |
102 | $color .= (strlen($b) < 2 ? '0' : '').$b; |
103 | return '#'.$color; |
103 | return '#'.$color; |
104 | } |
104 | } |
105 | 105 | ||
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?) |
106 | // TODO: Also support hsl() and hsla() color schemes |
108 | // TODO: Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept |
107 | function changeCSSWithRgbFunction($css_content, $rgb_function) { |
109 | $css_content = preg_replace('@(\\}\\s*)#@ismU', '\\1'.chr(1), $css_content); |
108 | $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('@(^\\s*)#@isU', '\\1'.chr(1), $css_content); |
111 | $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU', |
110 | $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU', |
112 | function ($x) use ($h_shift, $s_shift, $v_shift) { |
111 | function ($x) use ($rgb_function) { |
113 | if (strlen($x[1]) == 3) { |
112 | if (strlen($x[1]) == 3) { |
114 | $r = hexdec($x[1][0].$x[1][0]); |
113 | $r = hexdec($x[1][0].$x[1][0]); |
115 | $g = hexdec($x[1][1].$x[1][1]); |
114 | $g = hexdec($x[1][1].$x[1][1]); |
116 | $b = hexdec($x[1][2].$x[1][2]); |
115 | $b = hexdec($x[1][2].$x[1][2]); |
117 | } else { |
116 | } else { |
118 | $r = hexdec($x[1][0].$x[1][1]); |
117 | $r = hexdec($x[1][0].$x[1][1]); |
119 | $g = hexdec($x[1][2].$x[1][3]); |
118 | $g = hexdec($x[1][2].$x[1][3]); |
120 | $b = hexdec($x[1][4].$x[1][5]); |
119 | $b = hexdec($x[1][4].$x[1][5]); |
121 | } |
120 | } |
- | 121 | $rgb_function($r,$g,$b); |
|
- | 122 | return rgb2html($r,$g,$b); |
|
- | 123 | }, $css_content); |
|
- | 124 | $css_content = preg_replace_callback('@rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)@ismU', |
|
- | 125 | function ($x) use ($rgb_function) { |
|
- | 126 | $r = $x[1]; |
|
- | 127 | $g = $x[2]; |
|
- | 128 | $b = $x[3]; |
|
- | 129 | $rgb_function($r,$g,$b); |
|
- | 130 | return "rgb($r,$g,$b)"; |
|
- | 131 | }, $css_content); |
|
- | 132 | $css_content = preg_replace_callback('@rgba\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*([\\d\\.]+)\\s*\\)@ismU', |
|
- | 133 | function ($x) use ($rgb_function) { |
|
- | 134 | $r = $x[1]; |
|
- | 135 | $g = $x[2]; |
|
- | 136 | $b = $x[3]; |
|
- | 137 | $a = $x[4]; |
|
- | 138 | $rgb_function($r,$g,$b); |
|
- | 139 | return "rgba($r,$g,$b,$a)"; |
|
- | 140 | }, $css_content); |
|
- | 141 | $css_content = preg_replace_callback('@\\-rgb:\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*;@ismU', |
|
- | 142 | // Bootstrap uses "--bs-link-color-rgb: 13,110,253;" which we must also accept |
|
- | 143 | function ($x) use ($rgb_function) { |
|
- | 144 | $r = $x[1]; |
|
- | 145 | $g = $x[2]; |
|
- | 146 | $b = $x[3]; |
|
- | 147 | $rgb_function($r,$g,$b); |
|
- | 148 | return "-rgb:$r,$g,$b;"; |
|
- | 149 | }, $css_content); |
|
- | 150 | $css_content = str_replace(chr(1), '#', $css_content); |
|
- | 151 | return $css_content; |
|
- | 152 | } |
|
- | 153 | ||
- | 154 | 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) { |
|
122 | list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b); |
156 | list ($h,$s,$v) = RGB_TO_HSV($r, $g, $b); |
123 | $h = (float)$h; |
157 | $h = (float)$h; |
124 | $s = (float)$s; |
158 | $s = (float)$s; |
125 | $v = (float)$v; |
159 | $v = (float)$v; |
126 | $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1; |
160 | $h = ($h + $h_shift); while ($h > 1) $h -= 1; while ($h < 0) $h += 1; |
127 | $s = ($s + $s_shift); while ($s > 1) $s = 1; while ($s < 0) $s = 0; |
161 | $s = ($s + $s_shift); while ($s > 1) $s = 1; while ($s < 0) $s = 0; |
128 | $v = ($v + $v_shift); while ($v > 1) $v = 1; while ($v < 0) $v = 0; |
162 | $v = ($v + $v_shift); while ($v > 1) $v = 1; while ($v < 0) $v = 0; |
129 | list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v); |
163 | list ($r,$g,$b) = HSV_TO_RGB($h, $s, $v); |
130 | return rgb2html($r,$g,$b); |
- | |
131 | }, $css_content); |
164 | }; |
132 | $css_content = str_replace(chr(1), '#', $css_content); |
165 | return changeCSSWithRgbFunction($css_content, $rgb_function); |
133 | return $css_content; |
- | |
134 | } |
166 | } |
135 | 167 | ||
136 | function invertColorsOfCSS($css_content) { |
168 | function invertColorsOfCSS($css_content) { |
137 | // TODO: also support rgb() and rgba() color references (and maybe also hsl/hsla and color names?) |
- | |
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); |
- | |
141 | $css_content = preg_replace_callback('@#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})@ismU', |
- | |
142 | function ($x) { |
169 | $rgb_function = function(&$r,&$g,&$b) { |
143 | if (strlen($x[1]) == 3) { |
- | |
144 | $r = hexdec($x[1][0].$x[1][0]); |
- | |
145 | $g = hexdec($x[1][1].$x[1][1]); |
- | |
146 | $b = hexdec($x[1][2].$x[1][2]); |
- | |
147 | } else { |
- | |
148 | $r = hexdec($x[1][0].$x[1][1]); |
- | |
149 | $g = hexdec($x[1][2].$x[1][3]); |
- | |
150 | $b = hexdec($x[1][4].$x[1][5]); |
- | |
151 | } |
- | |
152 | $r = 255 - $r; |
170 | $r = 255 - $r; |
153 | $g = 255 - $g; |
171 | $g = 255 - $g; |
154 | $b = 255 - $b; |
172 | $b = 255 - $b; |
155 | return rgb2html($r,$g,$b); |
- | |
156 | }, $css_content); |
173 | }; |
157 | $css_content = str_replace(chr(1), '#', $css_content); |
174 | return changeCSSWithRgbFunction($css_content, $rgb_function); |
158 | return $css_content; |
- | |
159 | } |
175 | } |