Skip to content

Commit bf773b7

Browse files
authored
Merge pull request #8363 from Piyushrathoree/fix-createColorPicker-ignore-initial-color
fix createColorPicker ignores initial colour arg on chrome
2 parents 626996e + 3ef8127 commit bf773b7

2 files changed

Lines changed: 75 additions & 13 deletions

File tree

src/color/p5.Color.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,26 +364,45 @@ class Color {
364364
* </code>
365365
* </div>
366366
*/
367-
toString(format) {
368-
if (format === undefined && this._defaultStringValue !== undefined) {
367+
toString(format) {
368+
if (format === undefined && this._defaultStringValue !== undefined) {
369369
return this._defaultStringValue;
370+
}
371+
372+
let outputFormat = format;
373+
if (format === '#rrggbb') {
374+
outputFormat = 'hex';
370375
}
371376

372377
const key = `${this._color.space.id}-${this._color.coords.join(',')}-${this._color.alpha}-${format}`;
373378
let colorString = serializationMap.get(key);
374379

375-
if(!colorString){
380+
if (!colorString) {
376381
colorString = serialize(this._color, {
377-
format
382+
format: outputFormat
378383
});
384+
385+
if (format === '#rrggbb') {
386+
colorString = String(colorString);
387+
if (colorString.length === 4) {
388+
const r = colorString[1];
389+
const g = colorString[2];
390+
const b = colorString[3];
391+
colorString = `#${r}${r}${g}${g}${b}${b}`;
392+
}
393+
if (colorString.length > 7) {
394+
colorString = colorString.slice(0, 7);
395+
}
396+
colorString = colorString.toLowerCase();
397+
}
398+
379399
if (serializationMap.size > 1000) {
380400
serializationMap.delete(serializationMap.keys().next().value)
381401
}
382402
serializationMap.set(key, colorString);
383403
}
384404
return colorString;
385405
}
386-
387406
/**
388407
* Checks the contrast between two colors. This method returns a boolean
389408
* value to indicate if the two color has enough contrast. `true` means that

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)