Skip to content

Instantly share code, notes, and snippets.

@lukecarbis
Last active January 4, 2019 06:37
Show Gist options
  • Save lukecarbis/28d70ece69ee97adfbf769011301a573 to your computer and use it in GitHub Desktop.
Save lukecarbis/28d70ece69ee97adfbf769011301a573 to your computer and use it in GitHub Desktop.
Colour Picker template for Block Lab
<?php
$field = 'colour-picker';
$color = block_value( $field );
if ( '#' === substr( $color, 0, 1 ) ) {
$rgb = str_split( str_replace( '#', '', $color ), 2 );
$r = hexdec( $rgb[0] );
$g = hexdec( $rgb[1] );
$b = hexdec( $rgb[2] );
} elseif ( 'rgb' === substr( $color, 0, 3 ) ) {
$rgb = explode( ',', str_replace( array( 'rgba', '(', ')', ' ' ), '', $color ) );
$r = $rgb[0];
$g = $rgb[1];
$b = $rgb[2];
}
$text_color = '#fff';
if ( ( $r * 0.299 ) + ( $g * 0.587 ) + ( $b * 0.114 ) > 186 ) {
$text_color = '#000';
}
?>
<div style="background: <?php block_field( $field ); ?>; padding: 1em; border: none; box-shadow: inset 0 0 0 5px rgba(0,0,0,0.2); text-align: center;">
<code style="color: <?php echo esc_attr( $text_color ); ?>; text-transform: uppercase; font-size: 1.5em; background: none;">
<?php block_field( $field ); ?>
</code>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment