Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/color/p5.Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const map = (n, start1, stop1, start2, stop2, clamp) => {
};

const toHexComponent = v => {
const vInt = ~~(v * 255);
const vInt = Math.round(v * 255);
const hex = vInt.toString(16);
if (hex.length < 2) {
return '0' + hex;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/color/p5.Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,5 +850,27 @@ suite('p5.Color', function () {
assert.equal(result, '#9932cc');
});
});

suite('default format with fractional channel values', function () {
test('should round fractional channel values matching #rrggbb format', function () {
mockP5Prototype.colorMode(mockP5Prototype.RGB, 100);
const c = mockP5Prototype.color(50, 0, 0);
assert.equal(c.toString(), '#800000');
assert.equal(c.toString(), c.toString('#rrggbb'));
});

test('should round fractional channel in 255 range', function () {
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255);
const c = mockP5Prototype.color(127.5, 0, 0);
assert.equal(c.toString(), '#800000');
assert.equal(c.toString(), c.toString('#rrggbb'));
});

test('should round fractional alpha byte', function () {
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255);
const c = mockP5Prototype.color(255, 0, 102, 127.5);
assert.equal(c.toString(), '#ff006680');
});
});
});
});