| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions that can be used in the Customify config for filtering values. |
| 4 |
* |
| 5 |
* Think modifying colors, etc. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Adjust a hex color brightness |
| 10 |
* Allows us to create hover styles for custom link colors |
| 11 |
* |
| 12 |
* Taken from the Storefront theme by Automattic: https://github.com/woocommerce/storefront |
| 13 |
* |
| 14 |
* @param string $hex hex color e.g. #111111. |
| 15 |
* @param integer $steps factor by which to brighten/darken ranging from -255 (darken) to 255 (brighten). |
| 16 |
* @return string brightened/darkened hex color |
| 17 |
*/ |
| 18 |
function pixcloud_adjust_color_brightness( $hex, $steps ) { |
| 19 |
// Steps should be between -255 and 255. Negative = darker, positive = lighter. |
| 20 |
$steps = max( -255, min( 255, $steps ) ); |
| 21 |
|
| 22 |
// Format the hex color string. |
| 23 |
$hex = str_replace( '#', '', $hex ); |
| 24 |
|
| 25 |
if ( 3 == strlen( $hex ) ) { |
| 26 |
$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 ); |
| 27 |
} |
| 28 |
|
| 29 |
// Get decimal values. |
| 30 |
$r = hexdec( substr( $hex, 0, 2 ) ); |
| 31 |
$g = hexdec( substr( $hex, 2, 2 ) ); |
| 32 |
$b = hexdec( substr( $hex, 4, 2 ) ); |
| 33 |
|
| 34 |
// Adjust number of steps and keep it inside 0 to 255. |
| 35 |
$r = max( 0, min( 255, $r + $steps ) ); |
| 36 |
$g = max( 0, min( 255, $g + $steps ) ); |
| 37 |
$b = max( 0, min( 255, $b + $steps ) ); |
| 38 |
|
| 39 |
$r_hex = str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT ); |
| 40 |
$g_hex = str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT ); |
| 41 |
$b_hex = str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT ); |
| 42 |
|
| 43 |
return '#' . $r_hex . $g_hex . $b_hex; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Negate the value. |
| 48 |
* |
| 49 |
* @param int|float $value |
| 50 |
* @return int|float |
| 51 |
*/ |
| 52 |
function pixcloud_negate( $value ) { |
| 53 |
if ( ! is_numeric( $value ) ) { |
| 54 |
return $value; |
| 55 |
} |
| 56 |
|
| 57 |
return - $value; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Ensure that value is at least the $min value. |
| 62 |
* |
| 63 |
* @param int|float $value |
| 64 |
* @param int|float $min |
| 65 |
* @return int|float |
| 66 |
*/ |
| 67 |
function pixcloud_min( $value, $min ) { |
| 68 |
if ( ! is_numeric( $value ) || ! is_numeric( $min ) ) { |
| 69 |
return $value; |
| 70 |
} |
| 71 |
|
| 72 |
if ( $value < $min ) { |
| 73 |
return $min; |
| 74 |
} |
| 75 |
|
| 76 |
return $value; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Ensure that value is at most the $max value. |
| 81 |
* |
| 82 |
* @param int|float $value |
| 83 |
* @param int|float $max |
| 84 |
* @return int|float |
| 85 |
*/ |
| 86 |
function pixcloud_max( $value, $max ) { |
| 87 |
if ( ! is_numeric( $value ) || ! is_numeric( $max ) ) { |
| 88 |
return $value; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $value > $max ) { |
| 92 |
return $max; |
| 93 |
} |
| 94 |
|
| 95 |
return $value; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Ensure that value is between $min and $max. |
| 100 |
* |
| 101 |
* @param int|float $value |
| 102 |
* @param int|float $min |
| 103 |
* @param int|float $max |
| 104 |
* @return int|float |
| 105 |
*/ |
| 106 |
function pixcloud_min_max( $value, $min, $max ) { |
| 107 |
if ( ! is_numeric( $value ) || ! is_numeric( $min ) || ! is_numeric( $max ) ) { |
| 108 |
return $value; |
| 109 |
} |
| 110 |
|
| 111 |
if ( $value < $min ) { |
| 112 |
$value = $min; |
| 113 |
} |
| 114 |
|
| 115 |
if ( $value > $max ) { |
| 116 |
$value = $max; |
| 117 |
} |
| 118 |
|
| 119 |
return $value; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add something to the value. |
| 124 |
* |
| 125 |
* @param int|float $value |
| 126 |
* @param int|float $add |
| 127 |
* @return int|float |
| 128 |
*/ |
| 129 |
function pixcloud_add( $value, $add ) { |
| 130 |
if ( ! is_numeric( $value ) || ! is_numeric( $add ) ) { |
| 131 |
return $value; |
| 132 |
} |
| 133 |
|
| 134 |
return $value + $add; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Substract something from the value. |
| 139 |
* |
| 140 |
* @param int|float $value |
| 141 |
* @param int|float $substract |
| 142 |
* @return int|float |
| 143 |
*/ |
| 144 |
function pixcloud_substract( $value, $substract ) { |
| 145 |
if ( ! is_numeric( $value ) || ! is_numeric( $substract ) ) { |
| 146 |
return $value; |
| 147 |
} |
| 148 |
|
| 149 |
return $value - $substract; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Multiply the value. |
| 154 |
* |
| 155 |
* @param int|float $value |
| 156 |
* @param int|float $multiply |
| 157 |
* @return int|float |
| 158 |
*/ |
| 159 |
function pixcloud_multiply( $value, $multiply ) { |
| 160 |
if ( ! is_numeric( $value ) || ! is_numeric( $multiply ) ) { |
| 161 |
return $value; |
| 162 |
} |
| 163 |
|
| 164 |
return $value * $multiply; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Divide the value. |
| 169 |
* |
| 170 |
* @param int|float $value |
| 171 |
* @param int|float $divide |
| 172 |
* @return int|float |
| 173 |
*/ |
| 174 |
function pixcloud_divide( $value, $divide ) { |
| 175 |
if ( ! is_numeric( $value ) || ! is_numeric( $divide ) || empty( $divide ) ) { |
| 176 |
return $value; |
| 177 |
} |
| 178 |
|
| 179 |
return $value / $divide; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Divide the value and get the remainder. |
| 184 |
* |
| 185 |
* @param int|float $value |
| 186 |
* @param int|float $divide |
| 187 |
* @return int|float |
| 188 |
*/ |
| 189 |
function pixcloud_modulo( $value, $divide ) { |
| 190 |
if ( ! is_numeric( $value ) || ! is_numeric( $divide ) || empty( $divide ) ) { |
| 191 |
return $value; |
| 192 |
} |
| 193 |
|
| 194 |
return $value % $divide; |
| 195 |
} |