OLD | NEW |
---|---|
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // realize we've installed the default theme, we already have an extension | 70 // realize we've installed the default theme, we already have an extension |
71 // unpacked on the filesystem.) | 71 // unpacked on the filesystem.) |
72 const char kDefaultThemeGalleryID[] = "hkacjpbfdknhflllbcmjibkdeoafencn"; | 72 const char kDefaultThemeGalleryID[] = "hkacjpbfdknhflllbcmjibkdeoafencn"; |
73 | 73 |
74 // Wait this many seconds after startup to garbage collect unused themes. | 74 // Wait this many seconds after startup to garbage collect unused themes. |
75 // Removing unused themes is done after a delay because there is no | 75 // Removing unused themes is done after a delay because there is no |
76 // reason to do it at startup. | 76 // reason to do it at startup. |
77 // ExtensionService::GarbageCollectExtensions() does something similar. | 77 // ExtensionService::GarbageCollectExtensions() does something similar. |
78 const int kRemoveUnusedThemesStartupDelay = 30; | 78 const int kRemoveUnusedThemesStartupDelay = 30; |
79 | 79 |
80 | |
Evan Stade
2016/03/15 19:05:06
intentional?
Peter Kasting
2016/03/16 03:12:59
No.
| |
80 SkColor IncreaseLightness(SkColor color, double percent) { | 81 SkColor IncreaseLightness(SkColor color, double percent) { |
81 color_utils::HSL result; | 82 color_utils::HSL result; |
82 color_utils::SkColorToHSL(color, &result); | 83 color_utils::SkColorToHSL(color, &result); |
83 result.l += (1 - result.l) * percent; | 84 result.l += (1 - result.l) * percent; |
84 return color_utils::HSLToSkColor(result, SkColorGetA(color)); | 85 return color_utils::HSLToSkColor(result, SkColorGetA(color)); |
85 } | 86 } |
86 | 87 |
87 // Writes the theme pack to disk on a separate thread. | 88 // Writes the theme pack to disk on a separate thread. |
88 void WritePackToDiskCallback(BrowserThemePack* pack, | 89 void WritePackToDiskCallback(BrowserThemePack* pack, |
89 const base::FilePath& path) { | 90 const base::FilePath& path) { |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 switch (id) { | 419 switch (id) { |
419 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON: | 420 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON: |
420 return color_utils::HSLShift( | 421 return color_utils::HSLShift( |
421 gfx::kChromeIconGrey, | 422 gfx::kChromeIconGrey, |
422 GetTint(ThemeProperties::TINT_BUTTONS, incognito)); | 423 GetTint(ThemeProperties::TINT_BUTTONS, incognito)); |
423 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON_INACTIVE: | 424 case ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON_INACTIVE: |
424 // The active color is overridden in Gtk2UI. | 425 // The active color is overridden in Gtk2UI. |
425 return SkColorSetA( | 426 return SkColorSetA( |
426 GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON, incognito), | 427 GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON, incognito), |
427 0x33); | 428 0x33); |
429 case ThemeProperties::COLOR_TOOLBAR_TOP_SEPARATOR: { | |
430 const SkColor tab_color = | |
431 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito); | |
432 const SkColor frame_color = | |
433 GetColor(ThemeProperties::COLOR_FRAME, incognito); | |
Evan Stade
2016/03/17 01:07:59
note that https://codereview.chromium.org/17983230
Peter Kasting
2016/03/17 03:31:02
Yeah, it's fine.
| |
434 const SeparatorColorKey key(tab_color, frame_color); | |
435 auto i = separator_color_cache_.find(key); | |
436 if (i != separator_color_cache_.end()) | |
437 return i->second; | |
438 const SkColor separator_color = GetSeparatorColor(tab_color, frame_color); | |
439 separator_color_cache_[key] = separator_color; | |
440 return separator_color; | |
441 } | |
428 case ThemeProperties::COLOR_BACKGROUND_TAB: { | 442 case ThemeProperties::COLOR_BACKGROUND_TAB: { |
429 // The tints here serve a different purpose than TINT_BACKGROUND_TAB. | 443 // 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 | 444 // 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 | 445 // lightening the frame images. The tints here create solid colors for |
432 // background tabs by darkening the foreground tab (toolbar) color. | 446 // background tabs by darkening the foreground tab (toolbar) color. These |
433 const color_utils::HSL kTint = {-1, -1, 0.4296875}; | 447 // values are chosen to turn the default normal and incognito MD frame |
448 // colors (0xf2f2f2 and 0x505050) into 0xd0d0d0 and 0x373737, | |
449 // respectively. | |
450 const color_utils::HSL kTint = {-1, -1, 0.42975}; | |
434 const color_utils::HSL kTintIncognito = {-1, -1, 0.34375}; | 451 const color_utils::HSL kTintIncognito = {-1, -1, 0.34375}; |
435 return color_utils::HSLShift( | 452 return color_utils::HSLShift( |
436 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito), | 453 GetColor(ThemeProperties::COLOR_TOOLBAR, incognito), |
437 incognito ? kTintIncognito : kTint); | 454 incognito ? kTintIncognito : kTint); |
438 } | 455 } |
439 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_BACKGROUND: | 456 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_BACKGROUND: |
440 if (UsingDefaultTheme()) | 457 if (UsingDefaultTheme()) |
441 break; | 458 break; |
442 return GetColor(ThemeProperties::COLOR_TOOLBAR, incognito); | 459 return GetColor(ThemeProperties::COLOR_TOOLBAR, incognito); |
443 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_SEPARATOR: | 460 case ThemeProperties::COLOR_DETACHED_BOOKMARK_BAR_SEPARATOR: |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 | 601 |
585 bool ThemeService::ShouldUseNativeFrame() const { | 602 bool ThemeService::ShouldUseNativeFrame() const { |
586 return false; | 603 return false; |
587 } | 604 } |
588 | 605 |
589 bool ThemeService::HasCustomImage(int id) const { | 606 bool ThemeService::HasCustomImage(int id) const { |
590 return BrowserThemePack::IsPersistentImageID(id) && theme_supplier_ && | 607 return BrowserThemePack::IsPersistentImageID(id) && theme_supplier_ && |
591 theme_supplier_->HasCustomImage(id); | 608 theme_supplier_->HasCustomImage(id); |
592 } | 609 } |
593 | 610 |
611 // static | |
612 SkColor ThemeService::GetSeparatorColor(SkColor tab_color, | |
613 SkColor frame_color) { | |
614 // We use this alpha value for the separator if possible. | |
615 const SkAlpha kAlpha = 0x40; | |
616 | |
617 // In most cases, if the tab is lighter than the frame, we darken the | |
618 // frame; if the tab is darker than the frame, we lighten the frame. | |
619 // However, if the frame is already very dark or very light, respectively, | |
620 // this won't contrast sufficiently with the frame color, so we'll need to | |
621 // reverse when we're lightening and darkening. | |
622 const double tab_luminance = color_utils::GetRelativeLuminance(tab_color); | |
623 const double frame_luminance = color_utils::GetRelativeLuminance(frame_color); | |
624 const bool lighten = tab_luminance < frame_luminance; | |
625 SkColor separator_color = lighten ? SK_ColorWHITE : SK_ColorBLACK; | |
626 double separator_luminance = color_utils::GetRelativeLuminance( | |
627 color_utils::AlphaBlend(separator_color, frame_color, kAlpha)); | |
628 // The minimum contrast ratio here is just under the ~1.1469 in the default MD | |
629 // incognito theme. We want the separator to still darken the frame in that | |
630 // theme, but that's about as low of contrast as we're willing to accept. | |
631 const double kMinContrastRatio = 1.1465; | |
632 if (color_utils::GetContrastRatio(separator_luminance, frame_luminance) >= | |
633 kMinContrastRatio) | |
634 return SkColorSetA(separator_color, kAlpha); | |
635 | |
636 // We need to reverse whether we're darkening or lightening. We know the new | |
637 // separator color will contrast with the frame; check whether it also | |
638 // contrasts at least as well with the tab. | |
639 separator_color = color_utils::InvertColor(separator_color); | |
640 separator_luminance = color_utils::GetRelativeLuminance( | |
641 color_utils::AlphaBlend(separator_color, frame_color, kAlpha)); | |
642 if (color_utils::GetContrastRatio(separator_luminance, tab_luminance) >= | |
643 color_utils::GetContrastRatio(separator_luminance, frame_luminance)) | |
644 return SkColorSetA(separator_color, kAlpha); | |
645 | |
646 // The reversed separator doesn't contrast enough with the tab. Compute the | |
647 // resulting luminance from adjusting the tab color, instead of the frame | |
648 // color, by the separator color. | |
649 const double target_luminance = color_utils::GetRelativeLuminance( | |
650 color_utils::AlphaBlend(separator_color, tab_color, kAlpha)); | |
651 | |
652 // Now try to compute an alpha for the separator such that, when blended with | |
653 // the frame, it results in the above luminance. Because the luminance | |
654 // computation is not easily invertible, we use a binary search over the | |
655 // possible range of alpha values. | |
656 SkAlpha alpha = 128; | |
657 for (int delta = lighten ? 64 : -64; delta != 0; delta /= 2) { | |
658 const double luminance = color_utils::GetRelativeLuminance( | |
659 color_utils::AlphaBlend(separator_color, frame_color, alpha)); | |
660 if (luminance == target_luminance) | |
661 break; | |
662 alpha += (luminance < target_luminance) ? -delta : delta; | |
663 } | |
664 return SkColorSetA(separator_color, alpha); | |
665 } | |
666 | |
594 gfx::ImageSkia* ThemeService::GetImageSkiaNamed(int id, bool incognito) const { | 667 gfx::ImageSkia* ThemeService::GetImageSkiaNamed(int id, bool incognito) const { |
595 gfx::Image image = GetImageNamed(id, incognito); | 668 gfx::Image image = GetImageNamed(id, incognito); |
596 if (image.IsEmpty()) | 669 if (image.IsEmpty()) |
597 return nullptr; | 670 return nullptr; |
598 // TODO(pkotwicz): Remove this const cast. The gfx::Image interface returns | 671 // TODO(pkotwicz): Remove this const cast. The gfx::Image interface returns |
599 // its images const. GetImageSkiaNamed() also should but has many callsites. | 672 // its images const. GetImageSkiaNamed() also should but has many callsites. |
600 return const_cast<gfx::ImageSkia*>(image.ToImageSkia()); | 673 return const_cast<gfx::ImageSkia*>(image.ToImageSkia()); |
601 } | 674 } |
602 | 675 |
603 SkColor ThemeService::GetColor(int id, bool incognito) const { | 676 SkColor ThemeService::GetColor(int id, bool incognito) const { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
779 | 852 |
780 #if defined(ENABLE_SUPERVISED_USERS) | 853 #if defined(ENABLE_SUPERVISED_USERS) |
781 bool ThemeService::IsSupervisedUser() const { | 854 bool ThemeService::IsSupervisedUser() const { |
782 return profile_->IsSupervised(); | 855 return profile_->IsSupervised(); |
783 } | 856 } |
784 | 857 |
785 void ThemeService::SetSupervisedUserTheme() { | 858 void ThemeService::SetSupervisedUserTheme() { |
786 SetCustomDefaultTheme(new SupervisedUserTheme); | 859 SetCustomDefaultTheme(new SupervisedUserTheme); |
787 } | 860 } |
788 #endif | 861 #endif |
OLD | NEW |