Bug 1299741 part 4 - Add StyleComplexColor type for storing color combining numeric color and currentcolor. r=dbaron draft
authorXidorn Quan <me@upsuper.org>
Fri, 02 Sep 2016 14:58:10 +1000
changeset 415363 0ad610a40431e74a540714491be35ad444fd4372
parent 415362 de1003c5546f77dc251d1dc1c3634ac79b73481f
child 415364 b76c7d39ca21e4316a09524bbbc918a12ab81194
push id29862
push userxquan@mozilla.com
push dateTue, 20 Sep 2016 08:44:59 +0000
reviewersdbaron
bugs1299741
milestone52.0a1
Bug 1299741 part 4 - Add StyleComplexColor type for storing color combining numeric color and currentcolor. r=dbaron MozReview-Commit-ID: I6DaSaMCgtH
layout/style/StyleComplexColor.h
layout/style/moz.build
layout/style/nsStyleStruct.h
new file mode 100644
--- /dev/null
+++ b/layout/style/StyleComplexColor.h
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* represent a color combines a numeric color and currentcolor */
+
+#ifndef mozilla_StyleComplexColor_h_
+#define mozilla_StyleComplexColor_h_
+
+#include "nsColor.h"
+
+namespace mozilla {
+
+/**
+ * This struct represents a combined color from a numeric color and
+ * the current foreground color (currentcolor keyword).
+ * Conceptually, the formula is "color * (1 - p) + currentcolor * p"
+ * where p is mForegroundRatio. See mozilla::LinearBlendColors for
+ * the actual algorithm.
+ */
+struct StyleComplexColor
+{
+  nscolor mColor;
+  uint8_t mForegroundRatio;
+
+  StyleComplexColor() {}
+  StyleComplexColor(nscolor aColor, uint_fast8_t aForegroundRatio)
+    : mColor(aColor), mForegroundRatio(aForegroundRatio) {}
+
+  static StyleComplexColor FromColor(nscolor aColor)
+    { return StyleComplexColor(aColor, 0); }
+  static StyleComplexColor CurrentColor()
+    { return StyleComplexColor(NS_RGBA(0, 0, 0, 0), 255); }
+
+  bool IsNumericColor() const { return mForegroundRatio == 0; }
+  bool IsCurrentColor() const { return mForegroundRatio == 255; }
+
+  bool operator==(const StyleComplexColor& aOther) const {
+    return mForegroundRatio == aOther.mForegroundRatio &&
+           (IsCurrentColor() || mColor == aOther.mColor);
+  }
+  bool operator!=(const StyleComplexColor& aOther) const {
+    return !(*this == aOther);
+  }
+};
+
+}
+
+#endif // mozilla_StyleComplexColor_h_
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -95,16 +95,17 @@ EXPORTS.mozilla += [
     'ServoBindingList.h',
     'ServoBindings.h',
     'ServoElementSnapshot.h',
     'ServoStyleSet.h',
     'ServoStyleSheet.h',
     'SheetType.h',
     'StyleAnimationValue.h',
     'StyleBackendType.h',
+    'StyleComplexColor.h',
     'StyleContextSource.h',
     'StyleSetHandle.h',
     'StyleSetHandleInlines.h',
     'StyleSheet.h',
     'StyleSheetHandle.h',
     'StyleSheetHandleInlines.h',
     'StyleSheetInfo.h',
     'StyleSheetInlines.h',
--- a/layout/style/nsStyleStruct.h
+++ b/layout/style/nsStyleStruct.h
@@ -13,16 +13,17 @@
 #define nsStyleStruct_h___
 
 #include "mozilla/ArenaObjectID.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/CSSVariableValues.h"
 #include "mozilla/Maybe.h"
 #include "mozilla/SheetType.h"
 #include "mozilla/StaticPtr.h"
+#include "mozilla/StyleComplexColor.h"
 #include "mozilla/StyleStructContext.h"
 #include "mozilla/UniquePtr.h"
 #include "nsColor.h"
 #include "nsCoord.h"
 #include "nsMargin.h"
 #include "nsFont.h"
 #include "nsStyleCoord.h"
 #include "nsStyleConsts.h"
@@ -475,16 +476,21 @@ private:
 struct MOZ_NEEDS_MEMMOVABLE_MEMBERS nsStyleColor
 {
   explicit nsStyleColor(StyleStructContext aContext);
   nsStyleColor(const nsStyleColor& aOther);
   ~nsStyleColor() {
     MOZ_COUNT_DTOR(nsStyleColor);
   }
 
+  nscolor CalcComplexColor(const mozilla::StyleComplexColor& aColor) const {
+    return mozilla::LinearBlendColors(aColor.mColor, mColor,
+                                      aColor.mForegroundRatio);
+  }
+
   nsChangeHint CalcDifference(const nsStyleColor& aNewData) const;
   static nsChangeHint MaxDifference() {
     return nsChangeHint_RepaintFrame;
   }
   static nsChangeHint DifferenceAlwaysHandledForDescendants() {
     // CalcDifference never returns the reflow hints that are sometimes
     // handled for descendants at all.
     return nsChangeHint(0);