assets.php
1 month ago
color.php
1 month ago
countries.php
1 month ago
index.html
1 month ago
media.php
1 month ago
site.php
1 month ago
sitescripts.php
1 month ago
status.php
1 month ago
vikappointments.php
1 month ago
color.php
325 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Utility class working with colors. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | abstract class VAPHtmlColor |
| 20 | { |
| 21 | /** |
| 22 | * Converts a RGB color in a HEX string. |
| 23 | * This function accepts up to 3 arguments (r,g,b) or |
| 24 | * an array/object that contains the color attributes. |
| 25 | * |
| 26 | * Usage: |
| 27 | * rgb2hex(128, 128, 128); |
| 28 | * rgb2hex(array('r' => 128, 'g' => 128, 'b' => 128)); |
| 29 | * rgb2hex(array('red' => 128, 'green' => 128, 'blue' => 128)); |
| 30 | * |
| 31 | * @return string The hex color (prefixed with #). |
| 32 | */ |
| 33 | public static function rgb2hex() |
| 34 | { |
| 35 | $args = func_get_args(); |
| 36 | $rgb = array(); |
| 37 | $n = count($args); |
| 38 | |
| 39 | if ($n >= 3) |
| 40 | { |
| 41 | $rgb[] = $args[0]; |
| 42 | $rgb[] = $args[1]; |
| 43 | $rgb[] = $args[2]; |
| 44 | $rgb[] = array_pop($args); // alpha (if specified) |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | // use array notation |
| 49 | $color = (array) array_shift($args); |
| 50 | |
| 51 | // find RED color |
| 52 | if (isset($color['r'])) |
| 53 | { |
| 54 | $rgb[] = $color['r']; |
| 55 | } |
| 56 | else if (isset($color['red'])) |
| 57 | { |
| 58 | $rgb[] = $color['red']; |
| 59 | } |
| 60 | |
| 61 | // find GREEN color |
| 62 | if (isset($color['g'])) |
| 63 | { |
| 64 | $rgb[] = $color['g']; |
| 65 | } |
| 66 | else if (isset($color['green'])) |
| 67 | { |
| 68 | $rgb[] = $color['green']; |
| 69 | } |
| 70 | |
| 71 | // find BLUE color |
| 72 | if (isset($color['b'])) |
| 73 | { |
| 74 | $rgb[] = $color['b']; |
| 75 | } |
| 76 | else if (isset($color['blue'])) |
| 77 | { |
| 78 | $rgb[] = $color['blue']; |
| 79 | } |
| 80 | |
| 81 | // find ALPHA |
| 82 | if (isset($color['a'])) |
| 83 | { |
| 84 | $rgb[] = $color['a']; |
| 85 | } |
| 86 | else if (isset($color['alpha'])) |
| 87 | { |
| 88 | $rgb[] = $color['alpha']; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // check RGB length |
| 93 | if (count($rgb) < 3) |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // convert to HEX |
| 99 | $hex = '#' |
| 100 | . sprintf('%02x', $rgb[0]) |
| 101 | . sprintf('%02x', $rgb[1]) |
| 102 | . sprintf('%02x', $rgb[2]); |
| 103 | |
| 104 | if (count($rgb) == 4) |
| 105 | { |
| 106 | if (strpos($rgb[3], '%') !== false) |
| 107 | { |
| 108 | // percentage value |
| 109 | $rgb[3] = intval($rgb[3]); |
| 110 | $rgb[3] *= 255 / 100; |
| 111 | } |
| 112 | |
| 113 | $hex .= sprintf('%02x', $rgb[3]); |
| 114 | } |
| 115 | |
| 116 | return $hex; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Converts a HEX color in RGB representation. |
| 121 | * |
| 122 | * @param string $hex The HEX color (prefixed or not with #) |
| 123 | * @param boolean $string True to return the RGB string. |
| 124 | * |
| 125 | * @return mixed The RGB object or its string. |
| 126 | */ |
| 127 | public static function hex2rgb($hex, $string = false) |
| 128 | { |
| 129 | // trim # at the beginning, if any |
| 130 | $hex = ltrim($hex, '#'); |
| 131 | $len = strlen($hex); |
| 132 | |
| 133 | // validate HEX color |
| 134 | if (preg_match('/[^0-9a-f]/i', $hex) || !in_array($len, array(3, 4, 6, 8))) |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // when length is 3 or 4, repeat every char twice |
| 140 | if ($len == 3 || $len == 4) |
| 141 | { |
| 142 | $hex = preg_replace('/(.)/', '$1$1', $hex); |
| 143 | } |
| 144 | |
| 145 | if (strlen($hex) == 6) |
| 146 | { |
| 147 | // maximum opacity |
| 148 | $hex .= 'FF'; |
| 149 | } |
| 150 | |
| 151 | $color = new stdClass; |
| 152 | $color->red = (int) hexdec(substr($hex, 0, 2)); |
| 153 | $color->green = (int) hexdec(substr($hex, 2, 2)); |
| 154 | $color->blue = (int) hexdec(substr($hex, 4, 2)); |
| 155 | $color->alpha = (int) hexdec(substr($hex, 6, 2)); |
| 156 | |
| 157 | // calculate alpha percentage |
| 158 | $color->alphaPercent = $color->alpha / 255; |
| 159 | |
| 160 | if ($string) |
| 161 | { |
| 162 | if ($len == 4 || $len == 8) |
| 163 | { |
| 164 | // display RGBA |
| 165 | $color = sprintf('rgba(%d, %d, %d, %.2f)', $color->red, $color->green, $color->blue, $color->alphaPercent); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | // display RGB |
| 170 | $color = sprintf('rgb(%d, %d, %d)', $color->red, $color->green, $color->blue); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return $color; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Checks whether the specified color is light. |
| 179 | * |
| 180 | * @param mixed $color Either a HEX string or a RGB object. |
| 181 | * |
| 182 | * @return boolean True if light, false otherwise. |
| 183 | */ |
| 184 | public static function light($color) |
| 185 | { |
| 186 | if (!$color) |
| 187 | { |
| 188 | throw new InvalidArgumentException('Invalid color received'); |
| 189 | } |
| 190 | |
| 191 | if (is_string($color)) |
| 192 | { |
| 193 | // convert HEX color to RGB |
| 194 | $color = self::hex2rgb($color); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | // always treat color as an object |
| 199 | $color = (object) $color; |
| 200 | } |
| 201 | |
| 202 | // HSP (Highly Sensitive Poo) equation |
| 203 | $hsp = sqrt( |
| 204 | 0.299 * pow($color->red, 2) + |
| 205 | 0.587 * pow($color->green, 2) + |
| 206 | 0.114 * pow($color->blue, 2) |
| 207 | ); |
| 208 | |
| 209 | // using the HSP value, determine whether the color is light or dark |
| 210 | // return $hsp > 127.5; |
| 211 | return $hsp > 160; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Checks whether the specified color is dark. |
| 216 | * |
| 217 | * @param mixed $color Either a HEX string or a RGB object. |
| 218 | * |
| 219 | * @return boolean True if dark, false otherwise. |
| 220 | */ |
| 221 | public static function dark($color) |
| 222 | { |
| 223 | return self::light($color) === false; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Generates a random HEX(A) color. |
| 228 | * |
| 229 | * @param boolean $alpha True to include the opacity for HEXA. |
| 230 | * |
| 231 | * @return string The hex color. |
| 232 | */ |
| 233 | public static function random($alpha = false) |
| 234 | { |
| 235 | if ($alpha) |
| 236 | { |
| 237 | // use alpha |
| 238 | $max = 0xFFFFFFFF; |
| 239 | $pad = 8; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | // do not use alpha |
| 244 | $max = 0xFFFFFF; |
| 245 | $pad = 6; |
| 246 | } |
| 247 | |
| 248 | // generate color |
| 249 | return '#' . strtoupper(str_pad(dechex(mt_rand(0, $max)), $pad, '0', STR_PAD_LEFT)); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Choose a random color from the list of preset colors. |
| 254 | * |
| 255 | * @param boolean $list True to return the presets list, false to obtain |
| 256 | * a random color of the list. |
| 257 | * @param boolean $group False to explode the tonalities. |
| 258 | * |
| 259 | * @return mixed The hex color or an array. |
| 260 | */ |
| 261 | public static function preset($list = false, $group = true) |
| 262 | { |
| 263 | // Define the number of colors per tonality. |
| 264 | // IMPORTANT: change this value in case we are going to |
| 265 | // increase/decrease the number of colors per group. |
| 266 | $pack = 3; |
| 267 | |
| 268 | $preset = array( |
| 269 | // blue |
| 270 | '428BCA', |
| 271 | '0033CC', |
| 272 | '44AD8E', |
| 273 | // green |
| 274 | '5CB85C', |
| 275 | '69D100', |
| 276 | 'A8D695', |
| 277 | // red |
| 278 | 'AD4363', |
| 279 | 'D10069', |
| 280 | 'CC0033', |
| 281 | // orange |
| 282 | 'F0AD4E', |
| 283 | 'D9534F', |
| 284 | 'D1D100', |
| 285 | // purple |
| 286 | 'A295D6', |
| 287 | '5843AD', |
| 288 | '8E44AD', |
| 289 | // grey |
| 290 | '34495E', |
| 291 | '7F8C8D', |
| 292 | 'A0ACB8', |
| 293 | ); |
| 294 | |
| 295 | if (!$group) |
| 296 | { |
| 297 | // By default the colors are grouped under the same tonality. |
| 298 | // When we enter here, we need to ungroup them to always have |
| 299 | // different contiguous colors. |
| 300 | // In example, this one: [r,r,r,g,g,g,b,b,b] becomes: |
| 301 | // [r,g,b,r,g,b,r,g,b]. |
| 302 | $tmp = range(0, count($preset) - 1); |
| 303 | |
| 304 | for ($i = 0; $i < count($preset); $i++) |
| 305 | { |
| 306 | // NEW_INDEX = floor(INDEX / CPT) + (INDEX mod CPT) * COLORS / CPT |
| 307 | // *CPT = colors per tonality |
| 308 | $tmp[floor($i / $pack) + ($i % $pack) * count($preset) / $pack] = $preset[$i]; |
| 309 | } |
| 310 | |
| 311 | // use new preset |
| 312 | $preset = $tmp; |
| 313 | } |
| 314 | |
| 315 | if ($list) |
| 316 | { |
| 317 | // return full preset list |
| 318 | return $preset; |
| 319 | } |
| 320 | |
| 321 | // pick random color |
| 322 | return '#' . $preset[rand(0, count($preset) - 1)]; |
| 323 | } |
| 324 | } |
| 325 |