MCPcopy Index your code
hub / github.com/processing/p5.js / toString

Method toString

src/color/p5.Color.js:368–406  ·  view source on GitHub ↗

* Returns the color formatted as a `String`. * * Calling `myColor.toString()` can be useful for debugging, as in * `print(myColor.toString())`. It's also helpful for using p5.js with other * libraries. * * The parameter, `format`, is optional. If a format string is passed, as in

(format)

Source from the content-addressed store, hash-verified

366 * }
367 */
368 toString(format) {
369 if (format === undefined && this._defaultStringValue !== undefined) {
370 return this._defaultStringValue;
371 }
372
373 let outputFormat = format;
374 if (format === '#rrggbb') {
375 outputFormat = 'hex';
376 }
377
378 const key = `${this._color.space.id}-${this._color.coords.join(',')}-${this._color.alpha}-${format}`;
379 let colorString = serializationMap.get(key);
380
381 if (!colorString) {
382 colorString = serialize(this._color, {
383 format: outputFormat
384 });
385
386 if (format === '#rrggbb') {
387 colorString = String(colorString);
388 if (colorString.length === 4) {
389 const r = colorString[1];
390 const g = colorString[2];
391 const b = colorString[3];
392 colorString = `#${r}${r}${g}${g}${b}${b}`;
393 }
394 if (colorString.length > 7) {
395 colorString = colorString.slice(0, 7);
396 }
397 colorString = colorString.toLowerCase();
398 }
399
400 if (serializationMap.size > 1000) {
401 serializationMap.delete(serializationMap.keys().next().value)
402 }
403 serializationMap.set(key, colorString);
404 }
405 return colorString;
406 }
407 /**
408 * Checks the contrast between two colors. This method returns a boolean
409 * value to indicate if the two color has enough contrast. `true` means that

Callers 6

backgroundMethod · 0.45
fillMethod · 0.45
strokeMethod · 0.45
eraseMethod · 0.45
beginClipMethod · 0.45
toHexComponentFunction · 0.45

Calls 4

sliceMethod · 0.80
deleteMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected