Skip to content

Instantly share code, notes, and snippets.

@WPDevHQ
Last active November 27, 2016 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WPDevHQ/baede2ca3cfd3a1b6ee0f089fa818d8a to your computer and use it in GitHub Desktop.
Save WPDevHQ/baede2ca3cfd3a1b6ee0f089fa818d8a to your computer and use it in GitHub Desktop.
Custom color calculator based on the selected background color
<?php
/**
* Actions.
*
* This file adds the required CSS to the front end to the Actions Pro Theme.
*
* @package Actions
* @author WPDevHQ
* @license GPL-2.0+
* @link http://www.wpdevhq.com/
*/
/**
* Checks the settings for the link color, actions primary, actions secondary and button colors.
* If any of these value are set the appropriate CSS is output.
*
* @since 1.0.0
*/
function actions_pro_css() {
if ( wp_get_theme()->get('Name') != 'Actions Pro' ) {
$theme_style = 'actions-child-style';
} else {
$theme_style = 'actions-style';
}
$handle = $theme_style;
$color_link = get_theme_mod( 'actions_pro_link_color', actions_pro_customizer_get_default_link_color() );
$color_link_hover = get_theme_mod( 'actions_pro_link_hover_color', actions_pro_customizer_get_default_link_hover_color() );
$color_accent_pri = get_theme_mod( 'actions_pro_accent_primary', actions_pro_customizer_get_default_accent_primary() );
$color_accent_sec = get_theme_mod( 'actions_pro_accent_secondary', actions_pro_customizer_get_default_accent_secondary() );
$color_button_pri = get_theme_mod( 'actions_pro_button_primary', actions_pro_customizer_get_default_button_primary() );
$color_button_sec = get_theme_mod( 'actions_pro_button_secondary', actions_pro_customizer_get_default_button_secondary() );
$css = '';
//* Calculate Color Contrast
function actions_color_contrast( $color ) {
$hexcolor = str_replace( '#', '', $color );
$red = hexdec( substr( $hexcolor, 0, 2 ) );
$green = hexdec( substr( $hexcolor, 2, 2 ) );
$blue = hexdec( substr( $hexcolor, 4, 2 ) );
$luminosity = ( ( $red * 0.2126 ) + ( $green * 0.7152 ) + ( $blue * 0.0722 ) );
return ( $luminosity > 128 ) ? '#333333' : '#ffffff';
}
//* Calculate Color Brightness
function actions_color_brightness( $color, $change ) {
$hexcolor = str_replace( '#', '', $color );
$red = hexdec( substr( $hexcolor, 0, 2 ) );
$green = hexdec( substr( $hexcolor, 2, 2 ) );
$blue = hexdec( substr( $hexcolor, 4, 2 ) );
$red = max( 0, min( 255, $red + $change ) );
$green = max( 0, min( 255, $green + $change ) );
$blue = max( 0, min( 255, $blue + $change ) );
return '#'.dechex( $red ).dechex( $green ).dechex( $blue );
}
$css .= ( actions_pro_customizer_get_default_link_color() !== $color_link ) ? sprintf( 'a,.site-content a{color: %s;}', $color_link ) : '';
$css .= ( actions_pro_customizer_get_default_link_hover_color() !== $color_link_hover ) ? sprintf( 'a:hover,.site-content a:hover{color: %s;}', $color_link_hover ) : '';
$css .= ( actions_pro_customizer_get_default_accent_primary() !== $color_accent_pri ) ? sprintf( '
.site-header,.site-title a,.footer-area,.site-footer a,.archive-pagination li a:focus,
.archive-pagination li a:hover,.site-title a:hover,.archive-pagination .active a {background-color: %s;color: %s;}
', $color_accent_pri, actions_color_contrast( $color_accent_pri ) ) : '';
$css .= ( actions_pro_customizer_get_default_accent_secondary() !== $color_accent_sec ) ? sprintf( '
.site-header-menu,.main-navigation ul ul li,.site-header-menu a,.main-navigation li:hover > a,.main-navigation li.focus > a,
.main-navigation .menu-item-has-children > a::after,.dropdown-toggle,.dropdown-toggle:hover,.dropdown-toggle:focus {background-color: %s; color: %s;}
', $color_accent_sec, actions_color_contrast( $color_accent_sec ) ) : '';
$css .= ( actions_pro_customizer_get_default_accent_secondary() !== $color_accent_sec ) ? sprintf( '
.site-header-menu,.main-navigation .primary-menu,.main-navigation ul ul,.main-navigation li,.main-navigation ul ul li{border-color: %s;}', $color_accent_sec ) : '';
$css .= ( actions_pro_customizer_get_default_button_primary() !== $color_button_pri ) ? sprintf( '
input[type="submit"],input[type="button"],.header-elements .widget-area .button,.header-elements .widget-area a.button.small,
.header-elements .widget-area button.small,.enews-widget input[type="submit"],.comments-area input[type="submit"]{background-color: %s; color: %s;}
', $color_button_pri, actions_color_contrast( $color_button_pri ) ) : '';
$css .= ( actions_pro_customizer_get_default_button_secondary() !== $color_button_sec ) ? sprintf( '
input[type="submit"]:hover,input[type="submit"]:focus,input[type="button"]:hover,input[type="button"]:focus,.header-elements .widget-area .button:hover,
.header-elements .widget-area a.button.small:hover,.header-elements .widget-area button.small:hover,.header-elements .widget-area .button:focus,
.header-elements .widget-area a.button.small:forcus,.header-elements .widget-area button.small:forcus,#secondary.enews-widget input:hover[type="submit"],
.enews-widget input:focus[type="submit"],.comments-area input[type="submit"]:hover{background-color: %s; color: %s;}
', $color_button_sec, actions_color_contrast( $color_button_sec ) ) : '';
wp_add_inline_style( $handle, $css );
}
add_action( 'wp_enqueue_scripts', 'actions_pro_css', 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment