Refactor common CJS_Field color code into GetFormControlColor().

In CJS_Field, get_fill_color() and get_stroke_color() have nearly
identical code. Refactor that into a helper function. To make this
possible without resorting to function pointers, get rid of
GetBorderColorARGB(), GetOriginalBorderColorComponent(),
GetBackgroundColor() and GetOriginalBackgroundColorComponent() in
CPDF_FormControl. Those are just wrapper methods around GetColorARGB()
and GetOriginalColorComponent(). Expose the 2 methods called by the
wrapper methods, and call them directly from CJS_Field.

Change-Id: I8aeba694d7312ca2bf3ce2655322e99c3ba5bc6d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/81512
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_formcontrol.h b/core/fpdfdoc/cpdf_formcontrol.h
index d20c944..ead337b 100644
--- a/core/fpdfdoc/cpdf_formcontrol.h
+++ b/core/fpdfdoc/cpdf_formcontrol.h
@@ -55,26 +55,13 @@
   bool HasMKEntry(const ByteString& csEntry) const;
   int GetRotation() const;
 
-  CFX_Color::TypeAndARGB GetBorderColorARGB() {
-    return GetColorARGB(pdfium::appearance::kBC);
-  }
-
-  float GetOriginalBorderColorComponent(int index) {
-    return GetOriginalColorComponent(index, pdfium::appearance::kBC);
-  }
+  CFX_Color::TypeAndARGB GetColorARGB(const ByteString& csEntry);
+  float GetOriginalColorComponent(int index, const ByteString& csEntry);
 
   CFX_Color GetOriginalBorderColor() {
     return GetOriginalColor(pdfium::appearance::kBC);
   }
 
-  CFX_Color::TypeAndARGB GetBackgroundColor() {
-    return GetColorARGB(pdfium::appearance::kBG);
-  }
-
-  float GetOriginalBackgroundColorComponent(int index) {
-    return GetOriginalColorComponent(index, pdfium::appearance::kBG);
-  }
-
   CFX_Color GetOriginalBackgroundColor() {
     return GetOriginalColor(pdfium::appearance::kBG);
   }
@@ -105,8 +92,6 @@
 
  private:
   RetainPtr<CPDF_Font> GetDefaultControlFont() const;
-  CFX_Color::TypeAndARGB GetColorARGB(const ByteString& csEntry);
-  float GetOriginalColorComponent(int index, const ByteString& csEntry);
   CFX_Color GetOriginalColor(const ByteString& csEntry);
 
   WideString GetCaption(const ByteString& csEntry) const;
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index 4934fc3..118737f 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -11,6 +11,7 @@
 #include <utility>
 
 #include "constants/annotation_common.h"
