Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / image_util.cc
blob4b16367fd96de9ba1d8e7c93b29a3fcc433ba610
1 // Copyright 2014 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.
5 #include "extensions/common/image_util.h"
7 #include <stdint.h>
8 #include <vector>
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "third_party/skia/include/core/SkColor.h"
14 namespace extensions {
15 namespace image_util {
17 bool ParseCSSColorString(const std::string& color_string, SkColor* result) {
18 std::string formatted_color;
19 // Check the string for incorrect formatting.
20 if (color_string.empty() || color_string[0] != '#')
21 return false;
23 // Convert the string from #FFF format to #FFFFFF format.
24 if (color_string.length() == 4) {
25 for (size_t i = 1; i < 4; ++i) {
26 formatted_color += color_string[i];
27 formatted_color += color_string[i];
29 } else if (color_string.length() == 7) {
30 formatted_color = color_string.substr(1, 6);
31 } else {
32 return false;
35 // Convert the string to an integer and make sure it is in the correct value
36 // range.
37 std::vector<uint8_t> color_bytes;
38 if (!base::HexStringToBytes(formatted_color, &color_bytes))
39 return false;
41 *result = SkColorSetARGB(255, color_bytes[0], color_bytes[1], color_bytes[2]);
42 return true;
45 std::string GenerateCSSColorString(SkColor color) {
46 return base::StringPrintf("#%02X%02X%02X", SkColorGetR(color),
47 SkColorGetG(color), SkColorGetB(color));
50 } // namespace image_util
51 } // namespace extensions