Commit a4ec975d authored by Alex Ilin's avatar Alex Ilin Committed by Chromium LUCI CQ

Match OS theme when picking colors in the local profile creation

When a new profile is created through the local profile creation,
GenerateNewProfileColorWithGenerator() will try to choose a color with
lightness close to the current color mode (either light or dark).

Currently, the second and third line in the color picker will be
considered in light mode, and only the last line will be considered in
dark mode.

Bug: 1169223
Change-Id: If3c55831a8d4e69a51353b4e52f931dab9fdc287
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2642455Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Commit-Queue: Alex Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846109}
parent 44137327
......@@ -15,6 +15,7 @@
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/common/search/generated_colors_info.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
namespace {
......@@ -73,17 +74,17 @@ std::vector<int> GetAvailableColorIndices(
return available_color_indices;
}
base::Optional<double> ExtractCurrentColorLightness(
ProfileAttributesEntry* current_profile) {
if (!current_profile)
return base::nullopt;
base::Optional<ProfileThemeColors> current_colors =
current_profile->GetProfileThemeColorsIfSet();
if (!current_colors)
return base::nullopt;
double ExtractCurrentColorLightness(ProfileAttributesEntry* current_profile) {
ProfileThemeColors current_colors;
if (!current_profile) {
current_colors = GetDefaultProfileThemeColors(
ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors());
} else {
current_colors = current_profile->GetProfileThemeColors();
}
color_utils::HSL hsl;
color_utils::SkColorToHSL(current_colors->profile_highlight_color, &hsl);
color_utils::SkColorToHSL(current_colors.profile_highlight_color, &hsl);
return hsl.l;
}
......@@ -175,7 +176,7 @@ chrome_colors::ColorInfo GenerateNewProfileColorWithGenerator(
used_theme_colors.insert(*current_colors);
}
base::Optional<double> current_color_lightness =
double current_color_lightness =
ExtractCurrentColorLightness(current_profile);
// Collect indices of profile colors that match all the filters.
......
......@@ -20,6 +20,7 @@
#include "components/account_id/account_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
namespace {
......@@ -53,6 +54,14 @@ SkColor GetHighlightColor(size_t index) {
return theme_colors.profile_highlight_color;
}
bool IsColorMatchingColorScheme(SkColor color, bool should_use_dark_colors) {
SkColor default_color = GetDefaultProfileThemeColors(should_use_dark_colors)
.profile_highlight_color;
color_utils::HSL hsl;
color_utils::SkColorToHSL(default_color, &hsl);
return IsLightForAutoselection(color, hsl.l);
}
class ProfileColorsUtilTest : public testing::Test {
public:
ProfileColorsUtilTest()
......@@ -174,9 +183,33 @@ TEST_F(ProfileColorsUtilTest, IsLightForAutoselection) {
EXPECT_FALSE(IsLightForAutoselection(very_dark, very_light_hsl.l));
}
// Test that all colors are available with no other profiles.
TEST_F(ProfileColorsUtilTest, GenerateNewProfileColorWithNoColoredProfile) {
ExpectAllSaturatedColorsAvailable();
class ProfileColorsUtilTestDarkModeParam
: public ProfileColorsUtilTest,
public testing::WithParamInterface<bool> {
public:
void ExpectAllSaturatedColorsMatchingColorSchemeAvailable(
bool should_use_dark_colors) {
std::set<int> available_colors = GetAvailableColorsIds();
for (size_t i = 0; i < kColorsCount; ++i) {
SkColor highlight_color = GetHighlightColor(i);
if (IsSaturatedForAutoselection(highlight_color) &&
IsColorMatchingColorScheme(highlight_color, should_use_dark_colors)) {
EXPECT_TRUE(base::Contains(available_colors, GetColor(i).id));
} else {
EXPECT_FALSE(base::Contains(available_colors, GetColor(i).id));
}
}
}
};
// Test that all colors matching the native light-or-dark color scheme are
// available with no other profiles.
TEST_P(ProfileColorsUtilTestDarkModeParam,
GenerateNewProfileColorWithNoColoredProfile) {
bool should_use_dark_colors = GetParam();
ui::NativeTheme::GetInstanceForNativeUi()->set_use_dark_colors(
should_use_dark_colors);
ExpectAllSaturatedColorsMatchingColorSchemeAvailable(should_use_dark_colors);
// Add some profiles with the default theme.
AddProfile(base::nullopt);
......@@ -185,9 +218,13 @@ TEST_F(ProfileColorsUtilTest, GenerateNewProfileColorWithNoColoredProfile) {
AddProfile(SK_ColorRED);
// It still behaves the same, all colors are available.
ExpectAllSaturatedColorsAvailable();
ExpectAllSaturatedColorsMatchingColorSchemeAvailable(should_use_dark_colors);
}
INSTANTIATE_TEST_SUITE_P(All,
ProfileColorsUtilTestDarkModeParam,
testing::Bool());
// Test that the taken colors are not available.
TEST_F(ProfileColorsUtilTest,
GenerateNewProfileColorWithMultipleColoredProfiles) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment