Bug 1356941 ComputedDistance for IntermediateRGBA. r?boris draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Mon, 24 Apr 2017 15:03:42 +0900
changeset 566921 0f1e6c9ef32ea7bb043fb5fa5fe312145ba90457
parent 566920 b8e21d6c40015f69e644573f9461916a3fb69744
child 566922 d89c7c44fc50d9aefc5d33f67e432ba7fa6d7886
push id55379
push userhikezoe@mozilla.com
push dateMon, 24 Apr 2017 06:04:15 +0000
reviewersboris
bugs1356941
milestone55.0a1
Bug 1356941 ComputedDistance for IntermediateRGBA. r?boris MozReview-Commit-ID: 805TYcvvnMQ
servo/components/style/properties/helpers/animated_properties.mako.rs
--- a/servo/components/style/properties/helpers/animated_properties.mako.rs
+++ b/servo/components/style/properties/helpers/animated_properties.mako.rs
@@ -2320,16 +2320,41 @@ impl ComputeDistance for CSSParserColor 
             (CSSParserColor::RGBA(ref this), CSSParserColor::RGBA(ref other)) => {
                 this.compute_squared_distance(other)
             },
             _ => Ok(0.0),
         }
     }
 }
 
+impl ComputeDistance for IntermediateRGBA {
+    #[inline]
+    fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
+        self.compute_squared_distance(other).map(|sq| sq.sqrt())
+    }
+
+    #[inline]
+    fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {
+        let start = [ self.alpha,
+                      self.red * self.alpha,
+                      self.green * self.alpha,
+                      self.blue * self.alpha ];
+        let end = [ other.alpha,
+                    other.red * other.alpha,
+                    other.green * other.alpha,
+                    other.blue * other.alpha ];
+        let diff = start.iter().zip(&end)
+                               .fold(0.0f64, |n, (&a, &b)| {
+                                   let diff = (a - b) as f64;
+                                   n + diff * diff
+                               });
+        Ok(diff)
+    }
+}
+
 impl ComputeDistance for CalcLengthOrPercentage {
     #[inline]
     fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
         self.compute_squared_distance(other).map(|sq| sq.sqrt())
     }
 
     #[inline]
     fn compute_squared_distance(&self, other: &Self) -> Result<f64, ()> {