1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/themes/theme_properties.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/themes/browser_theme_pack.h"
11 #include "grit/theme_resources.h"
12 #include "ui/resources/grit/ui_resources.h"
13
14 namespace {
15
16 // ----------------------------------------------------------------------------
17 // Defaults for properties which are stored in the browser theme pack. If you
18 // change these defaults, you must increment the version number in
19 // browser_theme_pack.h
20
21 // Default colors.
22 #if defined(OS_CHROMEOS)
23 // Used for theme fallback colors.
24 const SkColor kDefaultColorFrame = SkColorSetRGB(109, 109, 109);
25 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(176, 176, 176);
26 #else
27 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
28 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
29 #endif // OS_CHROMEOS
30 const SkColor kDefaultColorFrameIncognito = SkColorSetRGB(83, 106, 139);
31 const SkColor kDefaultColorFrameIncognitoInactive =
32 SkColorSetRGB(126, 139, 156);
33 #if defined(OS_MACOSX)
34 const SkColor kDefaultColorToolbar = SkColorSetRGB(230, 230, 230);
35 #else
36 const SkColor kDefaultColorToolbar = SkColorSetRGB(223, 223, 223);
37 #endif
38 const SkColor kDefaultColorTabText = SK_ColorBLACK;
39 #if defined(OS_MACOSX)
40 const SkColor kDefaultColorBackgroundTabText = SK_ColorBLACK;
41 #else
42 const SkColor kDefaultColorBackgroundTabText = SkColorSetRGB(64, 64, 64);
43 #endif
44 const SkColor kDefaultColorBookmarkText = SK_ColorBLACK;
45 #if defined(OS_WIN)
46 const SkColor kDefaultColorNTPBackground =
47 color_utils::GetSysSkColor(COLOR_WINDOW);
48 const SkColor kDefaultColorNTPText =
49 color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
50 const SkColor kDefaultColorNTPLink =
51 color_utils::GetSysSkColor(COLOR_HOTLIGHT);
52 #else
53 // TODO(beng): source from theme provider.
54 const SkColor kDefaultColorNTPBackground = SK_ColorWHITE;
55 const SkColor kDefaultColorNTPText = SK_ColorBLACK;
56 const SkColor kDefaultColorNTPLink = SkColorSetRGB(6, 55, 116);
57 #endif
58 const SkColor kDefaultColorNTPHeader = SkColorSetRGB(150, 150, 150);
59 const SkColor kDefaultColorNTPSection = SkColorSetRGB(229, 229, 229);
60 const SkColor kDefaultColorNTPSectionText = SK_ColorBLACK;
61 const SkColor kDefaultColorNTPSectionLink = SkColorSetRGB(6, 55, 116);
62 const SkColor kDefaultColorButtonBackground = SkColorSetARGB(0, 0, 0, 0);
63
64 // Default tints.
65 const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 };
66 const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 };
67 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f };
68 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f };
69 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f };
70 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 };
71
72 // Default display properties.
73 const int kDefaultDisplayPropertyNTPAlignment =
74 ThemeProperties::ALIGN_CENTER;
75 const int kDefaultDisplayPropertyNTPTiling =
76 ThemeProperties::NO_REPEAT;
77 // By default, we do not use the ntp alternate logo.
78 const int kDefaultDisplayPropertyNTPAlternateLogo = 0;
79
80 // ----------------------------------------------------------------------------
81 // Defaults for properties which are not stored in the browser theme pack.
82
83 const SkColor kDefaultColorControlBackground = SK_ColorWHITE;
84 const SkColor kDefaultColorToolbarSeparator = SkColorSetRGB(170, 170, 171);
85
86 #if defined(OS_MACOSX)
87 const SkColor kDefaultColorToolbarButtonStroke = SkColorSetARGB(75, 81, 81, 81);
88 const SkColor kDefaultColorToolbarButtonStrokeInactive =
89 SkColorSetARGB(75, 99, 99, 99);
90 const SkColor kDefaultColorToolbarBezel = SkColorSetRGB(204, 204, 204);
91 const SkColor kDefaultColorToolbarStroke = SkColorSetRGB(103, 103, 103);
92 const SkColor kDefaultColorToolbarStrokeInactive = SkColorSetRGB(163, 163, 163);
93 #endif
94
95 // ----------------------------------------------------------------------------
96
97 // Strings used in alignment properties.
98 const char* kAlignmentCenter = "center";
99 const char* kAlignmentTop = "top";
100 const char* kAlignmentBottom = "bottom";
101 const char* kAlignmentLeft = "left";
102 const char* kAlignmentRight = "right";
103
104 // Strings used in background tiling repetition properties.
105 const char* kTilingNoRepeat = "no-repeat";
106 const char* kTilingRepeatX = "repeat-x";
107 const char* kTilingRepeatY = "repeat-y";
108 const char* kTilingRepeat = "repeat";
109
110 // The image resources that will be tinted by the 'button' tint value.
111 // If you change this list, you must increment the version number in
112 // browser_theme_pack.cc, and you should assign persistent IDs to the
113 // data table at the start of said file or else tinted versions of
114 // these resources will not be created.
115 //
116 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
117 // should be removed once the cocoa port no longer uses them.
118 const int kToolbarButtonIDs[] = {
119 IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P,
120 IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P,
121 IDR_HOME, IDR_HOME_H, IDR_HOME_P,
122 IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P,
123 IDR_STOP, IDR_STOP_D, IDR_STOP_H, IDR_STOP_P,
124 IDR_BROWSER_ACTIONS_OVERFLOW, IDR_BROWSER_ACTIONS_OVERFLOW_H,
125 IDR_BROWSER_ACTIONS_OVERFLOW_P,
126 IDR_TOOLS, IDR_TOOLS_H, IDR_TOOLS_P,
127 IDR_MENU_DROPARROW,
128 IDR_THROBBER, IDR_THROBBER_WAITING, IDR_THROBBER_LIGHT,
129 IDR_TOOLBAR_BEZEL_HOVER, IDR_TOOLBAR_BEZEL_PRESSED, IDR_TOOLS_BAR,
130 };
131
TintForUnderline(SkColor input)132 SkColor TintForUnderline(SkColor input) {
133 return SkColorSetA(input, SkColorGetA(input) / 3);
134 }
135
136 } // namespace
137
138 // static
StringToAlignment(const std::string & alignment)139 int ThemeProperties::StringToAlignment(const std::string& alignment) {
140 std::vector<std::string> split;
141 base::SplitStringAlongWhitespace(alignment, &split);
142
143 int alignment_mask = 0;
144 for (std::vector<std::string>::iterator component(split.begin());
145 component != split.end(); ++component) {
146 if (LowerCaseEqualsASCII(*component, kAlignmentTop))
147 alignment_mask |= ALIGN_TOP;
148 else if (LowerCaseEqualsASCII(*component, kAlignmentBottom))
149 alignment_mask |= ALIGN_BOTTOM;
150 else if (LowerCaseEqualsASCII(*component, kAlignmentLeft))
151 alignment_mask |= ALIGN_LEFT;
152 else if (LowerCaseEqualsASCII(*component, kAlignmentRight))
153 alignment_mask |= ALIGN_RIGHT;
154 }
155 return alignment_mask;
156 }
157
158 // static
StringToTiling(const std::string & tiling)159 int ThemeProperties::StringToTiling(const std::string& tiling) {
160 const char* component = tiling.c_str();
161
162 if (base::strcasecmp(component, kTilingRepeatX) == 0)
163 return REPEAT_X;
164 if (base::strcasecmp(component, kTilingRepeatY) == 0)
165 return REPEAT_Y;
166 if (base::strcasecmp(component, kTilingRepeat) == 0)
167 return REPEAT;
168 // NO_REPEAT is the default choice.
169 return NO_REPEAT;
170 }
171
172 // static
AlignmentToString(int alignment)173 std::string ThemeProperties::AlignmentToString(int alignment) {
174 // Convert from an AlignmentProperty back into a string.
175 std::string vertical_string(kAlignmentCenter);
176 std::string horizontal_string(kAlignmentCenter);
177
178 if (alignment & ALIGN_TOP)
179 vertical_string = kAlignmentTop;
180 else if (alignment & ALIGN_BOTTOM)
181 vertical_string = kAlignmentBottom;
182
183 if (alignment & ALIGN_LEFT)
184 horizontal_string = kAlignmentLeft;
185 else if (alignment & ALIGN_RIGHT)
186 horizontal_string = kAlignmentRight;
187
188 return horizontal_string + " " + vertical_string;
189 }
190
191 // static
TilingToString(int tiling)192 std::string ThemeProperties::TilingToString(int tiling) {
193 // Convert from a TilingProperty back into a string.
194 if (tiling == REPEAT_X)
195 return kTilingRepeatX;
196 if (tiling == REPEAT_Y)
197 return kTilingRepeatY;
198 if (tiling == REPEAT)
199 return kTilingRepeat;
200 return kTilingNoRepeat;
201 }
202
203 // static
IsThemeableImage(int id)204 bool ThemeProperties::IsThemeableImage(int id) {
205 // TODO(pkotwicz): Cache results to improve lookup speed.
206 std::set<int> themeable_idrs;
207 BrowserThemePack::GetThemeableImageIDRs(&themeable_idrs);
208 return themeable_idrs.find(id) != themeable_idrs.end();
209 }
210
211 // static
GetTintableToolbarButtons()212 const std::set<int>& ThemeProperties::GetTintableToolbarButtons() {
213 CR_DEFINE_STATIC_LOCAL(std::set<int>, button_set, ());
214 if (button_set.empty()) {
215 button_set = std::set<int>(
216 kToolbarButtonIDs,
217 kToolbarButtonIDs + arraysize(kToolbarButtonIDs));
218 }
219
220 return button_set;
221 }
222
223 // static
GetDefaultTint(int id)224 color_utils::HSL ThemeProperties::GetDefaultTint(int id) {
225 switch (id) {
226 case TINT_FRAME:
227 return kDefaultTintFrame;
228 case TINT_FRAME_INACTIVE:
229 return kDefaultTintFrameInactive;
230 case TINT_FRAME_INCOGNITO:
231 return kDefaultTintFrameIncognito;
232 case TINT_FRAME_INCOGNITO_INACTIVE:
233 return kDefaultTintFrameIncognitoInactive;
234 case TINT_BUTTONS:
235 return kDefaultTintButtons;
236 case TINT_BACKGROUND_TAB:
237 return kDefaultTintBackgroundTab;
238 default:
239 color_utils::HSL result = {-1, -1, -1};
240 return result;
241 }
242 }
243
244 // static
GetDefaultColor(int id)245 SkColor ThemeProperties::GetDefaultColor(int id) {
246 switch (id) {
247 // Properties stored in theme pack.
248 case COLOR_FRAME:
249 return kDefaultColorFrame;
250 case COLOR_FRAME_INACTIVE:
251 return kDefaultColorFrameInactive;
252 case COLOR_FRAME_INCOGNITO:
253 return kDefaultColorFrameIncognito;
254 case COLOR_FRAME_INCOGNITO_INACTIVE:
255 return kDefaultColorFrameIncognitoInactive;
256 case COLOR_TOOLBAR:
257 return kDefaultColorToolbar;
258 case COLOR_TAB_TEXT:
259 return kDefaultColorTabText;
260 case COLOR_BACKGROUND_TAB_TEXT:
261 return kDefaultColorBackgroundTabText;
262 case COLOR_BOOKMARK_TEXT:
263 return kDefaultColorBookmarkText;
264 case COLOR_NTP_BACKGROUND:
265 return kDefaultColorNTPBackground;
266 case COLOR_NTP_TEXT:
267 return kDefaultColorNTPText;
268 case COLOR_NTP_LINK:
269 return kDefaultColorNTPLink;
270 case COLOR_NTP_LINK_UNDERLINE:
271 return TintForUnderline(kDefaultColorNTPLink);
272 case COLOR_NTP_HEADER:
273 return kDefaultColorNTPHeader;
274 case COLOR_NTP_SECTION:
275 return kDefaultColorNTPSection;
276 case COLOR_NTP_SECTION_TEXT:
277 return kDefaultColorNTPSectionText;
278 case COLOR_NTP_SECTION_LINK:
279 return kDefaultColorNTPSectionLink;
280 case COLOR_NTP_SECTION_LINK_UNDERLINE:
281 return TintForUnderline(kDefaultColorNTPSectionLink);
282 case COLOR_BUTTON_BACKGROUND:
283 return kDefaultColorButtonBackground;
284
285 // Properties not stored in theme pack.
286 case COLOR_CONTROL_BACKGROUND:
287 return kDefaultColorControlBackground;
288 case COLOR_TOOLBAR_SEPARATOR:
289 return kDefaultColorToolbarSeparator;
290 #if defined(OS_MACOSX)
291 case COLOR_TOOLBAR_BUTTON_STROKE:
292 return kDefaultColorToolbarButtonStroke;
293 case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE:
294 return kDefaultColorToolbarButtonStrokeInactive;
295 case COLOR_TOOLBAR_BEZEL:
296 return kDefaultColorToolbarBezel;
297 case COLOR_TOOLBAR_STROKE:
298 return kDefaultColorToolbarStroke;
299 case COLOR_TOOLBAR_STROKE_INACTIVE:
300 return kDefaultColorToolbarStrokeInactive;
301 #endif
302 default:
303 // Return a debugging red color.
304 return SK_ColorRED;
305 }
306 }
307
308 // static
GetDefaultDisplayProperty(int id)309 int ThemeProperties::GetDefaultDisplayProperty(int id) {
310 switch (id) {
311 case NTP_BACKGROUND_ALIGNMENT:
312 return kDefaultDisplayPropertyNTPAlignment;
313 case NTP_BACKGROUND_TILING:
314 return kDefaultDisplayPropertyNTPTiling;
315 case NTP_LOGO_ALTERNATE:
316 return kDefaultDisplayPropertyNTPAlternateLogo;
317 }
318
319 return -1;
320 }
321