Make WordPress Core

Ticket #25580: 25580.2.diff

File 25580.2.diff, 6.3 KB (added by celloexpressions, 13 years ago)

Refresh of 25880.diff, with updated naming conventions that are referable to the color rather than its current uses.

  • wp-content/themes/twentyfourteen/inc/customizer.php

     
    2222        // Add the custom accent color setting and control.
    2323        $wp_customize->add_setting( 'accent_color', array(
    2424                'default'           => '#24890d',
    25                 'sanitize_callback' => 'twentyfourteen_generate_accent_colors',
     25                'sanitize_callback' => 'sanitize_hex_color',
    2626        ) );
    2727
    2828        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'accent_color', array(
     
    3030                'section'  => 'colors',
    3131                'settings' => 'accent_color',
    3232        ) ) );
     33
     34        add_filter( 'theme_mod_accent_mid',  'twentyfourteen_accent_mid'  );
     35        add_filter( 'theme_mod_accent_light', 'twentyfourteen_accent_light' );
    3336
    3437        // Add the featured content section.
    3538        $wp_customize->add_section( 'featured_content', array(
     
    6770add_action( 'customize_preview_init', 'twentyfourteen_customize_preview_js' );
    6871
    6972/**
    70  * Generate two variants of the accent color, return the original, and
    71  * save the others as theme mods.
    72  *
    73  * @since Twenty Fourteen 1.0
    74  *
    75  * @param string $color The original color.
    76  * @return string $color The original color, sanitized.
    77  */
    78 function twentyfourteen_generate_accent_colors( $color ) {
    79         $color = sanitize_hex_color( $color );
    80 
    81         set_theme_mod( 'accent_lighter', twentyfourteen_adjust_color( $color, 29 ) );
    82         set_theme_mod( 'accent_much_lighter', twentyfourteen_adjust_color( $color, 49 ) );
    83 
    84         return $color;
    85 }
    86 
    87 /**
    8873 * Tweak the brightness of a color by adjusting the RGB values by the given interval.
    8974 *
    9075 * Use positive values of $steps to brighten the color and negative values to darken the color.
    (this hunk was shorter than expected) 
    10691        // Convert hex to rgb.
    10792        $rgb = array( hexdec( substr( $color, 1, 2 ) ), hexdec( substr( $color, 3, 2 ) ), hexdec( substr( $color, 5, 2 ) ) );
    10893
    109         // Adjust color and switch back to hex.
     94        // Adjust color and switch back to 6-digit hex.
    11095        $hex = '#';
    111         foreach ( $rgb as $c ) {
    112                 $c += $steps;
    113                 if ( $c > 255 )
    114                         $c = 255;
    115                 elseif ( $c < 0 )
    116                         $c = 0;
    117                 $hex .= str_pad( dechex( $c ), 2, '0', STR_PAD_LEFT);
     96        foreach ( $rgb as $value ) {
     97                $value += $steps;
     98                if ( $value > 255 )
     99                        $value = 255;
     100                elseif ( $value < 0 )
     101                        $value = 0;
     102                $hex .= str_pad( dechex( $value ), 2, '0', STR_PAD_LEFT);
    118103        }
    119104
    120105        return $hex;
    121106}
    122107
     108 /**
     109 * Returns a slightly lighter color than what is set as the theme's
     110 * accent color.
     111 *
     112 * @since Twenty Fourteen 1.0
     113 *
     114 * @return string
     115 */
     116function twentyfourteen_accent_mid() {
     117        return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 29 );
     118}
     119
    123120/**
     121 * Returns a lighter color than what is set as the theme's accent color.
     122 *
     123 * @since Twenty Fourteen 1.0
     124 *
     125 * @return string
     126 */
     127function twentyfourteen_accent_light() {
     128        return twentyfourteen_adjust_color( get_theme_mod( 'accent_color' ), 49 );
     129}
     130
     131/**
     132 * Caches the generated variants of the theme's accent color.
     133 *
     134 * @since Twenty Fourteen 1.0
     135 *
     136 * @return void
     137 */
     138function twentyfourteen_rebuild_accent_colors() {
     139        set_theme_mod( 'accent_mid',  twentyfourteen_accent_mid()  );
     140        set_theme_mod( 'accent_light', twentyfourteen_accent_light() );
     141}
     142add_action( 'update_option_theme_mods_twentyfourteen', 'twentyfourteen_rebuild_accent_colors' );
     143
     144/**
    124145 * Output the CSS for the Theme Customizer options.
    125146 *
    126147 * @since Twenty Fourteen 1.0
     
    128151 * @return void
    129152 */
    130153function twentyfourteen_customizer_styles() {
    131         $accent_color = get_theme_mod( 'accent_color' );
     154        $accent_color = get_theme_mod( 'accent_color', '#24890d' );
    132155
    133156        // Don't do anything if the current color is the default.
    134157        if ( '#24890d' === $accent_color )
    135158                return;
    136159
    137         $accent_lighter = get_theme_mod( 'accent_lighter' );
    138         $accent_much_lighter = get_theme_mod( 'accent_much_lighter' );
     160        $accent_mid = get_theme_mod( 'accent_mid' );
     161        $accent_light = get_theme_mod( 'accent_light' );
    139162
    140163        $css = '/* Custom accent color. */
    141164                a,
     
    192215                        }
    193216                }
    194217
    195                 /* Generated variant of custom accent color: slightly lighter. */
     218                /* Generated "mid" variant of custom accent color. */
    196219                button:hover,
    197220                button:focus,
    198221                .contributor-posts-link:hover,
     
    220243                .content-sidebar .widget input[type="submit"]:hover,
    221244                .content-sidebar .widget input[type="submit"]:focus,
    222245                .slider-control-paging a:hover:before {
    223                         background-color: ' . $accent_lighter . ';
     246                        background-color: ' . $accent_mid . ';
    224247                }
    225248
    226249                a:active,
     
    245268                .content-sidebar .widget_twentyfourteen_ephemera .entry-meta a:hover,
    246269                .site-info a:hover,
    247270                .featured-content a:hover {
    248                         color: ' . $accent_lighter . ';
     271                        color: ' . $accent_mid . ';
    249272                }
    250273
    251274                .page-links a:hover,
    252275                .paging-navigation a:hover {
    253                         border-color: ' . $accent_lighter . ';
     276                        border-color: ' . $accent_mid . ';
    254277                }
    255278
    256279                .tag-links a:hover:before {
    257                         border-right-color: ' . $accent_lighter . ';
     280                        border-right-color: ' . $accent_mid . ';
    258281                }
    259282
    260283                @media screen and (min-width: 782px) {
    261284                        .primary-navigation ul ul a:hover,
    262285                        .primary-navigation ul ul li.focus > a {
    263                                 background-color: ' . $accent_lighter . ';
     286                                background-color: ' . $accent_mid . ';
    264287                        }
    265288                }
    266289
     
    267290                @media screen and (min-width: 1008px) {
    268291                        .secondary-navigation ul ul a:hover,
    269292                        .secondary-navigation ul ul li.focus > a {
    270                                 background-color: ' . $accent_lighter . ';
     293                                background-color: ' . $accent_mid . ';
    271294                        }
    272295                }
    273296
    274                 /* Generated variant of custom accent color: much lighter. */
     297                /* Generated "light" variant of custom accent color. */
    275298                button:active,
    276299                .contributor-posts-link:active,
    277300                input[type="button"]:active,
     
    283306                .content-sidebar .widget input[type="button"]:active,
    284307                .content-sidebar .widget input[type="reset"]:active,
    285308                .content-sidebar .widget input[type="submit"]:active {
    286                         background-color: ' . $accent_much_lighter . ';
     309                        background-color: ' . $accent_light . ';
    287310                }
    288311
    289312                .site-navigation .current_page_item > a,
     
    290313                .site-navigation .current_page_ancestor > a,
    291314                .site-navigation .current-menu-item > a,
    292315                .site-navigation .current-menu-ancestor > a {
    293                         color: ' . $accent_much_lighter . ';
     316                        color: ' . $accent_light . ';
    294317                }';
    295318
    296319