Bug 1299741 part 3 - Invoke DiluteColor from AddWeightedColors so that we can reuse AddWeightedColors directly. r=birtles draft
authorXidorn Quan <xidorn+moz@upsuper.org>
Thu, 01 Sep 2016 16:18:11 +1000
changeset 409095 2b3436c79ef0a901004cd63f973c35467de57e42
parent 409094 19bd9308dc2d48bd613a4db5abcdf1b09483a457
child 409096 86967cce8031de7755c08d576581246a73e12639
push id28388
push userxquan@mozilla.com
push dateFri, 02 Sep 2016 07:23:54 +0000
reviewersbirtles
bugs1299741
milestone51.0a1
Bug 1299741 part 3 - Invoke DiluteColor from AddWeightedColors so that we can reuse AddWeightedColors directly. r=birtles MozReview-Commit-ID: L6O8BXtoSzm
layout/style/StyleAnimationValue.cpp
--- a/layout/style/StyleAnimationValue.cpp
+++ b/layout/style/StyleAnimationValue.cpp
@@ -1135,25 +1135,60 @@ 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);
 }
 
+// Multiplies |aColor| by |aDilutionRatio| with premultiplication.
+// (The logic here should pretty closely match AddWeightedColors()' logic.)
+static nscolor
+DiluteColor(nscolor aColor, double aDilutionRatio)
+{
+  MOZ_ASSERT(aDilutionRatio >= 0.0 && aDilutionRatio <= 1.0,
+             "Dilution ratio should be in [0, 1]");
+
+  double A = NS_GET_A(aColor) * (1.0 / 255.0);
+  double Aresf = A * aDilutionRatio;
+  if (Aresf <= 0.0) {
+    return NS_RGBA(0, 0, 0, 0);
+  }
+
+  // Premultiplication
+  double R = NS_GET_R(aColor) * A;
+  double G = NS_GET_G(aColor) * A;
+  double B = NS_GET_B(aColor) * A;
+
+  double factor = 1.0 / Aresf;
+  return NS_RGBA(ClampColor(R * aDilutionRatio * factor),
+                 ClampColor(G * aDilutionRatio * factor),
+                 ClampColor(B * aDilutionRatio * factor),
+                 NSToIntRound(Aresf * 255.0));
+}
+
 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
 
+  // We are using AddWeighted() with a zero aCoeff2 for colors to
+  // pretend AddWeighted() against transparent color, i.e. rgba(0, 0, 0, 0).
+  // But unpremultiplication in this function does not work well
+  // for such cases, so we use another function named DiluteColor() which
+  // has a similar logic to this function.
+  if (aCoeff2 == 0.0) {
+    return DiluteColor(aColor1, aCoeff1);
+  }
+
   // 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;
@@ -1171,42 +1206,16 @@ AddWeightedColors(double aCoeff1, nscolo
   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);
 }
 
-// Multiplies |aColor| by |aDilutionRatio| with premultiplication.
-// (The logic here should pretty closely match AddWeightedColors()' logic.)
-static nscolor
-DiluteColor(nscolor aColor, double aDilutionRatio)
-{
-  MOZ_ASSERT(aDilutionRatio >= 0.0 && aDilutionRatio <= 1.0,
-             "Dilution ratio should be in [0, 1]");
-
-  double A = NS_GET_A(aColor) * (1.0 / 255.0);
-  double Aresf = A * aDilutionRatio;
-  if (Aresf <= 0.0) {
-    return NS_RGBA(0, 0, 0, 0);
-  }
-
-  // Premultiplication
-  double R = NS_GET_R(aColor) * A;
-  double G = NS_GET_G(aColor) * A;
-  double B = NS_GET_B(aColor) * A;
-
-  double factor = 1.0 / Aresf;
-  return NS_RGBA(ClampColor(R * aDilutionRatio * factor),
-                 ClampColor(G * aDilutionRatio * factor),
-                 ClampColor(B * aDilutionRatio * factor),
-                 NSToIntRound(Aresf * 255.0));
-}
-
 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");
@@ -2383,29 +2392,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();
-      nscolor resultColor;
-
-      // We are using AddWeighted() with a zero aCoeff2 for colors to
-      // pretend AddWeighted() against transparent color, i.e. rgba(0, 0, 0, 0).
-      // But unpremultiplication in AddWeightedColors() does not work well
-      // for such cases, so we use another function named DiluteColor() which
-      // has a similar logic to AddWeightedColors().
-      if (aCoeff2 == 0.0) {
-        resultColor = DiluteColor(color1, aCoeff1);
-      } else {
-        resultColor = AddWeightedColors(aCoeff1, color1, aCoeff2, color2);
-      }
-      aResultValue.SetColorValue(resultColor);
+      aResultValue.SetColorValue(AddWeightedColors(aCoeff1, color1,
+                                                   aCoeff2, color2));
       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;
       bool hasPct = (aCoeff1 != 0.0 && v1.mHasPercent) ||