Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #885 from WordPress/try/white-background-luminance
Browse files Browse the repository at this point in the history
Try: Use luminance of color to check for "white" background
  • Loading branch information
carolinan authored Nov 25, 2020
2 parents 488f3cd + 73286c5 commit 3b9cc7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion assets/js/customize-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
if ( isDark ) {
document.body.classList.add( 'is-dark-theme' );
document.documentElement.classList.add( 'is-dark-theme' );
document.body.classList.remove( 'is-light-theme' );
document.documentElement.classList.remove( 'is-light-theme' );
document.documentElement.classList.remove( 'respect-color-scheme-preference' );
} else {
document.body.classList.remove( 'is-dark-theme' );
document.documentElement.classList.remove( 'is-dark-theme' );
document.body.classList.add( 'is-light-theme' );
document.documentElement.classList.add( 'is-light-theme' );
if ( wp.customize( 'respect_user_color_preference' ).get() ) {
document.documentElement.classList.add( 'respect-color-scheme-preference' );
}
}

// Toggle the white background class.
if ( '#ffffff' === to.toLowerCase() ) {
if ( 225 <= lum ) {
document.body.classList.add( 'has-background-white' );
} else {
document.body.classList.remove( 'has-background-white' );
Expand Down
6 changes: 4 additions & 2 deletions classes/class-twenty-twenty-one-custom-colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ public static function get_relative_luminance_from_hex( $hex ) {
*/
public function body_class( $classes ) {
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
if ( 127 > self::get_relative_luminance_from_hex( $background_color ) ) {
$luminance = self::get_relative_luminance_from_hex( $background_color );

if ( 127 > $luminance ) {
$classes[] = 'is-dark-theme';
} else {
$classes[] = 'is-light-theme';
}

if ( 'ffffff' === strtolower( $background_color ) ) {
if ( 225 <= $luminance ) {
$classes[] = 'has-background-white';
}

Expand Down

0 comments on commit 3b9cc7e

Please sign in to comment.