| 1 |
/* |
| 2 |
* Copyright 2013 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 |
.pragma library |
| 18 |
|
| 19 |
// FIXME(loicm) It would be better to have these functions available in a global |
| 20 |
// set of common native C++ functions. |
| 21 |
|
| 22 |
function clamp(x, min, max) { |
| 23 |
if (min <= max) { |
| 24 |
return Math.max(min, Math.min(x, max)); |
| 25 |
} else { |
| 26 |
// swap min/max if min > max |
| 27 |
return clamp(x, max, min); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
function lerp(x, a, b) { |
| 32 |
return ((1.0 - x) * a) + (x * b); |
| 33 |
} |
| 34 |
|
| 35 |
// Linearly project a value x from [xmin, xmax] into [ymin, ymax] |
| 36 |
function projectValue(x, xmin, xmax, ymin, ymax) { |
| 37 |
return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin) |
| 38 |
} |
| 39 |
|
| 40 |
function clampAndProject(x, xmin, xmax, ymin, ymax) { |
| 41 |
return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax) |
| 42 |
} |