+#include "constants/appearance.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
@@ -428,7 +429,7 @@
 
 Optional<FX_COLORREF> CPDFSDK_Widget::GetFillColor() const {
   CFX_Color::TypeAndARGB type_argb_pair =
-      GetFormControl()->GetBackgroundColor();
+      GetFormControl()->GetColorARGB(pdfium::appearance::kBG);
 
   if (type_argb_pair.color_type == CFX_Color::Type::kTransparent)
     return pdfium::nullopt;
@@ -438,7 +439,7 @@
 
 Optional<FX_COLORREF> CPDFSDK_Widget::GetBorderColor() const {
   CFX_Color::TypeAndARGB type_argb_pair =
-      GetFormControl()->GetBorderColorARGB();
+      GetFormControl()->GetColorARGB(pdfium::appearance::kBC);
   if (type_argb_pair.color_type == CFX_Color::Type::kTransparent)
     return pdfium::nullopt;
 
diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp
index 7714284..aa55c7d 100644
--- a/fxjs/cjs_field.cpp
+++ b/fxjs/cjs_field.cpp
@@ -184,6 +184,29 @@
   return fields;
 }
 
+CFX_Color GetFormControlColor(CPDF_FormControl* pFormControl,
+                              const ByteString& entry) {
+  CFX_Color color;
+  switch (pFormControl->GetColorARGB(entry).color_type) {
+    case CFX_Color::Type::kTransparent:
+      return CFX_Color(CFX_Color::Type::kTransparent);
+    case CFX_Color::Type::kGray:
+      return CFX_Color(CFX_Color::Type::kGray,
+                       pFormControl->GetOriginalColorComponent(0, entry));
+    case CFX_Color::Type::kRGB:
+      return CFX_Color(CFX_Color::Type::kRGB,
+                       pFormControl->GetOriginalColorComponent(0, entry),
+                       pFormControl->GetOriginalColorComponent(1, entry),
+                       pFormControl->GetOriginalColorComponent(2, entry));
+    case CFX_Color::Type::kCMYK:
+      return CFX_Color(CFX_Color::Type::kCMYK,
+                       pFormControl->GetOriginalColorComponent(0, entry),
+                       pFormControl->GetOriginalColorComponent(1, entry),
+                       pFormControl->GetOriginalColorComponent(2, entry),
+                       pFormControl->GetOriginalColorComponent(3, entry));
+  }
+}
+
 bool SetWidgetDisplayStatus(CPDFSDK_Widget* pWidget, int value) {
   if (!pWidget)
     return false;
@@ -1291,30 +1314,7 @@
   if (!pFormControl)
     return CJS_Result::Failure(JSMessage::kBadObjectError);
 
-  CFX_Color color;
-  switch (pFormControl->GetBackgroundColor().color_type) {
-    case CFX_Color::Type::kTransparent:
-      color = CFX_Color(CFX_Color::Type::kTransparent);
-      break;
-    case CFX_Color::Type::kGray:
-      color = CFX_Color(CFX_Color::Type::kGray,
-                        pFormControl->GetOriginalBackgroundColorComponent(0));
-      break;
-    case CFX_Color::Type::kRGB:
-      color = CFX_Color(CFX_Color::Type::kRGB,
-                        pFormControl->GetOriginalBackgroundColorComponent(0),
-                        pFormControl->GetOriginalBackgroundColorComponent(1),
-                        pFormControl->GetOriginalBackgroundColorComponent(2));
-      break;
-    case CFX_Color::Type::kCMYK:
-      color = CFX_Color(CFX_Color::Type::kCMYK,
-                        pFormControl->GetOriginalBackgroundColorComponent(0),
-                        pFormControl->GetOriginalBackgroundColorComponent(1),
-                        pFormControl->GetOriginalBackgroundColorComponent(2),
-                        pFormControl->GetOriginalBackgroundColorComponent(3));
-      break;
-  }
-
+  CFX_Color color = GetFormControlColor(pFormControl, pdfium::appearance::kBG);
   v8::Local<v8::Value> array =
       CJS_Color::ConvertPWLColorToArray(pRuntime, color);
   if (array.IsEmpty())
@@ -1830,30 +1830,7 @@
   if (!pFormControl)
     return CJS_Result::Failure(JSMessage::kBadObjectError);
 
-  CFX_Color color;
-  switch (pFormControl->GetBorderColorARGB().color_type) {
-    case CFX_Color::Type::kTransparent:
-      color = CFX_Color(CFX_Color::Type::kTransparent);
-      break;
-    case CFX_Color::Type::kGray:
-      color = CFX_Color(CFX_Color::Type::kGray,
-                        pFormControl->GetOriginalBorderColorComponent(0));
-      break;
-    case CFX_Color::Type::kRGB:
-      color = CFX_Color(CFX_Color::Type::kRGB,
-                        pFormControl->GetOriginalBorderColorComponent(0),
-                        pFormControl->GetOriginalBorderColorComponent(1),
-                        pFormControl->GetOriginalBorderColorComponent(2));
-      break;
-    case CFX_Color::Type::kCMYK:
-      color = CFX_Color(CFX_Color::Type::kCMYK,
-                        pFormControl->GetOriginalBorderColorComponent(0),
-                        pFormControl->GetOriginalBorderColorComponent(1),
-                        pFormControl->GetOriginalBorderColorComponent(2),
-                        pFormControl->GetOriginalBorderColorComponent(3));
-      break;
-  }
-
+  CFX_Color color = GetFormControlColor(pFormControl, pdfium::appearance::kBC);
   v8::Local<v8::Value> array =
       CJS_Color::ConvertPWLColorToArray(pRuntime, color);
   if (array.IsEmpty())