Skip to content

Commit

Permalink
* fix wrong handling of values outside scale domain due to internal M…
Browse files Browse the repository at this point in the history
…ath.pow usage. See [Issue 331](gka#331)

Signed-off-by: regorxxx <[email protected]>
  • Loading branch information
regorxxx committed Mar 10, 2024
1 parent 295dbab commit 3b85a96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/generator/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ module.exports = function(colors) {
t = tMapLightness(t); // lightness correction
}

if (_gamma !== 1) { t = pow(t, _gamma); }
if (t > 1) { t = 1; } // pow fails with negative bases, but values are always clamped to the extremes
else if (t < 0) { t = 0 }
else if (_gamma !== 1) { t = pow(t, _gamma); }

t = _padding[0] + (t * (1 - _padding[0] - _padding[1]));

Expand Down
10 changes: 10 additions & 0 deletions test/scales.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,15 @@ vows
)
}
},

'scale domain': {
topic: {
f: scale(['white','black']).domain([1, 3]).gamma(1.1)
},
'should return left color within domain'(topic) { assert.equal(topic.f(1).hex(), '#ffffff'); },
'should return right color within domain'(topic) { assert.equal(topic.f(3).hex(), '#000000'); },
'should return left color outside domain'(topic) { assert.equal(topic.f(0).hex(), '#ffffff'); },
'should return right color outside domain'(topic) { assert.equal(topic.f(4).hex(), '#000000'); }
},

}).export(module);

0 comments on commit 3b85a96

Please sign in to comment.