LinearScale.java revision aca904e29aea542b6e6a4fcc837759a57a270a49
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera.ui.motion; 18 19/** 20 * Represents a discrete linear scale function. 21 */ 22public final class LinearScale { 23 private final float mDomainA; 24 private final float mDomainB; 25 private final float mRangeA; 26 private final float mRangeB; 27 28 private final float mScale; 29 30 public LinearScale(float domainA, float domainB, float rangeA, float rangeB) { 31 mDomainA = domainA; 32 mDomainB = domainB; 33 mRangeA = rangeA; 34 mRangeB = rangeB; 35 36 // Precomputed ratio between input domain and output range. 37 mScale = (mRangeB - mRangeA) / (mDomainB - mDomainA); 38 } 39 40 /** 41 * Clamp a given domain value to the given domain. 42 */ 43 public float clamp(float domainValue) { 44 if (mDomainA > mDomainB) { 45 return Math.max(mDomainB, Math.min(mDomainA, domainValue)); 46 } 47 48 return Math.max(mDomainA, Math.min(mDomainB, domainValue)); 49 } 50 51 /** 52 * Returns true if the value is within the domain. 53 */ 54 public boolean isInDomain(float domainValue) { 55 if (mDomainA > mDomainB) { 56 return domainValue >= mDomainA && domainValue <= mDomainB; 57 } 58 return domainValue >= mDomainB && domainValue <= mDomainA; 59 } 60 61 /** 62 * Linearly scale a given domain value into the output range. 63 */ 64 public float scale(float domainValue) { 65 return mRangeA + (domainValue - mDomainA) * mScale; 66 } 67 68 /** 69 * For the current domain and range parameters produce a new scale function 70 * that is the inverse of the current scale function. 71 */ 72 public LinearScale inverse() { 73 return new LinearScale(mRangeA, mRangeB, mDomainA, mDomainB); 74 } 75}