Skip to content

Commit 2271a30

Browse files
remove a debug comment and updated unit test for p5.color.js
1 parent 4372cbf commit 2271a30

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

src/color/p5.Color.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ class Color {
331331
colorString = serialize(this._color, {
332332
format: outputFormat
333333
});
334-
// // DEBUG LOG
335-
// if (format === '#rrggbb') {
336-
// console.log('DEBUG:', format, 'Input:', this._color, 'Got:', colorString);
337-
// }
334+
338335
if (format === '#rrggbb') {
339336
colorString = String(colorString);
340337

test/unit/color/p5.Color.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,58 @@ 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+
suite('#rrggbb format', function() {
796+
test('should expand short hex (#rgb) to full 6-digit format', function() {
797+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
798+
const c = mockP5Prototype.color('#f06');
799+
const result = c.toString('#rrggbb');
800+
assert.equal(result, '#ff0066');
801+
});
802+
803+
test('should truncate hex with alpha (#rrggbbaa) to 6 digits', function() {
804+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
805+
const c = mockP5Prototype.color(255, 0, 102, 128);
806+
const result = c.toString('#rrggbb');
807+
assert.equal(result.length, 7);
808+
assert.equal(result, '#ff0066');
809+
});
810+
811+
test('should output lowercase hex string', function() {
812+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
813+
const c = mockP5Prototype.color(255, 170, 187);
814+
const result = c.toString('#rrggbb');
815+
assert.equal(result, result.toLowerCase());
816+
assert.equal(result, '#ffaabb');
817+
});
818+
819+
test('should correctly format standard RGB colors', function() {
820+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
821+
const c = mockP5Prototype.color(255, 0, 102);
822+
const result = c.toString('#rrggbb');
823+
assert.equal(result, '#ff0066');
824+
});
825+
826+
test('should correctly format black color', function() {
827+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
828+
const c = mockP5Prototype.color(0, 0, 0);
829+
const result = c.toString('#rrggbb');
830+
assert.equal(result, '#000000');
831+
});
796832

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-
// });
833+
test('should correctly format white color', function() {
834+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
835+
const c = mockP5Prototype.color(255, 255, 255);
836+
const result = c.toString('#rrggbb');
837+
assert.equal(result, '#ffffff');
838+
});
802839

803-
// NOTE: need some tests here
840+
test('should handle colors created from 6-digit hex string', function() {
841+
mockP5Prototype.colorMode(mockP5Prototype.RGB, 255, 255, 255, 255);
842+
const c = mockP5Prototype.color('#9932cc');
843+
const result = c.toString('#rrggbb');
844+
assert.equal(result, '#9932cc');
845+
});
846+
});
804847
});
805848
});

0 commit comments

Comments
 (0)