* @license http://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/BraadMartin/components/tree/master/customizer/alpha-color-picker
*/
class Popularfx_Customize_Alpha_Color_Control extends Popularfx_Custom_Control {
/**
* The type of control being rendered
*/
public $type = 'alpha-color';
/**
* Add support for palettes to be passed in.
*
* Supported palette values are true, false, or an array of RGBa and Hex colors.
*/
public $palette;
/**
* Add support for showing the opacity value on the slider handle.
*/
public $show_opacity;
/**
* Enqueue our scripts and styles
*/
public function enqueue() {
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'popularfx-customizer-controls' );
}
/**
* Render the control in the customizer
*/
public function render_content() {
// Process the palette
if ( is_array( $this->palette ) ) {
$palette = implode( '|', $this->palette );
} else {
// Default to true.
$palette = ( false === $this->palette || 'false' === $this->palette ) ? 'false' : 'true';
}
// Support passing show_opacity as string or boolean. Default to true.
$show_opacity = ( false === $this->show_opacity || 'false' === $this->show_opacity ) ? 'false' : 'true';
// Output the label and description if they were passed in.
if ( isset( $this->label ) && '' !== $this->label ) {
echo '' . sanitize_text_field( $this->label ) . '';
}
if ( isset( $this->description ) && '' !== $this->description ) {
echo '' . sanitize_text_field( $this->description ) . '';
} ?>
link(); ?> />
default;
}
if ( false === strpos( $input, 'rgba' ) ) {
// If string doesn't start with 'rgba' then santize as hex color
$input = sanitize_hex_color( $input );
} else {
// Sanitize as RGBa color
$input = str_replace( ' ', '', $input );
sscanf( $input, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
$input = 'rgba(' . popularfx_in_range( $red, 0, 255 ) . ',' . popularfx_in_range( $green, 0, 255 ) . ',' . popularfx_in_range( $blue, 0, 255 ) . ',' . popularfx_in_range( $alpha, 0, 1 ) . ')';
}
return $input;
}
}
/**
* Only allow values between a certain minimum & maxmium range
*
* @param number Input to be sanitized
* @return number Sanitized input
*/
if ( ! function_exists( 'popularfx_in_range' ) ) {
function popularfx_in_range( $input, $min, $max ){
if ( $input < $min ) {
$input = $min;
}
if ( $input > $max ) {
$input = $max;
}
return $input;
}
}
}