Skip to content

Commit 8f8d94a

Browse files
Fix Color.toString() hex format support for issue #8451
1 parent 626996e commit 8f8d94a

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

src/color/p5.Color.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ class Color {
336336
* @param {String} [format] how the color string will be formatted.
337337
* Leaving this empty formats the string as rgba(r, g, b, a).
338338
* '#rgb' '#rgba' '#rrggbb' and '#rrggbbaa' format as hexadecimal color codes.
339+
* 'hex' is an alias for hexadecimal format.
339340
* 'rgb' 'hsb' and 'hsl' return the color formatted in the specified color mode.
340341
* 'rgba' 'hsba' and 'hsla' are the same as above but with alpha channels.
341342
* 'rgb%' 'hsb%' 'hsl%' 'rgba%' 'hsba%' and 'hsla%' format as percentages.
@@ -369,12 +370,23 @@ class Color {
369370
return this._defaultStringValue;
370371
}
371372

373+
// Map p5.js format strings to colorjs.io format strings
374+
let colorjsFormat = format;
375+
if (format === 'hex') {
376+
// 'hex' is a common alias for '#rrggbb'
377+
colorjsFormat = 'hex';
378+
} else if (format === '#rrggbb' || format === '#rrggbbaa' ||
379+
format === '#rgb' || format === '#rgba') {
380+
// colorjs.io uses 'hex' for all hex formats
381+
colorjsFormat = 'hex';
382+
}
383+
372384
const key = `${this._color.space.id}-${this._color.coords.join(',')}-${this._color.alpha}-${format}`;
373385
let colorString = serializationMap.get(key);
374386

375387
if(!colorString){
376388
colorString = serialize(this._color, {
377-
format
389+
format: colorjsFormat
378390
});
379391
if (serializationMap.size > 1000) {
380392
serializationMap.delete(serializationMap.keys().next().value)

test/unit/color/p5.Color.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,29 @@ suite('p5.Color', function() {
791791
});
792792
});
793793

794-
suite.todo('p5.Color.prototype.toString', function() {
795-
// var colorStr;
794+
suite('p5.Color.prototype.toString', function() {
795+
test('should format as #rrggbb hex string', function() {
796+
const testColor = mockP5Prototype.color(153, 50, 204); // darkorchid
797+
const result = testColor.toString('#rrggbb');
798+
assert.equal(result, '#9932cc');
799+
});
796800

797-
// beforeEach(function() {
798-
// mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
799-
// c = mockP5Prototype.color(128, 0, 128, 128);
800-
// colorStr = c.toString();
801-
// });
801+
test('should format as hex string with "hex" alias', function() {
802+
const testColor = mockP5Prototype.color(153, 50, 204); // darkorchid
803+
const result = testColor.toString('hex');
804+
assert.equal(result, '#9932cc');
805+
});
802806

803-
// NOTE: need some tests here
807+
test('should handle single digit hex values with padding', function() {
808+
const testColor = mockP5Prototype.color(10, 5, 15);
809+
const result = testColor.toString('#rrggbb');
810+
assert.equal(result, '#0a050f');
811+
});
812+
813+
test('should format hex with alpha channel', function() {
814+
const testColor = mockP5Prototype.color(153, 50, 204, 128);
815+
const result = testColor.toString('hex');
816+
assert.match(result, /^#[0-9a-f]{8}$/i);
817+
});
804818
});
805819
});

0 commit comments

Comments
 (0)