1 // Copyright 2019 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfdoc/cpdf_color_utils.h"
8
9 #include "core/fpdfapi/parser/cpdf_array.h"
10 #include "core/fpdfdoc/cpdf_defaultappearance.h"
11 #include "core/fxcrt/bytestring.h"
12
13 namespace fpdfdoc {
14
CFXColorFromArray(const CPDF_Array & array)15 CFX_Color CFXColorFromArray(const CPDF_Array& array) {
16 CFX_Color rt;
17 switch (array.size()) {
18 case 1:
19 rt = CFX_Color(CFX_Color::kGray, array.GetNumberAt(0));
20 break;
21 case 3:
22 rt = CFX_Color(CFX_Color::kRGB, array.GetNumberAt(0),
23 array.GetNumberAt(1), array.GetNumberAt(2));
24 break;
25 case 4:
26 rt = CFX_Color(CFX_Color::kCMYK, array.GetNumberAt(0),
27 array.GetNumberAt(1), array.GetNumberAt(2),
28 array.GetNumberAt(3));
29 break;
30 }
31 return rt;
32 }
33
CFXColorFromString(const ByteString & str)34 CFX_Color CFXColorFromString(const ByteString& str) {
35 CPDF_DefaultAppearance appearance(str);
36 float values[4];
37 Optional<CFX_Color::Type> color_type = appearance.GetColor(values);
38 if (!color_type || *color_type == CFX_Color::kTransparent)
39 return CFX_Color(CFX_Color::kTransparent);
40 if (*color_type == CFX_Color::kGray)
41 return CFX_Color(CFX_Color::kGray, values[0]);
42 if (*color_type == CFX_Color::kRGB)
43 return CFX_Color(CFX_Color::kRGB, values[0], values[1], values[2]);
44 if (*color_type == CFX_Color::kCMYK) {
45 return CFX_Color(CFX_Color::kCMYK, values[0], values[1], values[2],
46 values[3]);
47 }
48
49 NOTREACHED();
50 return CFX_Color(CFX_Color::kTransparent);
51 }
52
53 } // namespace fpdfdoc
54