blob: b3c0ca6fb4e32d73ed557c085e3f3569aa1bfba4 [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
2* Copyright 2019 Google LLC
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#ifndef SkCurve_DEFINED
9#define SkCurve_DEFINED
10
Brian Osman125daa42019-02-20 12:25:20 -050011#include "SkColor.h"
Brian Osman7c979f52019-02-12 13:27:51 -050012#include "SkScalar.h"
Brian Osman8b6283f2019-02-14 16:55:21 -050013#include "SkTArray.h"
Brian Osman7c979f52019-02-12 13:27:51 -050014
15class SkFieldVisitor;
16class SkRandom;
17
Brian Osmane12e4992019-02-19 15:39:18 -050018/**
19 * SkCurve implements a keyframed 1D function, useful for animating values over time. This pattern
20 * is common in digital content creation tools. An SkCurve might represent rotation, scale, opacity,
21 * or any other scalar quantity.
22 *
23 * An SkCurve has a logical domain of [0, 1], and is made of one or more SkCurveSegments.
24 * Each segment describes the behavior of the curve in some sub-domain. For an SkCurve with N
25 * segments, there are (N - 1) intermediate x-values that subdivide the domain. The first and last
26 * x-values are implicitly 0 and 1:
27 *
28 * 0 ... x[0] ... x[1] ... ... 1
29 * Segment_0 Segment_1 ... Segment_N-1
30 *
31 * Each segment describes a function over [0, 1] - x-values are re-normalized to the segment's
32 * domain when being evaluated. The segments are cubic polynomials, defined by four values (fMin).
33 * These are the values at x=0 and x=1, as well as control points at x=1/3 and x=2/3.
34 *
35 * For segments with fConstant == true, only the first value is used (fMin[0]).
36 *
37 * Each segment has two additional features for creating interesting (and varied) animation:
38 * - A segment can be ranged. Ranged segments have two sets of coefficients, and a random value
39 * taken from the SkRandom will be used to lerp betwen them. Typically, the SkRandom passed to
40 * eval will be in the same state at each call, so this value will be stable. That causes a
41 * ranged SkCurve to produce a single smooth cubic function somewhere within the range defined
42 * by fMin and fMax.
43 * - A segment can be bidirectional. In that case, after a value is computed, it will be negated
44 * 50% of the time.
45 */
46
Brian Osman8b6283f2019-02-14 16:55:21 -050047struct SkCurveSegment {
48 SkScalar eval(SkScalar x, SkRandom& random) const;
49 void visitFields(SkFieldVisitor* v);
50
51 void setConstant(SkScalar c) {
52 fConstant = true;
53 fRanged = false;
54 fMin[0] = c;
55 }
56
57 SkScalar fMin[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
58 SkScalar fMax[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
59
60 bool fConstant = true;
61 bool fRanged = false;
62 bool fBidirectional = false;
63};
Brian Osman7c979f52019-02-12 13:27:51 -050064
65struct SkCurve {
66 SkCurve(SkScalar c = 0.0f) {
Brian Osman8b6283f2019-02-14 16:55:21 -050067 fSegments.push_back().setConstant(c);
Brian Osman7c979f52019-02-12 13:27:51 -050068 }
69
Brian Osmane12e4992019-02-19 15:39:18 -050070 // Evaluate this curve at x, using random for curves that have ranged or bidirectional segments.
Brian Osman8b6283f2019-02-14 16:55:21 -050071 SkScalar eval(SkScalar x, SkRandom& random) const;
Brian Osman7c979f52019-02-12 13:27:51 -050072 void visitFields(SkFieldVisitor* v);
Brian Osmane12e4992019-02-19 15:39:18 -050073
74 // Returns the (very conversative) range of this SkCurve in extents (as [minimum, maximum]).
Brian Osman112aa2d2019-02-15 10:45:56 -050075 void getExtents(SkScalar extents[2]) const;
Brian Osman7c979f52019-02-12 13:27:51 -050076
Brian Osmane12e4992019-02-19 15:39:18 -050077 // It should always be true that (fXValues.count() + 1) == fSegments.count()
Brian Osman8b6283f2019-02-14 16:55:21 -050078 SkTArray<SkScalar, true> fXValues;
79 SkTArray<SkCurveSegment, true> fSegments;
Brian Osman7c979f52019-02-12 13:27:51 -050080};
81
Brian Osman125daa42019-02-20 12:25:20 -050082/**
83 * SkColorCurve is similar to SkCurve, but keyframes 4D values - specifically colors. Because
84 * negative colors rarely make sense, SkColorCurves do not support bidirectional segments, but
85 * support all other features (including cubic interpolation).
86 */
87
88struct SkColorCurveSegment {
89 SkColorCurveSegment() {
90 for (int i = 0; i < 4; ++i) {
91 fMin[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
92 fMax[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
93 }
94 }
95
96 SkColor4f eval(SkScalar x, SkRandom& random) const;
97 void visitFields(SkFieldVisitor* v);
98
99 void setConstant(SkColor4f c) {
100 fConstant = true;
101 fRanged = false;
102 fMin[0] = c;
103 }
104
105 SkColor4f fMin[4];
106 SkColor4f fMax[4];
107
108 bool fConstant = true;
109 bool fRanged = false;
110};
111
112struct SkColorCurve {
113 SkColorCurve(SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f }) {
114 fSegments.push_back().setConstant(c);
115 }
116
117 SkColor4f eval(SkScalar x, SkRandom& random) const;
118 void visitFields(SkFieldVisitor* v);
119
120 SkTArray<SkScalar, true> fXValues;
121 SkTArray<SkColorCurveSegment, true> fSegments;
122};
123
Brian Osman7c979f52019-02-12 13:27:51 -0500124#endif // SkCurve_DEFINED