Skip to content

Commit 4e4df90

Browse files
authored
Update domain function to return all scaled-positions rather than only [min, max] (#380)
* Update domain function to return all scaled-positions rather than only [min, max] See #376 (comment) * Add no-args doc for scale.domain * Add tests for multi-stop domain
1 parent f50528f commit 4e4df90

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ chroma.scale(['yellow', 'red', 'black']);
827827
### scale.domain
828828
#### (domain)
829829

830-
You can change the input domain to match your specific use case.
830+
You can change the input domain to match your specific use case. If called with no arguments, `scale.domain` returns the original array of positions along the scale where the color ramp was sampled.
831831

832832
```js
833833
// default domain is [0,1]

src/generator/scale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export default function (colors) {
196196

197197
f.domain = function (domain) {
198198
if (!arguments.length) {
199-
return _domain;
199+
return _pos.map(p => _min + p * (_max - _min));;
200200
}
201201
_min = domain[0];
202202
_max = domain[domain.length - 1];

test/scales.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,25 @@ describe('Some tests for scale()', () => {
410410
expect(f(0.625).hex()).toBe(f1(0.75).hex());
411411
});
412412
});
413+
414+
describe('multi-stop domain with matching ramp stops', () => {
415+
const f = scale(['yellow', 'red', 'black']).domain([0, 25, 100]);
416+
417+
it('returns the original domain positions', () => {
418+
expect(f.domain()).toEqual([0, 25, 100]);
419+
});
420+
421+
it('maps first stop', () => {
422+
expect(f(0).hex()).toBe('#ffff00');
423+
});
424+
425+
it('maps mid stop', () => {
426+
expect(f(25).hex()).toBe('#ff8080');
427+
});
428+
429+
it('maps last stop', () => {
430+
expect(f(100).hex()).toBe('#000000');
431+
});
432+
});
433+
413434
});

0 commit comments

Comments
 (0)