forked from Wavyzz/dolibarr
Add color lib for color automation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user