~zsombi/ubuntu-ui-toolkit/listitemSelectModeBugs

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/1.3/mathUtils.js

  • Committer: Zsombor Egri
  • Date: 2015-11-20 16:37:59 UTC
  • mfrom: (1662.2.62 staging)
  • Revision ID: zsombor.egri@canonical.com-20151120163759-8p94jar0o53nbu95
staging sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2012-2015 Canonical Ltd.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU Lesser General Public License as published by
6
 
 * the Free Software Foundation; version 3.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU Lesser General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 */
16
 
 
17
 
/*!
18
 
  \qmltype mathUtils
19
 
  \inqmlmodule Ubuntu.Components 1.3
20
 
  \ingroup ubuntu
21
 
  \brief Various mathematical utility functions.
22
 
 */
23
 
 
24
 
.pragma library
25
 
 
26
 
/*!
27
 
  \qmlmethod mathUtils::clamp(x, min, max)
28
 
  Ensure the value x is between min and max
29
 
 */
30
 
function clamp(x, min, max) {
31
 
    if (min <= max) {
32
 
        return Math.max(min, Math.min(x, max));
33
 
    } else {
34
 
        // swap min/max if min > max
35
 
        return clamp(x, max, min);
36
 
    }
37
 
}
38
 
 
39
 
/*!
40
 
  \qmlmethod mathUtils::lerp(delta, from, to)
41
 
  Get the linear interpolation
42
 
 */
43
 
function lerp(delta, from, to) {
44
 
    return ((1.0 - delta) * from) + (delta * to);
45
 
}
46
 
 
47
 
/*!
48
 
  \qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
49
 
  Linearly project a value x from [xmin, xmax] into [ymin, ymax]
50
 
 */
51
 
function projectValue(x, xmin, xmax, ymin, ymax) {
52
 
    return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin)
53
 
}
54
 
 
55
 
/*!
56
 
  \qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
57
 
  Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
58
 
 */
59
 
function clampAndProject(x, xmin, xmax, ymin, ymax) {
60
 
    return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax)
61
 
}