Changeset 49247 for trunk/src/wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-custom-colors.php
- Timestamp:
- 10/20/2020 06:24:33 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-content/themes/twentytwentyone/classes/class-twenty-twenty-one-custom-colors.php
r49216 r49247 33 33 34 34 /** 35 * Determine the luminance of the given color and then return #fff or #000 so that ourtext is always readable.35 * Determine the luminance of the given color and then return #fff or #000 so that the text is always readable. 36 36 * 37 37 * @access public … … 44 44 */ 45 45 public function custom_get_readable_color( $background_color ) { 46 return ( 127 < $this->get_relative_luminance_from_hex( $background_color ) ) ? '#000' : '#fff';46 return ( 127 < self::get_relative_luminance_from_hex( $background_color ) ) ? '#000' : '#fff'; 47 47 } 48 48 … … 116 116 (string) filemtime( get_theme_file_path( 'assets/css/custom-color-overrides.css' ) ) 117 117 ); 118 if ( 'd1e4dd' !== strtolower( get_theme_mod( 'background_color', 'D1E4DD' ) ) ) { 118 119 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 120 if ( 'd1e4dd' !== strtolower( $background_color ) ) { 119 121 wp_add_inline_style( 'twenty-twenty-one-custom-color-overrides', $this->generate_custom_color_variables( 'editor' ) ); 122 } 123 124 $should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', true ); // @phpstan-ignore-line. Passing true instead of default value of false to get_theme_mod. 125 if ( $should_respect_color_scheme && self::get_relative_luminance_from_hex( $background_color ) > 127 ) { 126 // Add dark mode variable overrides. 127 wp_add_inline_style( 'twenty-twenty-one-custom-color-overrides', '@media (prefers-color-scheme: dark) { :root .editor-styles-wrapper { --global--color-background: var(--global--color-dark-gray); --global--color-primary: var(--global--color-light-gray); --global--color-secondary: var(--global--color-light-gray); } }' ); 120 128 } 121 129 } … … 123 131 /** 124 132 * Get luminance from a HEX color. 133 * 134 * @static 125 135 * 126 136 * @access public … … 132 142 * @return int Returns a number (0-255). 133 143 */ 134 public function get_relative_luminance_from_hex( $hex ) {144 public static function get_relative_luminance_from_hex( $hex ) { 135 145 136 146 // Remove the "#" symbol from the beginning of the color. 137 147 $hex = ltrim( $hex, '#' ); 138 148 139 // Make sure we have 6 digits for the below calculations.149 // Make sure there are 6 digits for the below calculations. 140 150 if ( 3 === strlen( $hex ) ) { 141 151 $hex = substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) . substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) . substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ); … … 165 175 public function body_class( $classes ) { 166 176 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 167 if ( 127 > $this->get_relative_luminance_from_hex( $background_color ) ) {177 if ( 127 > self::get_relative_luminance_from_hex( $background_color ) ) { 168 178 $classes[] = 'is-background-dark'; 169 179 } else { … … 171 181 } 172 182 173 $light_colors_default_palette = array( '#D1E4DD', '#D1DFE4', '#D1D1E4', '#E4D1D1', '#E4DAD1', '#EEEADD', '#FFFFFF' );174 if ( in_array( strtoupper( '#' . ltrim( $background_color, '#' ) ), $light_colors_default_palette, true ) ) {175 $classes[] = 'has-default-light-palette-background';176 }177 178 183 return $classes; 179 184 }
Note: See TracChangeset
for help on using the changeset viewer.