Bug 1294614 - Part 2: Factor out AddWeightedColors. r?dholbert draft
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Fri, 26 Aug 2016 09:10:25 +0900
changeset 405878 0a9fae49472c5f04be62d5b8e40d220e8b7935bf
parent 405877 62d172201f44c09decccf67b3ba329dd0f32651b
child 405879 b73a978b6e424530173f1e061b0cb506f2b60c4d
push id27583
push userhiikezoe@mozilla-japan.org
push dateFri, 26 Aug 2016 01:48:04 +0000
reviewersdholbert
bugs1294614
milestone51.0a1
Bug 1294614 - Part 2: Factor out AddWeightedColors. r?dholbert MozReview-Commit-ID: 2DhPWr7tRA1
layout/style/StyleAnimationValue.cpp
--- a/layout/style/StyleAnimationValue.cpp
+++ b/layout/style/StyleAnimationValue.cpp
@@ -1155,16 +1155,52 @@ AddCSSValuePercentNumber(const uint32_t 
   // aCoeff2 is 0, then we'll return the value halfway between 1 and
   // aValue1, rather than the value halfway between 0 and aValue1.
   // Note that we do something similar in AddTransformScale().
   float result = (n1 - aInitialVal) * aCoeff1 + (n2 - aInitialVal) * aCoeff2;
   aResult.SetFloatValue(RestrictValue(aValueRestrictions, result + aInitialVal),
                         eCSSUnit_Number);
 }
 
+static nscolor
+AddWeightedColors(double aCoeff1, nscolor aColor1,
+                  double aCoeff2, nscolor aColor2)
+{
+  // FIXME (spec): The CSS transitions spec doesn't say whether
+  // colors are premultiplied, but things work better when they are,
+  // so use premultiplication.  Spec issue is still open per
+  // http://lists.w3.org/Archives/Public/www-style/2009Jul/0050.html
+
+  // To save some math, scale the alpha down to a 0-1 scale, but
+  // leave the color components on a 0-255 scale.
+  double A1 = NS_GET_A(aColor1) * (1.0 / 255.0);
+  double R1 = NS_GET_R(aColor1) * A1;
+  double G1 = NS_GET_G(aColor1) * A1;
+  double B1 = NS_GET_B(aColor1) * A1;
+  double A2 = NS_GET_A(aColor2) * (1.0 / 255.0);
+  double R2 = NS_GET_R(aColor2) * A2;
+  double G2 = NS_GET_G(aColor2) * A2;
+  double B2 = NS_GET_B(aColor2) * A2;
+  double Aresf = (A1 * aCoeff1 + A2 * aCoeff2);
+  if (Aresf <= 0.0) {
+    return NS_RGBA(0, 0, 0, 0);
+  }
+
+  if (Aresf > 1.0) {
+    Aresf = 1.0;
+  }
+
+  double factor = 1.0 / Aresf;
+  uint8_t Ares = NSToIntRound(Aresf * 255.0);
+  uint8_t Rres = ClampColor((R1 * aCoeff1 + R2 * aCoeff2) * factor);
+  uint8_t Gres = ClampColor((G1 * aCoeff1 + G2 * aCoeff2) * factor);
+  uint8_t Bres = ClampColor((B1 * aCoeff1 + B2 * aCoeff2) * factor);
+  return NS_RGBA(Rres, Gres, Bres, Ares);
+}
+
 static bool
 AddShadowItems(double aCoeff1, const nsCSSValue &aValue1,
                double aCoeff2, const nsCSSValue &aValue2,
                nsCSSValueList **&aResultTail)
 {
   // X, Y, Radius, Spread, Color, Inset
   MOZ_ASSERT(aValue1.GetUnit() == eCSSUnit_Array,
              "wrong unit");
@@ -2343,46 +2379,18 @@ StyleAnimationValue::AddWeighted(nsCSSPr
       aResultValue.SetFloatValue(RestrictValue(aProperty,
         aCoeff1 * aValue1.GetFloatValue() +
         aCoeff2 * aValue2.GetFloatValue()));
       return true;
     }
     case eUnit_Color: {
       nscolor color1 = aValue1.GetColorValue();
       nscolor color2 = aValue2.GetColorValue();
-      // FIXME (spec): The CSS transitions spec doesn't say whether
-      // colors are premultiplied, but things work better when they are,
-      // so use premultiplication.  Spec issue is still open per
-      // http://lists.w3.org/Archives/Public/www-style/2009Jul/0050.html
-
-      // To save some math, scale the alpha down to a 0-1 scale, but
-      // leave the color components on a 0-255 scale.
-      double A1 = NS_GET_A(color1) * (1.0 / 255.0);
-      double R1 = NS_GET_R(color1) * A1;
-      double G1 = NS_GET_G(color1) * A1;
-      double B1 = NS_GET_B(color1) * A1;
-      double A2 = NS_GET_A(color2) * (1.0 / 255.0);
-      double R2 = NS_GET_R(color2) * A2;
-      double G2 = NS_GET_G(color2) * A2;
-      double B2 = NS_GET_B(color2) * A2;
-      double Aresf = (A1 * aCoeff1 + A2 * aCoeff2);
-      nscolor resultColor;
-      if (Aresf <= 0.0) {
-        resultColor = NS_RGBA(0, 0, 0, 0);
-      } else {
-        if (Aresf > 1.0) {
-          Aresf = 1.0;
-        }
-        double factor = 1.0 / Aresf;
-        uint8_t Ares = NSToIntRound(Aresf * 255.0);
-        uint8_t Rres = ClampColor((R1 * aCoeff1 + R2 * aCoeff2) * factor);
-        uint8_t Gres = ClampColor((G1 * aCoeff1 + G2 * aCoeff2) * factor);
-        uint8_t Bres = ClampColor((B1 * aCoeff1 + B2 * aCoeff2) * factor);
-        resultColor = NS_RGBA(Rres, Gres, Bres, Ares);
-      }
+      nscolor resultColor =
+        AddWeightedColors(aCoeff1, color1, aCoeff2, color2);
       aResultValue.SetColorValue(resultColor);
       return true;
     }
     case eUnit_Calc: {
       PixelCalcValue v1 = ExtractCalcValue(aValue1);
       PixelCalcValue v2 = ExtractCalcValue(aValue2);
       double len = aCoeff1 * v1.mLength + aCoeff2 * v2.mLength;
       double pct = aCoeff1 * v1.mPercent + aCoeff2 * v2.mPercent;