1 // Copyright 2019 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 "ui/color/color_provider.h"
6 
7 #include <utility>
8 
9 #include "ui/color/color_mixer.h"
10 #include "ui/gfx/color_palette.h"
11 
12 namespace ui {
13 
AddMixer()14 ColorMixer& ColorProvider::AddMixer() {
15   // Adding a mixer could change any of the result colors.
16   cache_.clear();
17 
18   // Supply each new mixer with the previous mixer in the pipeline; this way
19   // GetColor() need not query each mixer in order, but simply ask the last
20   // mixer for its result, and trust mixers to query each other back up the
21   // chain as needed.
22   mixers_.emplace_front(mixers_.empty() ? nullptr : &mixers_.front());
23   return mixers_.front();
24 }
25 
GetColor(ColorId id) const26 SkColor ColorProvider::GetColor(ColorId id) const {
27   DCHECK_COLOR_ID_VALID(id);
28 
29   if (mixers_.empty())
30     return gfx::kPlaceholderColor;
31 
32   // Only compute the result color when it's not already in the cache.
33   auto i = cache_.find(id);
34   if (i == cache_.end())
35     i = cache_.insert({id, mixers_.front().GetResultColor(id)}).first;
36 
37   return i->second;
38 }
39 
40 ColorProvider::ColorProvider() = default;
41 
42 ColorProvider::~ColorProvider() = default;
43 
44 }  // namespace ui
45