Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/browser/themes/theme_service.cc

Issue 1785613004: Dynamically compute tab/frame separator color. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Exclude test on Mac Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/themes/theme_service.h ('k') | chrome/browser/themes/theme_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/themes/theme_service.h" 5 #include "chrome/browser/themes/theme_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 switch (id) { 418 switch (id) {
419 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON: 419 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON:
420 return color_utils::HSLShift( 420 return color_utils::HSLShift(
421 gfx::kChromeIconGrey, 421 gfx::kChromeIconGrey,
422 GetTint(ThemeProperties::TINT_BUTTONS, incognito)); 422 GetTint(ThemeProperties::TINT_BUTTONS, incognito));
423 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON_INACTIVE: 423 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON_INACTIVE:
424 // The active color is overridden in Gtk2UI. 424 // The active color is overridden in Gtk2UI.
425 return SkColorSetA( 425 return SkColorSetA(
426 GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON, incognito), 426 GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON, incognito),
427 0x33); 427 0x33);
428 case ThemeProperties::COLOR_TOOLBAR_TOP_SEPARATOR: {
429 const SkColor tab_color =
430 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito);
431 const SkColor frame_color =
432 GetColor(ThemeProperties::COLOR_FRAME, incognito);
433 const SeparatorColorKey key(tab_color, frame_color);
434 auto i = separator_color_cache_.find(key);
435 if (i != separator_color_cache_.end())
436 return i->second;
437 const SkColor separator_color = GetSeparatorColor(tab_color, frame_color);
438 separator_color_cache_[key] = separator_color;
439 return separator_color;
440 }
428 case ThemeProperties::COLOR_BACKGROUND_TAB: { 441 case ThemeProperties::COLOR_BACKGROUND_TAB: {
429 // The tints here serve a different purpose than TINT_BACKGROUND_TAB. 442 // The tints here serve a different purpose than TINT_BACKGROUND_TAB.
430 // That tint is used to create background tab images for custom themes by 443 // That tint is used to create background tab images for custom themes by
431 // lightening the frame images. The tints here create solid colors for 444 // lightening the frame images. The tints here create solid colors for
432 // background tabs by darkening the foreground tab (toolbar) color. 445 // background tabs by darkening the foreground tab (toolbar) color. These
433 const color_utils::HSL kTint = {-1, -1, 0.4296875}; 446 // values are chosen to turn the default normal and incognito MD frame
447 // colors (0xf2f2f2 and 0x505050) into 0xd0d0d0 and 0x373737,
448 // respectively.
449 const color_utils::HSL kTint = {-1, -1, 0.42975};
434 const color_utils::HSL kTintIncognito = {-1, -1, 0.34375}; 450 const color_utils::HSL kTintIncognito = {-1, -1, 0.34375};
435 return color_utils::HSLShift( 451 return color_utils::HSLShift(
436 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito), 452 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito),
437 incognito ? kTintIncognito : kTint); 453 incognito ? kTintIncognito : kTint);
438 } 454 }
439 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_BACKGROUND: 455 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_BACKGROUND:
440 if (UsingDefaultTheme()) 456 if (UsingDefaultTheme())
441 break; 457 break;
442 return GetColor(ThemeProperties::COLOR_TOOLBAR, incognito); 458 return GetColor(ThemeProperties::COLOR_TOOLBAR, incognito);
443 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_SEPARATOR: 459 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_SEPARATOR:
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 600
585 bool ThemeService::ShouldUseNativeFrame() const { 601 bool ThemeService::ShouldUseNativeFrame() const {
586 return false; 602 return false;
587 } 603 }
588 604
589 bool ThemeService::HasCustomImage(int id) const { 605 bool ThemeService::HasCustomImage(int id) const {
590 return BrowserThemePack::IsPersistentImageID(id) && theme_supplier_ && 606 return BrowserThemePack::IsPersistentImageID(id) && theme_supplier_ &&
591 theme_supplier_->HasCustomImage(id); 607 theme_supplier_->HasCustomImage(id);
592 } 608 }
593 609
610 // static
611 SkColor ThemeService::GetSeparatorColor(SkColor tab_color,
612 SkColor frame_color) {
613 // We use this alpha value for the separator if possible.
614 const SkAlpha kAlpha = 0x40;
615
616 // In most cases, if the tab is lighter than the frame, we darken the
617 // frame; if the tab is darker than the frame, we lighten the frame.
618 // However, if the frame is already very dark or very light, respectively,
619 // this won't contrast sufficiently with the frame color, so we'll need to
620 // reverse when we're lightening and darkening.
621 const double tab_luminance = color_utils::GetRelativeLuminance(tab_color);
622 const double frame_luminance = color_utils::GetRelativeLuminance(frame_color);
623 const bool lighten = tab_luminance < frame_luminance;
624 SkColor separator_color = lighten ? SK_ColorWHITE : SK_ColorBLACK;
625 double separator_luminance = color_utils::GetRelativeLuminance(
626 color_utils::AlphaBlend(separator_color, frame_color, kAlpha));
627 // The minimum contrast ratio here is just under the ~1.1469 in the default MD
628 // incognito theme. We want the separator to still darken the frame in that
629 // theme, but that's about as low of contrast as we're willing to accept.
630 const double kMinContrastRatio = 1.1465;
631 if (color_utils::GetContrastRatio(separator_luminance, frame_luminance) >=
632 kMinContrastRatio)
633 return SkColorSetA(separator_color, kAlpha);
634
635 // We need to reverse whether we're darkening or lightening. We know the new
636 // separator color will contrast with the frame; check whether it also
637 // contrasts at least as well with the tab.
638 separator_color = color_utils::InvertColor(separator_color);
639 separator_luminance = color_utils::GetRelativeLuminance(
640 color_utils::AlphaBlend(separator_color, frame_color, kAlpha));
641 if (color_utils::GetContrastRatio(separator_luminance, tab_luminance) >=
642 color_utils::GetContrastRatio(separator_luminance, frame_luminance))
643 return SkColorSetA(separator_color, kAlpha);
644
645 // The reversed separator doesn't contrast enough with the tab. Compute the
646 // resulting luminance from adjusting the tab color, instead of the frame
647 // color, by the separator color.
648 const double target_luminance = color_utils::GetRelativeLuminance(
649 color_utils::AlphaBlend(separator_color, tab_color, kAlpha));
650
651 // Now try to compute an alpha for the separator such that, when blended with
652 // the frame, it results in the above luminance. Because the luminance
653 // computation is not easily invertible, we use a binary search over the
654 // possible range of alpha values.
655 SkAlpha alpha = 128;
656 for (int delta = lighten ? 64 : -64; delta != 0; delta /= 2) {
657 const double luminance = color_utils::GetRelativeLuminance(
658 color_utils::AlphaBlend(separator_color, frame_color, alpha));
659 if (luminance == target_luminance)
660 break;
661 alpha += (luminance < target_luminance) ? -delta : delta;
662 }
663 return SkColorSetA(separator_color, alpha);
664 }
665
594 gfx::ImageSkia* ThemeService::GetImageSkiaNamed(int id, bool incognito) const { 666 gfx::ImageSkia* ThemeService::GetImageSkiaNamed(int id, bool incognito) const {
595 gfx::Image image = GetImageNamed(id, incognito); 667 gfx::Image image = GetImageNamed(id, incognito);
596 if (image.IsEmpty()) 668 if (image.IsEmpty())
597 return nullptr; 669 return nullptr;
598 // TODO(pkotwicz): Remove this const cast. The gfx::Image interface returns 670 // TODO(pkotwicz): Remove this const cast. The gfx::Image interface returns
599 // its images const. GetImageSkiaNamed() also should but has many callsites. 671 // its images const. GetImageSkiaNamed() also should but has many callsites.
600 return const_cast<gfx::ImageSkia*>(image.ToImageSkia()); 672 return const_cast<gfx::ImageSkia*>(image.ToImageSkia());
601 } 673 }
602 674
603 SkColor ThemeService::GetColor(int id, bool incognito) const { 675 SkColor ThemeService::GetColor(int id, bool incognito) const {
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 851
780 #if defined(ENABLE_SUPERVISED_USERS) 852 #if defined(ENABLE_SUPERVISED_USERS)
781 bool ThemeService::IsSupervisedUser() const { 853 bool ThemeService::IsSupervisedUser() const {
782 return profile_->IsSupervised(); 854 return profile_->IsSupervised();
783 } 855 }
784 856
785 void ThemeService::SetSupervisedUserTheme() { 857 void ThemeService::SetSupervisedUserTheme() {
786 SetCustomDefaultTheme(new SupervisedUserTheme); 858 SetCustomDefaultTheme(new SupervisedUserTheme);
787 } 859 }
788 #endif 860 #endif
OLDNEW
« no previous file with comments | « chrome/browser/themes/theme_service.h ('k') | chrome/browser/themes/theme_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698