1 // Copyright 2017 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 "xfa/fxfa/parser/cxfa_color.h"
8 
9 #include "fxjs/xfa/cjx_node.h"
10 #include "third_party/base/ptr_util.h"
11 
12 namespace {
13 
14 const CXFA_Node::PropertyData kColorPropertyData[] = {
15     {XFA_Element::Extras, 1, 0},
16 };
17 
18 const CXFA_Node::AttributeData kColorAttributeData[] = {
19     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
20     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
21     {XFA_Attribute::CSpace, XFA_AttributeType::CData, (void*)L"SRGB"},
22     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
23     {XFA_Attribute::Value, XFA_AttributeType::CData, nullptr},
24 };
25 
26 }  // namespace
27 
CXFA_Color(CXFA_Document * doc,XFA_PacketType packet)28 CXFA_Color::CXFA_Color(CXFA_Document* doc, XFA_PacketType packet)
29     : CXFA_Node(doc,
30                 packet,
31                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
32                 XFA_ObjectType::Node,
33                 XFA_Element::Color,
34                 kColorPropertyData,
35                 kColorAttributeData,
36                 pdfium::MakeUnique<CJX_Node>(this)) {}
37 
38 CXFA_Color::~CXFA_Color() = default;
39 
GetValue()40 FX_ARGB CXFA_Color::GetValue() {
41   Optional<WideString> val = JSObject()->TryCData(XFA_Attribute::Value, false);
42   return val ? StringToFXARGB(val->AsStringView()) : 0xFF000000;
43 }
44 
GetValueOrDefault(FX_ARGB defaultValue)45 FX_ARGB CXFA_Color::GetValueOrDefault(FX_ARGB defaultValue) {
46   Optional<WideString> val = JSObject()->TryCData(XFA_Attribute::Value, false);
47   return val ? StringToFXARGB(val->AsStringView()) : defaultValue;
48 }
49 
SetValue(FX_ARGB color)50 void CXFA_Color::SetValue(FX_ARGB color) {
51   int a;
52   int r;
53   int g;
54   int b;
55   std::tie(a, r, g, b) = ArgbDecode(color);
56   JSObject()->SetCData(XFA_Attribute::Value,
57                        WideString::Format(L"%d,%d,%d", r, g, b), false, false);
58 }
59