1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_BLEND_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_BLEND_H_
7 
8 #include "third_party/blink/renderer/platform/geometry/float_point.h"
9 #include "third_party/blink/renderer/platform/geometry/int_point.h"
10 #include "third_party/blink/renderer/platform/geometry/layout_unit.h"
11 #include "third_party/blink/renderer/platform/platform_export.h"
12 #include "third_party/blink/renderer/platform/wtf/math_extras.h"
13 
14 #include <type_traits>
15 
16 namespace blink {
17 
Blend(int from,int to,double progress)18 inline int Blend(int from, int to, double progress) {
19   return static_cast<int>(lround(from + (to - from) * progress));
20 }
21 
22 // For unsigned types.
23 template <typename T>
Blend(T from,T to,double progress)24 inline T Blend(T from, T to, double progress) {
25   static_assert(std::is_integral<T>::value,
26                 "blend can only be used with integer types");
27   return clampTo<T>(round(to > from ? from + (to - from) * progress
28                                     : from - (from - to) * progress));
29 }
30 
Blend(double from,double to,double progress)31 inline double Blend(double from, double to, double progress) {
32   return from + (to - from) * progress;
33 }
34 
Blend(float from,float to,double progress)35 inline float Blend(float from, float to, double progress) {
36   return static_cast<float>(from + (to - from) * progress);
37 }
38 
Blend(LayoutUnit from,LayoutUnit to,double progress)39 inline LayoutUnit Blend(LayoutUnit from, LayoutUnit to, double progress) {
40   return LayoutUnit(from + (to - from) * progress);
41 }
42 
Blend(const IntPoint & from,const IntPoint & to,double progress)43 inline IntPoint Blend(const IntPoint& from,
44                       const IntPoint& to,
45                       double progress) {
46   return IntPoint(Blend(from.X(), to.X(), progress),
47                   Blend(from.Y(), to.Y(), progress));
48 }
49 
Blend(const FloatPoint & from,const FloatPoint & to,double progress)50 inline FloatPoint Blend(const FloatPoint& from,
51                         const FloatPoint& to,
52                         double progress) {
53   return FloatPoint(Blend(from.X(), to.X(), progress),
54                     Blend(from.Y(), to.Y(), progress));
55 }
56 
57 }  // namespace blink
58 
59 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_BLEND_H_
60