summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/motion/LinearScale.java
blob: 5886f6882a7ad38a2326e509c26475793650cc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.ui.motion;

/**
 * Represents a discrete linear scale function.
 */
public final class LinearScale {
    private final float mDomainA;
    private final float mDomainB;
    private final float mRangeA;
    private final float mRangeB;

    private final float mScale;

    public LinearScale(float domainA, float domainB, float rangeA, float rangeB) {
        mDomainA = domainA;
        mDomainB = domainB;
        mRangeA = rangeA;
        mRangeB = rangeB;

        // Precomputed ratio between input domain and output range.
        float scale = (mRangeB - mRangeA) / (mDomainB - mDomainA);
        mScale = Float.isNaN(scale) ? 0.0f : scale;
    }

    /**
     * Clamp a given domain value to the given domain.
     */
    public float clamp(float domainValue) {
        if (mDomainA > mDomainB) {
            return Math.max(mDomainB, Math.min(mDomainA, domainValue));
        }

        return Math.max(mDomainA, Math.min(mDomainB, domainValue));
    }

    /**
     * Returns true if the value is within the domain.
     */
    public boolean isInDomain(float domainValue) {
        if (mDomainA > mDomainB) {
            return domainValue <= mDomainA && domainValue >= mDomainB;
        }
        return domainValue >= mDomainA && domainValue <= mDomainB;
    }

    /**
     * Linearly scale a given domain value into the output range.
     */
    public float scale(float domainValue) {
        return mRangeA + (domainValue - mDomainA) * mScale;
    }

    /**
     * For the current domain and range parameters produce a new scale function
     * that is the inverse of the current scale function.
     */
    public LinearScale inverse() {
        return new LinearScale(mRangeA, mRangeB, mDomainA, mDomainB);
    }

    @Override
    public String toString() {
        return "LinearScale{" +
              "mDomainA=" + mDomainA +
              ", mDomainB=" + mDomainB +
              ", mRangeA=" + mRangeA +
              ", mRangeB=" + mRangeB + "}";
    }
}