diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 45a8d5bd5c6..d2e8aab98f9 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -7849,6 +7849,7 @@ function getDictvalue($tablename, $field, $id, $checkentity = false, $rowidfield */ function colorIsLight($stringcolor) { + $stringcolor = str_replace('#', '', $stringcolor); $res = -1; if (!empty($stringcolor)) { diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 7ffc09625a3..61dae6d0a5c 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -2200,6 +2200,105 @@ function colorStringToArray($stringcolor, $colorifnotfound = array(88,88,88)) return array(hexdec($reg[1]),hexdec($reg[2]),hexdec($reg[3])); } +/** + * @param string $color + * @param boolean $allow_white + * @return boolean + */ +function colorValidateHex($color, $allow_white = false) +{ + + if(!$allow_white && ($color === '#fff' || $color === '#ffffff') ) return false; + + if(preg_match('/^#[a-f0-9]{6}$/i', $color)) //hex color is valid + { + return true; + } + return false; +} + + +/** + * @param string $hex + * @param integer $steps + * @return string + */ +function colorAdjustBrightness($hex, $steps) { + // Steps should be between -255 and 255. Negative = darker, positive = lighter + $steps = max(-255, min(255, $steps)); + + // Normalize into a six character long hex string + $hex = str_replace('#', '', $hex); + if (strlen($hex) == 3) { + $hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2); + } + + // Split into three parts: R, G and B + $color_parts = str_split($hex, 2); + $return = '#'; + + foreach ($color_parts as $color) { + $color = hexdec($color); // Convert to decimal + $color = max(0,min(255,$color + $steps)); // Adjust color + $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code + } + + return $return; +} + +/** + * @param string $hex + * @param integer $percent 0 to 100 + * @return string + */ +function colorDarker($hex, $percent) { + $steps = intval(255 * $percent / 100 ) * -1; + return colorAdjustBrightness($hex, $steps); +} + +/** + * @param string $hex + * @param integer $percent 0 to 100 + * @return string + */ +function colorLighten($hex, $percent) { + $steps = intval(255 * $percent / 100 ); + return colorAdjustBrightness($hex, $steps); +} + + +/** + * @param string $hex + * @param float $alpha 0 to 1 + * @param bool $returnArray : + * @return string + */ +function colorHexToRgb($hex, $alpha = false, $returnArray = false) { + $string = ''; + $hex = str_replace('#', '', $hex); + $length = strlen($hex); + $rgb = array(); + $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0)); + $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0)); + $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0)); + if ( $alpha !== false ) { + $rgb['a'] = floatval($alpha); + $string = 'rgba('.implode(',', $rgb ).')'; + } + else{ + $string = 'rgb('.implode(',', $rgb ).')'; + } + + if($returnArray){ + return $rgb; + } + else{ + return $string; + } + +} + + /** * Applies the Cartesian product algorithm to an array * Source: http://stackoverflow.com/a/15973172 diff --git a/htdocs/theme/eldy/_badges.css.php b/htdocs/theme/eldy/_badges.css.php index accd4150bd4..a92790a12dd 100644 --- a/htdocs/theme/eldy/_badges.css.php +++ b/htdocs/theme/eldy/_badges.css.php @@ -1,12 +1,10 @@ +/*