Bug 1299741 part 2 - Move ClampColor to nsColor.h. r?jrmuizel draft
authorXidorn Quan <xidorn+moz@upsuper.org>
Fri, 02 Sep 2016 00:18:28 +1000
changeset 408683 4e82fff7a19fcaa83def725cf85599ead4e8114f
parent 408479 6c39611f9d1f747f89cc4ae391696a739d0a6882
child 408684 268267d7317f6b190a797f83b7e3e44b32d5c5a7
push id28271
push userxquan@mozilla.com
push dateThu, 01 Sep 2016 14:23:32 +0000
reviewersjrmuizel
bugs1299741
milestone51.0a1
Bug 1299741 part 2 - Move ClampColor to nsColor.h. r?jrmuizel MozReview-Commit-ID: 6qbVhOA3DE4
gfx/src/nsColor.h
layout/style/StyleAnimationValue.cpp
--- a/gfx/src/nsColor.h
+++ b/gfx/src/nsColor.h
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsColor_h___
 #define nsColor_h___
 
 #include <stddef.h>                     // for size_t
 #include <stdint.h>                     // for uint8_t, uint32_t
 #include "nscore.h"                     // for nsAString
+#include "nsCoord.h"                    // for NSToIntRound
 
 class nsAString;
 class nsString;
 
 // A color is a 32 bit unsigned integer with four components: R, G, B
 // and A.
 typedef uint32_t nscolor;
 
@@ -28,16 +29,28 @@ typedef uint32_t nscolor;
   ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
 
 // Extract color components from nscolor
 #define NS_GET_R(_rgba) ((uint8_t) ((_rgba) & 0xff))
 #define NS_GET_G(_rgba) ((uint8_t) (((_rgba) >> 8) & 0xff))
 #define NS_GET_B(_rgba) ((uint8_t) (((_rgba) >> 16) & 0xff))
 #define NS_GET_A(_rgba) ((uint8_t) (((_rgba) >> 24) & 0xff))
 
+template<typename T>
+inline uint_fast8_t NS_ClampColor(T aColor)
+{
+  if (aColor >= 255) {
+    return 255;
+  }
+  if (aColor <= 0) {
+    return 0;
+  }
+  return NSToIntRound(aColor);
+}
+
 // Fast approximate division by 255. It has the property that
 // for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
 // But it only uses two adds and two shifts instead of an 
 // integer division (which is expensive on many processors).
 //
 // equivalent to target=v/255
 #define FAST_DIVIDE_BY_255(target,v)               \
   PR_BEGIN_MACRO                                   \
--- a/layout/style/StyleAnimationValue.cpp
+++ b/layout/style/StyleAnimationValue.cpp
@@ -947,27 +947,16 @@ StyleAnimationValue::ComputeDistance(nsC
       return true;
     }
   }
 
   MOZ_ASSERT(false, "Can't compute distance using the given common unit");
   return false;
 }
 
-#define MAX_PACKED_COLOR_COMPONENT 255
-
-inline uint8_t ClampColor(double aColor)
-{
-  if (aColor >= MAX_PACKED_COLOR_COMPONENT)
-    return MAX_PACKED_COLOR_COMPONENT;
-  if (aColor <= 0.0)
-    return 0;
-  return NSToIntRound(aColor);
-}
-
 // Ensure that a float/double value isn't NaN by returning zero instead
 // (NaN doesn't have a sign) as a general restriction for floating point
 // values in RestrictValue.
 template<typename T>
 MOZ_ALWAYS_INLINE T
 EnsureNotNan(T aValue)
 {
   return aValue;
@@ -1180,19 +1169,19 @@ AddWeightedColors(double aCoeff1, nscolo
   }
 
   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);
+  uint8_t Rres = NS_ClampColor((R1 * aCoeff1 + R2 * aCoeff2) * factor);
+  uint8_t Gres = NS_ClampColor((G1 * aCoeff1 + G2 * aCoeff2) * factor);
+  uint8_t Bres = NS_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)
 {
@@ -1206,19 +1195,19 @@ DiluteColor(nscolor aColor, double aDilu
   }
 
   // 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),
+  return NS_RGBA(NS_ClampColor(R * aDilutionRatio * factor),
+                 NS_ClampColor(G * aDilutionRatio * factor),
+                 NS_ClampColor(B * aDilutionRatio * factor),
                  NSToIntRound(Aresf * 255.0));
 }
 
 static bool
 AddShadowItems(double aCoeff1, const nsCSSValue &aValue1,
                double aCoeff2, const nsCSSValue &aValue2,
                nsCSSValueList **&aResultTail)
 {