Bug 1299741 part 8 - Add StyleComplexColor type for storing color combining numeric color and currentcolor. r?dbaron draft
authorXidorn Quan <me@upsuper.org>
Thu, 01 Sep 2016 16:09:16 +1000
changeset 408661 e26e5fcc5c3b7e93836d1432f8fe7d9f5d5f78b7
parent 408660 9885f90d3ec07eda1ba3197b13d32a448d023cf5
child 408662 e1fc37ddb89eeb8ad525596aca47f20268244c27
push id28267
push userxquan@mozilla.com
push dateThu, 01 Sep 2016 14:00:40 +0000
reviewersdbaron
bugs1299741
milestone51.0a1
Bug 1299741 part 8 - 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,49 @@
+/* -*- 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 {
+
+// A struct represents a color combined from a numeric color and the
+// current foreground color (currentcolor keyword).
+// The target color is roughly "color * (1 - p) + currentcolor * p".
+// where p is mForegroundRatio.
+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 IsCurrentColor() == aOther.IsCurrentColor() ||
+           (mColor == aOther.mColor &&
+            mForegroundRatio == aOther.mForegroundRatio);
+  }
+  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"
@@ -474,16 +475,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 NS_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);