'#0267ff', 'color_mentions' => '#0267ff', 'color_sidebar_background' => '#f3f3f3', 'color_sidebar_content' => '#00101c', ]; return $default_colors[ $key ]; } // Whether a color option is set and is a non-default value function is_custom_color( array $options, string $key ): bool { if ( ! isset( $options[ $key ] ) ) { return false; } $color = $options[ $key ]; return get_default_color( $key ) !== $color; } function color_hex_to_rgba( string $hex, float $alpha ): string { if ( '' === $hex ) { $hex = '#000000'; } $rgb = sscanf( $hex, '#%02x%02x%02x' ); $rgb[] = $alpha; return sprintf( 'rgba(%d, %d, %d, %f);', ...$rgb ); } // Return a brightness value from 0 to 255 function get_brightness( string $hex ): int { [$r, $g, $b] = sscanf( $hex, '#%02x%02x%02x' ); return ( $r * 299 + $g * 587 + $b * 114 ) / 1000; } // Generate an appropriate border color for the background color function border_color_for_background( string $bg_hex ): string { $brightness = get_brightness( $bg_hex ); if ( $brightness > 243 ) { $bg_hsl = Color::hexToHsl( $bg_hex ); $bg_hsl['L'] *= 0.97; return color_hex_to_rgba( '#' . Color::hslToHex( $bg_hsl ), 0.6 ); } elseif ( $brightness > 200 ) { return 'rgba( 255, 255, 255, 0.4 )'; } else { return 'rgba( 255, 255, 255, 0.15 )'; } } // Utility function to add a single color setting and control function register_color( \WP_Customize_Manager $wp_customize, string $key, string $label ) { $wp_customize->add_setting( "p2020_theme_options[$key]", [ 'default' => get_default_color( $key ), 'sanitize_callback' => 'sanitize_hex_color', ] ); $wp_customize->add_control( new \WP_Customize_Color_Control( $wp_customize, $key, [ 'label' => $label, 'section' => 'colors', 'settings' => "p2020_theme_options[$key]", ] ) ); } /** * Add color settings to Customizer * * @param \WP_Customize_Manager $wp_customize Theme Customizer object. */ function customize_register_colors( \WP_Customize_Manager $wp_customize ) { register_color( $wp_customize, 'color_link', __( 'Links', 'p2020' ) ); register_color( $wp_customize, 'color_mentions', __( 'Mentions', 'p2020' ) ); register_color( $wp_customize, 'color_sidebar_background', __( 'Sidebar background', 'p2020' ) ); register_color( $wp_customize, 'color_sidebar_content', __( 'Sidebar content', 'p2020' ) ); } add_action( 'customize_register', __NAMESPACE__ . '\customize_register_colors' ); /** * Add styles for color options * * Keep variable names in sync with CSS themes. */ function color_styles() { $options = get_theme_mod( 'p2020_theme_options' ); if ( ! $options ) { return; } // If on an admin page (i.e. looking at the full editor), set a narrower scope than // `.editor-styles-wrapper` so that custom colors can override the colors injected via style-editor.css $scope = is_admin() ? '.block-editor-writing-flow' : ':root'; ?>