浏览代码

Functions: Mods to support color manipulations for waveform generation.

fusionate 1 年之前
父节点
当前提交
5b452b332e
共有 1 个文件被更改,包括 33 次插入3 次删除
  1. 33 3
      resources/functions.php

+ 33 - 3
resources/functions.php

@@ -1135,29 +1135,59 @@
 //function to convert hexidecimal color value to rgb string/array value
 //function to convert hexidecimal color value to rgb string/array value
 	if (!function_exists('hex_to_rgb')) {
 	if (!function_exists('hex_to_rgb')) {
 
 
-		function hex_to_rgb($hex, $delim = '') {
+		function hex_to_rgb($hex, $delim = null, $include_alpha = false, $alpha = 1) {
 			$hex = str_replace("#", "", $hex);
 			$hex = str_replace("#", "", $hex);
 
 
 			if (strlen($hex) == 3) {
 			if (strlen($hex) == 3) {
 				$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
 				$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
 				$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
 				$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
 				$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
 				$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
-			} else {
+			}
+			else {
 				$r = hexdec(substr($hex, 0, 2));
 				$r = hexdec(substr($hex, 0, 2));
 				$g = hexdec(substr($hex, 2, 2));
 				$g = hexdec(substr($hex, 2, 2));
 				$b = hexdec(substr($hex, 4, 2));
 				$b = hexdec(substr($hex, 4, 2));
 			}
 			}
 			$rgb = array($r, $g, $b);
 			$rgb = array($r, $g, $b);
+			if ($include_alpha) { $rgb[] = $alpha; }
 
 
 			if (!empty($delim)) {
 			if (!empty($delim)) {
 				return implode($delim, $rgb); // return rgb delimited string
 				return implode($delim, $rgb); // return rgb delimited string
-			} else {
+			}
+			else {
 				return $rgb; // return array of rgb values
 				return $rgb; // return array of rgb values
 			}
 			}
 		}
 		}
 
 
 	}
 	}
 
 
+//function to convert a hex or rgb/a color to an rgba array
+	if (!function_exists('color_to_rgba_array')) {
+
+		function color_to_rgba_array($string, $alpha = null) {
+			if (!empty($string)) {
+				if (strpos($string, '#') === 0) { //is hex
+					return hex_to_rgb($string, null, true, $alpha);
+				}
+				else if (strpos($string, 'rgb') === 0) { //is rgb/a
+					$string = str_replace(['rgba(','rgb(',')'], '', $string); //values to csv
+					$array = explode(',', $string); //create array
+					if (!empty($array)) {
+						if (@sizeof($array) == 3) { //add alpha
+							$array[] = $alpha ?? 1;
+						}
+						else if (@sizeof($array) == 4 && !empty($alpha)) { //replace alpha
+							$array[3] = $alpha;
+						}
+					}
+					return !empty($array) && is_array($array) ? $array : false;
+				}
+			}
+			return false;
+		}
+
+	}
+
 //function to get a color's luminence level -- dependencies: rgb_to_hsl()
 //function to get a color's luminence level -- dependencies: rgb_to_hsl()
 	if (!function_exists('get_color_luminence')) {
 	if (!function_exists('get_color_luminence')) {