Skip to content

Commit 3986a03

Browse files
committed
Add unlerp() as inverse of lerp()
1 parent a0815f9 commit 3986a03

File tree

10 files changed

+29
-10
lines changed

10 files changed

+29
-10
lines changed

docs/api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ codebase: 'https://github.com/evercoder/culori/blob/main'
100100
<li><a href='#color-spaces'>rgb</a></li>
101101
<li><a href='#round'>round</a></li>
102102
<li><a href='#samples'>samples</a></li>
103+
<li><a href='#unlerp'>unlerp</a></li>
103104
<li><a href='#useMode'>useMode</a></li>
104105
<li><a href='#wcagContrast'>wcagContrast</a></li>
105106
<li><a href='#wcagLuminance'>wcagLuminance</a></li>
@@ -838,6 +839,12 @@ lerp(5, 10, 0.5);
838839
// ⇒ 7.5
839840
```
840841

842+
<a id="unlerp" href="#unlerp">#</a> **unlerp**(_a_, _b_, _v_) → _value_
843+
844+
<span aria-label='Source:'>☞</span> [src/interpolate/lerp.js]({{codebase}}/src/interpolate/lerp.js)
845+
846+
Returns the point `t` at which the value `v` is located between the values `a` and `b`. The inverse of `lerp`.
847+
841848
### Mappings
842849

843850
<a id="mapper" href="#mapper">#</a> **mapper**(_fn_, _mode = "rgb"_) → _function (color | string)_

src/deficiency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import converter from './converter.js';
22
import prepare from './_prepare.js';
3-
import lerp from './interpolate/lerp.js';
3+
import { lerp } from './interpolate/lerp.js';
44

55
let rgb = converter('rgb');
66

src/filter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { mapper, mapTransferLinear } from './map.js';
22
import converter from './converter.js';
33
import prepare from './_prepare.js';
4-
import lerp from './util/lerp.js';
54
import { getMode } from './modes.js';
65

76
const minzero = v => Math.max(v, 0);
87
const clamp = v => Math.max(Math.min(v, 1), 0);
8+
const lerp = (a, b, t) =>
9+
a === undefined || b === undefined ? undefined : a + t * (b - a);
910

1011
const matrixSepia = amount => {
1112
let a = 1 - clamp(amount);

src/index-fn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export {
9393
interpolatorSplineMonotoneClosed
9494
} from './interpolate/splineMonotone.js';
9595

96-
export { default as lerp } from './interpolate/lerp.js';
96+
export { lerp, unlerp } from './interpolate/lerp.js';
9797
export { default as samples } from './samples.js';
9898
export { default as displayable } from './displayable.js';
9999
export { clampRgb, clampChroma } from './clamp.js';

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export {
9494
interpolatorSplineMonotoneClosed
9595
} from './interpolate/splineMonotone.js';
9696

97-
export { default as lerp } from './interpolate/lerp.js';
97+
export { lerp, unlerp } from './interpolate/lerp.js';
9898
export { default as samples } from './samples.js';
9999
export { default as displayable } from './displayable.js';
100100
export { clampRgb, clampChroma } from './clamp.js';

src/interpolate/lerp.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
const lerp = (a, b, t) => a + t * (b - a);
2+
const unlerp = (a, b, v) => (v - a) / (b - a);
23

3-
export default lerp;
4+
export { lerp, unlerp };

src/interpolate/linear.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import lerp from './lerp.js';
1+
import { lerp } from './lerp.js';
22
import { interpolatorPiecewise } from './piecewise.js';
33

44
export const interpolatorLinear = interpolatorPiecewise(lerp);

src/util/lerp.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/api.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const API_FULL = [
208208
'serializeHex8',
209209
'serializeHsl',
210210
'serializeRgb',
211+
'unlerp',
211212
'useMode',
212213
'useParser',
213214
'wcagContrast',
@@ -442,6 +443,7 @@ const API_FN = [
442443
'serializeHex8',
443444
'serializeHsl',
444445
'serializeRgb',
446+
'unlerp',
445447
'useMode',
446448
'useParser',
447449
'wcagContrast',

test/lerp.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tape from 'tape';
2+
import { lerp, unlerp } from '../src/index.js';
3+
4+
tape('lerp()', t => {
5+
t.equal(lerp(10, 2, 0.5), 6);
6+
t.end();
7+
});
8+
9+
tape('unlerp()', t => {
10+
t.equal(unlerp(5, 10, 2.5), -0.5);
11+
t.end();
12+
});

0 commit comments

Comments
 (0)