wppaste
WordPress

Twenty_Twenty_One_Custom_Colors

Source
wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-custom-colors.php:13
This class is in charge of color customization via the Customizer.

Compatibility

WordPress
core
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Methods · 7

Source code

class Twenty_Twenty_One_Custom_Colors { 	/**	 * Instantiates the object.	 *	 * @since Twenty Twenty-One 1.0	 */	public function __construct() { 		// Enqueue color variables for customizer & frontend.		add_action( 'wp_enqueue_scripts', array( $this, 'custom_color_variables' ) ); 		// Enqueue color variables for editor.		if ( is_admin() ) {			add_action( 'enqueue_block_assets', array( $this, 'editor_custom_color_variables' ) );		} 		// Add body-class if needed.		add_filter( 'body_class', array( $this, 'body_class' ) );	} 	/**	 * Determines the luminance of the given color and then returns #fff or #000 so that the text is always readable.	 *	 * @since Twenty Twenty-One 1.0	 *	 * @param string $background_color The background color.	 * @return string (hex color)	 */	public function custom_get_readable_color( $background_color ) {		return ( 127 < self::get_relative_luminance_from_hex( $background_color ) ) ? '#000' : '#fff';	} 	/**	 * Generates color variables.	 *	 * Adjusts the color value of the CSS variables depending on the background color theme mod.	 * Both text and link colors needs to be updated.	 * The code below needs to be updated, because the colors are no longer theme mods.	 *	 * @since Twenty Twenty-One 1.0	 *	 * @param string|null $context Can be "editor" or null.	 * @return string	 */	public function generate_custom_color_variables( $context = null ) { 		$theme_css        = 'editor' === $context ? ':root .editor-styles-wrapper{' : ':root{';		$background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 		if ( 'd1e4dd' !== strtolower( $background_color ) ) {			$theme_css .= '--global--color-background: #' . $background_color . ';';			$theme_css .= '--global--color-primary: ' . $this->custom_get_readable_color( $background_color ) . ';';			$theme_css .= '--global--color-secondary: ' . $this->custom_get_readable_color( $background_color ) . ';';			$theme_css .= '--button--color-background: ' . $this->custom_get_readable_color( $background_color ) . ';';			$theme_css .= '--button--color-text-hover: ' . $this->custom_get_readable_color( $background_color ) . ';'; 			if ( '#fff' === $this->custom_get_readable_color( $background_color ) ) {				$theme_css .= '--table--stripes-border-color: rgba(240, 240, 240, 0.15);';				$theme_css .= '--table--stripes-background-color: rgba(240, 240, 240, 0.15);';			}		} 		$theme_css .= '}'; 		return $theme_css;	} 	/**	 * Customizer & frontend custom color variables.	 *	 * @since Twenty Twenty-One 1.0	 *	 * @return void	 */	public function custom_color_variables() {		if ( 'd1e4dd' !== strtolower( get_theme_mod( 'background_color', 'D1E4DD' ) ) ) {			wp_add_inline_style( 'twenty-twenty-one-style', $this->generate_custom_color_variables() );		}	}

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-custom-colors.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.