Last active
January 4, 2019 06:37
-
-
Save lukecarbis/28d70ece69ee97adfbf769011301a573 to your computer and use it in GitHub Desktop.
Colour Picker template for Block Lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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